MFC中 windows object 和 C++ object

The Problem

Windows objects are normally represented by HANDLEs. The MFC classes wrap Windows object handles with C++ objects. The handle wrapping functions of the MFC class library provide a way to find the C++ object that is wrapping the Windows object with a particular handle. There are times when a Windows object does not have a C++ wrapper object, however, and at these times a temporary object is created to act as the C++ wrapper.

mfc 映射的 windows object ---->c++ wrapper
以下使用 mfc 的函数 如:fromhandle, getdlgitem,都会返回temporary 和 pemanent  c++ wrapper object
 注意:
     零时的 对象 会被在空闲时(OnIdle()函数)被删除,不能存下在下次消息处理中 使用
The default OnIdle processing in CWinThread automatically calls DeleteTempMap for each class that supports temporary handle maps

The Windows objects that use handle maps are:

  • HWND (CWnd and CWnd-derived classes)
  • HDC (CDC and CDC-derived classes)
  • HMENU (CMenu)
  • HPEN (CGdiObject)
  • HBRUSH (CGdiObject)
  • HFONT (CGdiObject)
  • HBITMAP (CGdiObject)
  • HPALETTE (CGdiObject)
  • HRGN (CGdiObject)
  • HIMAGELIST (CImageList)
  • SOCKET (CSocket)

///

Given a handle to any of these objects, you can find the MFC object that wraps the handle by calling the static member function FromHandle. For example, given an HWND called hWnd:

CWnd::FromHandle(hWnd)

will return a pointer to the CWnd that wraps the hWnd. If that hWnd does not have a specific wrapper object, then a temporary CWnd is created to wrap the hWnd. This makes it possible to get a valid C++ object from any handle.

Once you have a wrapper object, you can get to its handle through a public member variable. In the case of a CWnd, m_hWnd contains the HWND for that object.

Attaching Handles to MFC Objects

Given a newly created handle-wrapper object and a handle to a Windows object, you can associate the two by calling Attach. For example:

CWnd myWnd;
myWnd.Attach(hWnd);  mywnd 析构时会调用 destroywindow ,连同 hwnd 一起销毁

This makes an entry in the permanent map associating myWnd and hWnd.
Calling CWnd::FromHandle(hWnd) will now return a pointer to myWnd.
When myWnd is deleted, the destructor will automatically destroy the hWnd by calling the Windows DestroyWindow function. If this is not desired, the hWnd must be detached from myWnd before the myWnd object is destroyed (normally when leaving the scope at which myWnd was defined). The Detach member function does this.

myWnd.Detach();

More About Temporary Objects

Temporary objects are created whenever FromHandle is given a handle that does not already have a wrapper object. These temporary objects are detached from their handle and deleted by the DeleteTempMap functions. The default OnIdle processing in CWinThread automatically calls DeleteTempMap for each class that supports temporary handle maps. This means that you cannot assume a pointer to a temporary object will be valid past the point of exit from the function where the pointer was obtained, as the temporary object will be deleted during the Windows message-loop idle time.

很重要:
在多线程中传递 c++ wrapper object 是无效的(无论是 temporary 还是 permanent)
只能传递 windows handle, 换句话就是说, 线程 只能 访问 自己创建的c++ wrapper object

Wrapper Objects and Multiple Threads

Both temporary and permanent objects are maintained on a per-thread basis. That is, one thread cannot access another threads C++ wrapper objects, regardless of whether it is temporary or permanent. As stated above, temporary objects are deleted when the thread which that temporary object belongs enters OnIdle.

To pass these objects from one thread to another, always send them as their native HANDLE type. Passing a C++ wrapper object from one thread to another will often result in unexpected results.

