2

我需要将我的 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 错误 http://www.freeimagehosting.net/uploads/f29bc27823.png

再次调用它,一切正常。这是怎么回事?!?

4

1 回答 1

2

可能是因为 rh_wnd 为空?在至少 2 种情况下,您将从 MainWindow() 返回 null。检查可能是个好主意

IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow();
if ( rh_wnd != null )
   DocEditor.Show(rh_wnd);

如果上述方法停止了错误,您可能需要检查上述哪个条件返回 null,然后从那里开始。

希望这可以帮助。

于 2011-03-09T01:24:34.043 回答