Windows objects, handles, and MFC objects

Source: Internet
Author: User

WINDOWSMFC Programming C

Directory (?) [-]

    1. Windows object handles with MFC objects
      1. Windows objects
      2. Handle
      3. MFC objects
      4. Zatan

Windows objects, handles, and MFC objects(2009-04-28 22:11:34) reproduced
Tags: windows object handles MFC object Talk Category: MFC and Windows programming
Windows objects are identified by a handle, and the corresponding MFC class is the C + + wrapper for those handles. In-memory Windows objects must have a unique handle to identify, but there is not necessarily a corresponding MFC class object in memory.   When you need to get the corresponding MFC class object for a Windows object and there is no such object in memory, the system creates a temporary MFC class object that is returned to the user and is recycled at a later idle time. The Windows object handle and its corresponding MFC class are shown in the following table:

HWND

CWnd and derived classes

HDC

CDC and derived classes

HMENU

CMenu

Hpen,hbrush,hfont,hbitmap,hpalette,HRGN

CGdiObject

Himagelist

CImageList

SOCKET

CSocket


If you have any of the Windows object handles above, you can call the corresponding class's static member function FromHandle to find the corresponding MFC object (the system maintains a mapping from a Windows object handle to an MFC object for each thread).   A persistent map and a temporary map); If you have the MFC object above, you can also get the corresponding Windows object handle through the public member variable of the MFC class. For example, given a handle to an HWND type, HWND, you can use the

Cwnd::fromhandle (HWND);

To obtain a pointer to the CWnd object. If the HWND does not have a corresponding CWnd object, the system produces a temporary CWnd object associated with the HWND and returns a pointer to the object.   After obtaining the CWnd object, you can get a handle to the Window object through the public member m_hwnd of CWnd. If a temporary MFC object is generated when calling FromHandle, the mapping between the handle and the MFC object is saved in the system's temporary map. By default, Cwinthread::onidle automatically calls the DELETETEMPMAP function for MFC classes that support temporary handle mappings.   In the DeleteTempMap function, these temporary objects are canceled associated with the handle and then destroyed. If you have a Windows object handle, you can create a corresponding MFC object and then associate the MFC object with the Windows object handle.   At this point, the MFC object and the Windows object are mapped to each other. For example, for the following code:

CWnd Mywnd;

Mywnd.attach (HWND);

A mapping of HWND to MYWND will be established. Thereafter, you call Cwnd::fromhandle (HWND) to return a pointer to the Mywnd object. If the Mywnd object is destroyed, its destructor will automatically destroy the Windows object referred to by the HWND by calling DestroyWindow. If the behavior is not expected, you need to call the Detach member function to dismiss the association (mapping), such as Mywnd.detach (), before the Mywnd is destroyed.all temporary MFC objects and persistent (permanent) MFC objects are maintained and managed on a thread-by-unit basis. That is, one thread is not able to access the MFC wrapper class object of another thread, whether it is temporary or persistent. In order to pass these Windows objects between different threads, you should always pass through the handle type. Passing MFC wrapper objects from one thread to another can cause unpredictable results. Because MFC wrapper class objects are managed in threads, multiple MFC objects may correspond to the same handle in different threads in the program.

Question: If there are multiple MFC objects in the same thread attach the same handle, which MFC object will be returned by calling FromHandle on the handle? No behavior defined?

Windows objects, handles, and MFC objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.