作为非托管HWND的子项的托管表单

时间:2010-04-28 23:58:24

标签: c# winforms unmanaged hwnd

我需要将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

再次调用它,一切正常。 发生了什么事???

1 个答案:

答案 0 :(得分:2)

可能因为rh_wnd为空?至少有两种情况你会从MainWindow()返回null。检查

可能是个好主意
IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow();
if ( rh_wnd != null )
   DocEditor.Show(rh_wnd);

如果以上内容阻止了错误,您可能需要检查上述哪个条件返回null,并从那里开始。

希望这有帮助。