WPF 互操作 - HwndHost 不会自动销毁托管窗口
我无法确保正确销毁托管窗口。
我有一个 HwndHost 派生类,正在 TabControl 中显示(尽管这可能不相关)。我试图在选项卡关闭时销毁托管内容(而不是在包含的窗口关闭时)。
我目前拥有 myControlHost.Dispose()
效果的代码,这确保了 HwndHost. DestroyWindowCore 立即被调用。问题是,DestroyWindowCore 实际上并没有销毁托管的 HWND 内容!
我本以为这足以确保底层 CWnd 派生的应用程序接收到 WM_CLOSE 或其他内容,但这似乎不会发生 - Spy++ 仅报告已注册的正在发送消息“HwndSubclass.DetachMessage”。
我读到您不应该在 DestroyWindowCore
中显式向托管窗口发送 WM_CLOSE
,因为这应该自动发生。
手动删除 HwndHost
派生控件时,确保正确销毁托管窗口的正确方法是什么?
I am having trouble ensuring a hosted window is correctly destroyed.
I have a HwndHost
-derived class that I am displaying in a TabControl (though that is probably irrelevant). I am trying to destroy the hosted content when the tab closes (not when the containing window closes.)
I currently have code to the effect of myControlHost.Dispose()
, which ensures that HwndHost.DestroyWindowCore
is called immediately. The problem is, DestroyWindowCore does not actually destroy the hosted HWND content!
I would have thought that this was enough to ensure that the underlying CWnd
-derived application receives a WM_CLOSE
or something, but this does not seem to happen - Spy++ reports only a registered message "HwndSubclass.DetachMessage" being sent.
I have read that you are not supposed to explicitly send your hosted window a WM_CLOSE
in the DestroyWindowCore
, as this is supposed to happen automatically.
What is the correct way to ensure a hosted window is correctly destroyed when manually removing a HwndHost
-derived control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据这个MSDN文档,他们在DestroyWindowCore中调用DestroyWindow():
http://msdn.microsoft.com/en-us/library/ms752055.aspx
DestroyWindow() 会将 WM_CLOSE 消息发布到消息队列中,因此实际上您不需要或不应该直接发送/发布 WM_CLOSE。
在我的应用程序中,实际上我在子 DLL 中调用 DestroyWindow(),该子 DLL 在 DestroyWindowCore 回调中从 C# 端调用。然后,一切工作正常。
According to this MSDN document, they are calling DestroyWindow() in DestroyWindowCore:
http://msdn.microsoft.com/en-us/library/ms752055.aspx
DestroyWindow() will post WM_CLOSE message into message queue, so actually you don't need or should not directly send/post WM_CLOSE.
In my application, actually I am calling DestroyWindow() in a sub DLL which is called from C# side in DestroyWindowCore callback. Then, everything is working fine.