C# 仅具有parentWnd的父级中的中心窗体
我正在和开发一个插件,使用Windows窗体显示有关某些文件的信息 在api文档中,建议使用此类将Pdm窗口设置为父窗口C# 仅具有parentWnd的父级中的中心窗体,c#,winforms,solidworks,C#,Winforms,Solidworks,我正在和开发一个插件,使用Windows窗体显示有关某些文件的信息 在api文档中,建议使用此类将Pdm窗口设置为父窗口 public class WindowHandle : IWin32Window { private IntPtr mHwnd; public WindowHandle(int hWnd) { mHwnd = new IntPtr(hWnd); } public IntPtr Handle {
public class WindowHandle : IWin32Window
{
private IntPtr mHwnd;
public WindowHandle(int hWnd)
{
mHwnd = new IntPtr(hWnd);
}
public IntPtr Handle
{
get { return mHwnd; }
}
}
它工作得很好,所以,当我展示我的状态时,我正在做下一个
//i recived hWnd parameter from the api
WindowHandle myHandle = new WindowHandle(hWnd);
var weForm = new myForm();
weForm.StartPosition = FormStartPosition.CenterParent;
weForm.ShowDialog(myHandle);
此时,表单显示没有问题,Pdm窗口实际上是父窗口,但表单未居中
我在他的父级中找到了如何将表单居中的方法,但显然,当您使用从IWin32Window
继承的自定义类时,任何人都可以工作
如何使表单居中?如果自动居中功能不起作用,则您需要自己进行计算 数学基本上意味着对齐每个窗口的中心点
int left = parentBounds.Left + (parentBounds.Width / 2) - (childBounds.Width / 2);
int right = parentBounds.Top + (parentBounds.Height / 2) - (childBounds.Height / 2);
不是父母,而是主人。您能否从
weForm.Load
中的this.Owner
中获得有意义的值?另外,尝试调用(结果也很可靠,功能不是DPIAware)