【问题标题】:What makes a Winform position initially stale?是什么让 Winform 职位最初过时?
【发布时间】:2009-07-14 17:14:37
【问题描述】:

下面的代码演示了一个非常简单的问题;我希望我只是错过了有人可能会透露的设置。

目标

(1) 启动主winform(MainForm)。
(2) 按 按钮显示辅助winform(ShadowForm),它是半透明的,应该完全覆盖MainForm。

实际发生的情况

场景 1:启动主 winform,然后按下按钮:ShadowForm 以正确的大小显示,但位置不正确,向右下方(好像是级联的)。 按下按钮再次关闭 ShadowForm。再次按下按钮重新打开 ShadowForm,现在它位于正确的位置,覆盖 MainForm。

场景 2:启动主 winform,移动它,然后按下按钮:ShadowForm 以正确的大小显示,但位置不正确(MainForm 在移动之前的位置)。按下按钮关闭;再次按下以重新打开,现在 ShadowForm 处于正确位置。

using System;
using System.Windows.Forms;

namespace LocationTest
{
    static class Program
    {
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

    public class MainForm : Form
    {
        ShadowForm shadowForm = new ShadowForm();
        Button button1 = new Button();
        System.ComponentModel.IContainer components = null;

        public MainForm()
        {
            this.SuspendLayout();
            this.button1.Location = new System.Drawing.Point(102, 44);
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.ResumeLayout(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (shadowForm.Visible) { shadowForm.Hide(); }
            else
            {
                shadowForm.Size = Size; // this always works
                shadowForm.Location = Location; // this fails first time, but works second time!
                shadowForm.Show();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) { components.Dispose(); }
            base.Dispose(disposing);
        }
    }

    public class ShadowForm : Form
    {
        private System.ComponentModel.IContainer components = null;

        public ShadowForm()
        {
            this.SuspendLayout();
            this.BackColor = System.Drawing.Color.Black;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Opacity = 0.5;
            this.Click += new System.EventHandler(this.ShadowForm_Click);
            this.ResumeLayout(false);
        }

        private void ShadowForm_Click(object sender, EventArgs e)
        {
            Hide();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) { components.Dispose(); }
            base.Dispose(disposing);
        }
    }
}

【问题讨论】:

    标签: c# winforms overlay transparency


    【解决方案1】:

    在第一次设置您的位置之前,您应该将StartPosition 设置为手动。

    shadowForm.StartPosition = FormStartPosition.Manual;
    shadowForm.Size = Size; // this always works
    shadowForm.Location = Location; // this fails first time, but works second time!
    shadowForm.Show();
    

    或者按照乔尔的建议:

    shadowForm.StartPosition = FormStartPosition.CenterParent;  // Location shouldn't need to be set
    shadowForm.Size = Size; // this always works
    shadowForm.Show();
    

    【讨论】:

    • 或者,当您使用它时,只需将 StartPosition 设置为“CenterParent”,然后完全忘记位置。
    • 感谢您非常快速(且正确!)的回复。我不知道那个特定的属性。您不仅解决了这个问题,而且还解决了我的“待办事项”列表中的另一个项目,其中涉及一个子窗口未在与当前窗口相同的监视器上打开。向你致敬!
    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多