c# - 两个窗体在屏幕中具有相同的位置但不同的位置
问题描述
我在 form1 中新建了一个 form2,set form2.Location = form1.Location
。程序运行时,它们显示在屏幕上的不同位置,但两者Location.X
完全相同。而且两者相差9像素X
。太奇怪了。
当我设置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);
推荐阅读
- concurrency - 子文档变异操作无法与 XDCR 一起正常工作
- c# - 如何将对象移动到表单应用程序中的一组随机坐标
- python - Tensorflow 错误连接字符和词嵌入
- c++ - 列表插入迭代器超出范围错误
- node.js - 从heroku发出http请求
- google-apps-script - 如何从特定行获取正确的图像 ID 并发送单独的电子邮件?
- ios - 在 Swift/Objective-C 中重用 tableviewcell 时,属性标签不会消失
- google-sheets - 用数组公式替换 IF(AND)) 函数
- c# - Xamarin Forms MacOS 中的 FadeTo 动画无法正常工作
- angular - 如何选择 NGXS 状态的一部分?