c# - 是什么让 Winform 职位最初变得陈旧?

标签 c# winforms overlay transparency

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

目标

(1) 启动主窗体(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);
        }
    }
}

最佳答案

您应该设置 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();

关于c# - 是什么让 Winform 职位最初变得陈旧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1126763/

相关文章:

c# - 切换到上一个事件的应用程序,例如 Alt-Tab

c# - C# 中的非标准 UI

C# 更改非 unicode 语言或更改位置

c# - 正则表达式无法过滤 WinForms 中的 TextBox 字符

javascript - 简单小巧的纯 javascript 灯箱(对话框覆盖)?

jquery - 如何使用 jQuery 将鼠标悬停在 div 上时显示覆盖 div?

android - 在谷歌地图上扩展覆盖标记?

c# - TcpClient 与服务器通信以保持 c# 中的事件连接?

c# - 路由附加事件 WPF/MVVM 后丢失 EventArgs

c# - Visual Studio 禁用运行时异常?