从 CWnd::FromHandle 获得的 CWnd 的生命周期是多少?

发布于 2024-08-06 06:56:17 字数 258 浏览 10 评论 0原文

根据 msdn,当我得到 CWnd 时* 与 CWnd::FromHandle 一起,

指针可能是临时的,不应存储以供以后使用。

我不清楚“稍后使用”是什么意思。它只是当前方法的范围吗? 据我所知,Win32中没有GC!

According tomsdn, when I get a CWnd* with CWnd::FromHandle,

The pointer may be temporary and should not be stored for later use.

What is meant by "later use" is not clear to me. Is it only the scope of the current method?
As far as I know, there is no GC in Win32!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝天 2024-08-13 06:56:17

MFC维护了许多句柄映射,从HWND到CWnd,HDC到CDC等,它们存储在线程状态中。每个句柄映射都包含一个永久映射和临时映射 - 当您调用 CWnd::Create 或 CDC::Attach 等方法时,会添加永久条目,而当您在没有句柄的句柄上调用 FromHandle 时,会创建临时条目。永久入境。

临时条目在空闲处理期间(在 CWinApp::OnIdle 中)被清除,因此只能在处理当前消息时安全地使用它们。一旦您返回到消息循环,或进入另一个模态循环(例如通过调用DoModal),它们就可能被删除。

MFC maintains a number of handle maps, from HWND to CWnd, HDC to CDC etc, which are stored in the thread state. Each handle map contains a permanent map and temporary map - permanent entries are added when you call a method such as CWnd::Create or CDC::Attach, while temporary entries are created when you call FromHandle on a handle that doesn't have a permanent entry.

Temporary entries are cleaned up during idle processing (in CWinApp::OnIdle), so they can only safely be used while processing the current message. As soon as you return to the message loop, or enter another modal loop (e.g. by calling DoModal) then they may be deleted.

甜`诱少女 2024-08-13 06:56:17

基于相同的 MSDN 描述,我假设这意味着如果没有 CWnd 附加到作为对象提供的 hWnd,它将创建一个临时 CWnd,一旦超出范围或调用其他地方的析构函数,该临时 CWnd 可能会被销毁,或者为相关 hWnd 显式创建 CWnd。因此,如果您已经创建了 CWnd,那么应该没问题,否则您可能需要非常小心地存储收到的指针。

Based on the same MSDN description, I would assume that this means that if no CWnd is attached to the hWnd provided as an object, it will create a temporary CWnd which probably gets destroyed once something goes out of scope, or a destructor elsewhere is called, or a CWnd is explicitly created for the hWnd in question. So if you already have a CWnd created, you should be OK, otherwise you will probably need to be very careful with storing the pointer you receive.

拥抱我好吗 2024-08-13 06:56:17

通常他们只希望您在函数范围内使用此句柄。不要将其存储为类字段,在对象的整个生命周期中引用它。

Typically they only want you to use this handle with in the scope of your function. And not to store it as a class field where you reference it through out the life of your object.

昔日梦未散 2024-08-13 06:56:17

FromHandle 基本上用于获取对已经存在的窗口对象的瞬时引用。 MFC 将这些引用存储在称为临时句柄映射的内部结构中(句柄映射是 Windows HWND 到 MFC CWnd 对象的映射,MFC 使用该映射进行 Win32 调用来操作 MFC 对象对应的实际 Windows 窗口)。为了避免此结构中的对象数量增长超出所有范围,在 MFC 的空闲循环处理期间从句柄映射中删除项目。

正如您可能已经猜到的,还有一个永久句柄映射不会有这种自动清理行为。如果您需要获取不将其 HWND 引用放入临时句柄映射中的 CWnd 对象,您可以调用 FromHandlePermanent()。

-罗恩

FromHandle is basically used for getting a transient reference to an already existing window object. MFC stores these references in an internal structure called a temporary handle map (a handle map is a map of Windows HWNDs to MFC CWnd objects used by MFC to make Win32 calls to manipulate the actual Windows window the MFC object corresponds to). In order to avoid the number of objects in this structure from growing beyond all bounds, items are deleted from the handle map during MFC's idle loop processing.

As you may have guessed, there is also a permanent handle map that won't have this automatic clean up behavior. If you need to get a CWnd object that doesn't put its HWND reference in the temporary handle map you can call FromHandlePermanent().

-Ron

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文