[英]Two form with the same Location but different Position in the screen
我在 form1 中新建了一个 form2,设置form2.Location = form1.Location
。 当程序运行时,它们显示在屏幕上的不同位置,但它们的Location.X
完全相同。 并且两个X
之间有 9 个像素的差异。 太奇怪了。
当我设置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);
这是代码。
表格1
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();
}
}
}
表格2
public partial class TestForm : DevExpress.XtraEditors.XtraForm
{
public delegate void DoAThing();
public event DoAThing ShowOnMeMo;
public TestForm()
{
InitializeComponent();
}
private void simpleButton1_MouseClick(object sender, MouseEventArgs e)
{
ShowOnMeMo?.Invoke();
}
}
感谢@Dai 的回答,我找到了解决这个问题的方法。
int nonclientWidth = Bounds.Width - ClientSize.Width, nonclientHeight = (Bounds.Height - SystemInformation.CaptionHeight) - ClientSize.Height;
overlay.Location = new Point(this.Location.X + nonclientWidth / 2, this.Location.Y + SystemInformation.CaptionHeight + nonclientHeight / 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.