如何将非托管对话框设置为 WinForm 表单的所有者?
我需要能够获取 WinForm 对话框的所有者的 HWND。在非托管中,我有一个后台线程,用于获取前面窗口的 HWND。然后,代码调用 ::GetParent(frontHWND) 来查看是否需要隐藏不同的非模式 MFC 对话框。当 WinForm 对话框是 frontHWND 时,我总是为 GetParent 调用返回 NULL。我也尝试过 GetOwner,意识到 .Net 试图消除 Parent 和 Owner 之间的差异。查看带有 Spy++ 的 WinForm 对话框,它还表示 WinForm 没有父级或所有者。我传入的
NativeWindow ^natWin = gcnew NativeWindow();
natWin->AssignHandle(IntPtr(hwndParent));
managedDlg->ShowDialog(natWin);
上面的代码没有设置WinForm的所有者。我尝试从 OnFormShown() 中的 WinForm 代码调用 Win32 SetParent,但锁定了 MFC 应用程序和 WinForm。
有人可以解释如何让我的非托管对话框/应用程序成为托管 winform 的所有者/父级吗?
I need to be able to get a WinForm dialog's Owner's HWND. In unmanaged I have a background thread that gets the HWND for the window in front. The code then calls ::GetParent(frontHWND) to see if it needs to hide a different non-modal MFC dialog. When the WinForm dialog is the frontHWND, I always get NULL back for the GetParent call. I have also tried GetOwner realizing .Net tried to cleanup the difference between Parent and Owner. Looking at the WinForm dialog w/ Spy++, it also say the WinForm has no parent or owner. I have passed in
NativeWindow ^natWin = gcnew NativeWindow();
natWin->AssignHandle(IntPtr(hwndParent));
managedDlg->ShowDialog(natWin);
The above code didn't set the owner of the WinForm. I tried calling the Win32 SetParent from the WinForm code in OnFormShown(), but the locked up the MFC application and the WinForm.
Can someone explain how to get my unmanaged dialog/app to be the owner/parent of the managed winform?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了显示具有 C++ 父级的 C# 表单,我这样做:
将此代码放入 C++/CLI 包装器 DLL 中。
希望这有帮助。
编辑:“w”必须针对nullptr进行测试,因为Control::FromHandle可能会失败。参见这里:
为什么 Control.FromHandle(IntPtr) 返回null 在一个挂钩进程中并返回“Form”的有效对象?在另一个挂钩进程中?
因此,故障安全代码将是:
To show a C# form with a C++ parent I do this:
this code is put in a C++/CLI wrapper DLL.
Hope this helps.
Edit: "w" must be tested against nullptr, because Control::FromHandle could fail. See here:
Why Control.FromHandle(IntPtr) returns null in one hooked process and returns valid object of "Form"? in another hooked process?
So, fail-safe code would be: