C# 在屏幕中位置相同但位置不同的两个窗体

C# 在屏幕中位置相同但位置不同的两个窗体,c#,winforms,C#,Winforms,我在form1中新建一个form2,设置form2.Location=form1.Location。当程序运行时,它们显示在屏幕上的不同位置,但它们的位置.X完全相同。两个X之间存在9像素的差异。太奇怪了 当我设置overlay.Location=新点时(this.Location.X,this.Location.Y+(Height-ClientSize.Height)) 当我设置overlay.Location=新点时(this.Location.X+9,this.Location.Y+(H

我在form1中新建一个form2,设置
form2.Location=form1.Location
。当程序运行时,它们显示在屏幕上的不同位置,但它们的
位置.X
完全相同。两个
X
之间存在9像素的差异。太奇怪了

当我设置
overlay.Location=新点时(this.Location.X,this.Location.Y+(Height-ClientSize.Height))

当我设置
overlay.Location=新点时(this.Location.X+9,this.Location.Y+(Height-ClientSize.Height)-9)

这是密码

表格1

表格2

公共部分类测试表单:DevExpress.XtraEditors.XtraForm
{
public delegate void DoAThing();
公共活动展示;
公共测试表单()
{
初始化组件();
}
private void SimpleButton 1u鼠标单击(对象发送器,MouseEventArgs e)
{
ShowOnMeMo?.Invoke();
}
}

多亏@Dai的回答,我找到了解决这个问题的方法

int nonclientWidth=Bounds.Width-ClientSize.Width,nonclientHeight=(Bounds.Height-SystemInformation.CaptionHeight)-ClientSize.Height;
overlay.Location=新点(this.Location.X+nonclienttwidth/2,this.Location.Y+SystemInformation.CaptionHeight+nonclientHeight/2);

坐标不同,因为您的
覆盖
表单没有非客户端区域(您关闭了系统标题栏和窗口边框)。你需要调整你的坐标来解释这个差异。(不要硬编码差异,因为它们随每个Windows版本的不同而不同,而是使用GetSystemMetrics:有一个属性控制窗体的显示方式:
form1.StartPosition=FormStartPosition.Manual
,请参阅。@Dai谢谢,这是正确的答案。我确实找到了标题栏的高度,但标题栏上有段落
SM\u CYCAPTION
在打印了
GetSystemMetrics()
的所有结果之后,我们找不到非客户端的宽度或高度。但是我确实找到了另一种方法,通过使用
Form.Bounds
SystemInformation.CaptionHeight
,来确保正确的非客户端大小。我将代码显示为答案
public partial class Form1 : Form
{
    bool isOverlayGenerated = false;
    TestForm overlay = new TestForm();
    public Form1()
    {
        InitializeComponent();
        VisibleChanged += Form1_VisibleChanged;
        overlay.Size = ClientSize;
        overlay.ShowOnMeMo += () =>
        {
            overlay.memoEdit1.EditValue = $"form1: ({Location.X}, {Location.Y})";
            overlay.memoEdit1.EditValue += Environment.NewLine + $"form2: ({overlay.Location.X}, {overlay.Location.Y})";
        };
        overlay.Owner = this;
    }

    private void Form1_VisibleChanged(object sender, EventArgs e)
    {
        if (!isOverlayGenerated)
        {
            overlay.Location = new Point(this.Location.X, this.Location.Y + (Height - ClientSize.Height));
            //overlay.Location = new Point(this.Location.X + 9, this.Location.Y + (Height - ClientSize.Height) - 9);
            isOverlayGenerated = true;
            overlay.Show();
        }
    }
}