【问题标题】:Managed form as child of unmanaged HWND托管表单作为非托管 HWND 的子代
【发布时间】:2010-04-28 23:58:23
【问题描述】:

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

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

【问题讨论】:

    标签: c# winforms unmanaged hwnd


    【解决方案1】:

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

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

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 2012-07-20
      相关资源
      最近更新 更多