c# - 作为非托管 HWND 子级的托管窗体

标签 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

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

最佳答案

可能是因为 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/

相关文章:

c# - ASP.NET Web API 中处理程序和筛选器的依赖注入(inject)

c# - 如何遍历多个数组?

c# - WPF 中的命令,实现问题

C# Winforms 如何在函数中更新 toolStrip

c# - SQLite 错误 : Failed to find or load the registered .Net Framework 数据提供程序

c# - Windows Azure 找不到 C++/CLI 项目的 DLL

c# - 当对象的值为 null 时,C# 能知道对象的类型吗?

c# - 合并两个(或更多)PDF

c# - 如何在 C# 中管理多个版本的非托管 DLL 调用的使用?

c++ - 如何找到此 MSDN 文档中引用的实际值?