我需要将我的 System.Windows.Forms.Form 显示为非托管 C++ HWND 的子窗口。这是检索 NativeWindow 的 C# SDK 代码:
public static NativeWindow MainWindow()
{
Diagnostics.Process process = Diagnostics.Process.GetCurrentProcess();
if (null == process)
return null;
IntPtr handle = process.MainWindowHandle;
if (IntPtr.Zero == handle)
return null;
NativeWindow wnd = new NativeWindow();
wnd.AssignHandle(handle);
return wnd;
}
在插件中是这样实现的:
IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow();
DocEditor.Show(rh_wnd);
这行得通……大多数时候。但是我第一次调用这段代码时它也经常失败:
HWND Error http://www.freeimagehosting.net/uploads/f29bc27823.png
再次调用,一切正常。 怎么回事?!?
最佳答案
可能是因为 rh_wnd 为空?至少有 2 种情况您会从 MainWindow() 返回 null。检查一下可能是个好主意
IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow();
if ( rh_wnd != null )
DocEditor.Show(rh_wnd);
如果上述停止了错误,您可能需要检查以上哪些条件返回 null,然后从那里开始。
希望这对您有所帮助。
关于c# - 作为非托管 HWND 子级的托管窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2733927/