从CWnd :: FromHandle获得的CWnd的生存期是多长? [英] What is the lifetime of a CWnd obtained from CWnd::FromHandle?

查看:262
本文介绍了从CWnd :: FromHandle获得的CWnd的生存期是多长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 msdn ,使用CWnd :: FromHandle获取CWnd *,

According to msdn, when I get a CWnd* with CWnd::FromHandle,

指针可能是临时的,不应存储以备后用.

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

我不清楚以后使用"是什么意思.仅仅是当前方法的范围吗? 据我所知,Win32中没有GC!

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!

推荐答案

MFC维护了许多句柄映射,从HWND到CWnd,从HDC到CDC等,它们以线程状态存储.每个句柄映射都包含一个永久映射和一个临时映射-当您调用诸如CWnd :: Create或CDC :: Attach之类的方法时,将添加永久条目,而当您在不具有该属性的句柄上调用FromHandle时,将创建永久条目.永久入境.

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.

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

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.

这篇关于从CWnd :: FromHandle获得的CWnd的生存期是多长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