Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅具有parentWnd的父级中的中心窗体_C#_Winforms_Solidworks - Fatal编程技术网

C# 仅具有parentWnd的父级中的中心窗体

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 {

我正在和开发一个插件,使用Windows窗体显示有关某些文件的信息

在api文档中,建议使用此类将Pdm窗口设置为父窗口

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)