MFC中 windows object 和 C++ object相关推荐

  1. MFC中Windows窗口消息循环及多线程之间关系

       Windows中一个进程可以包含多个线程,由多个线程组成.在Windows应用程序中,窗体是由"UI线程(User Interface Thread)"的特殊类型的线程创建的 ...

  2. 在WPF中,如何得到任何Object对象的XAML代码?

    原文:在WPF中,如何得到任何Object对象的XAML代码? 在WPF中,可以使用System.Windows.Markup.XamlWriter.Save(objName)得到任何Object对象 ...

  3. SAP中的BOPF(Business Object Processing Framework)

    https://www.cnblogs.com/DicksonJYL/p/9945986.html 目录 BOPF代表什么? 谁在使用BOPF? 怎样才能使用BOPF? 应用基础设施的主要组件有哪些? ...

  4. Object o = new Object()在内存中占几个字节

    CAS: Compare and Swap,即比较再交换. jdk5增加了并发包java.util.concurrent.*,其下面的类使用CAS算法实现了区别于synchronouse同步锁的一种乐 ...

  5. Java中泛型 Class<T>、T与Class<?>、 Object类和Class类、 object.getClass() 和 Object.class

    From:Java中泛型 Class<T>.T 与 Class<?>. Object类 和 Class类. object.getClass() 和 Object.class : ...

  6. java Arrarlist中的add(int index,Object ojb)

    java Arrarlist中的add(int index,Object ojb) 怎么自己用代码实现这个功能啊? 求讲解 askdjklas3 | 浏览 11337 次  2012-02-29 19 ...

  7. 金庸群侠传 Windows版:用 Object Pascal 和 SDL 实现的 DOS 游戏《金庸群侠传》的重制版

    金庸群侠传 Windows版:用 Object Pascal 和 SDL 实现的 DOS 游戏<金庸群侠传>的重制版. 原 DOS 下面的经典游戏<金庸群侠传>pascal 复 ...

  8. flutter中dynamic、var和Object的区别

    dynamic dynamic d = "CSDN"; 在运行时由系统根据该变量的 赋值 自动 推断该变量的数据类型 动态数据类型,在运行时可以改变数据类型: dynamic d ...

  9. JS中的toString、Object.toString、Object.prototype.toString

    不同类型值的toString方法: //Number const num = 123; num.toString(); // '123' (123).toString(); // '123' 整数必须 ...

最新文章

  1. android AIDL IPC深入分析
  2. 程序员无休止加班的真正原因
  3. virtualbox 安装ubuntu 时,看不到继续、退出按钮?共享文件无权限?
  4. 工作总结20190121
  5. GaussianBlur函数
  6. 服务器集群技术(备份服务器方案和均摊工作方案)(用来解决服务器挂掉问题)...
  7. 初等数论--原根--怎么判断a是不是模m的原根
  8. cocos2dx java 调用lua_Cocos2d-x Lua实现从Android回调到Lua的方法
  9. HTTP管线化(HTTP pipelining)
  10. Flask爱家租房--房屋管理(获取主页幻灯片展示的房屋基本信息)
  11. 荣耀Magic3相机界面公布:提供专业“电影”功能
  12. 如何从手机上做风控,设备指纹如何下手?
  13. Learning to Segment Object Candidates
  14. Cesium:鼠标监听事件绑定
  15. 驱动程序和应用程序的区别_复仇者黑客组织—教你写第一个Linux设备驱动程序...
  16. 【9933】单词的划分
  17. 缺少链接库报错:ld: symbol(s) not found for architecture x86_64
  18. 支付宝小程序框架分析
  19. Windows任务管理器被procexp(Process Explorer)取代后如何恢复
  20. 史上最全 IT 类学习资源

热门文章

  1. Word2Vec学习笔记(四)——Negative Sampling 模型
  2. 势在人为:人才吸引力报告2020
  3. java移位运算 cpu gpu_关于java操作中的移位运算
  4. 【Java】ASCII类对ASCII码的处理
  5. Linux异步IO实现方案总结
  6. 文件上传至将File转换成MultiPartFile
  7. mysql使用sql语句查询数据库所有表注释已经表字段注释
  8. lombok在IntelliJ IDEA下的使用
  9. Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种...
  10. Python 调试方法