Parsing MFC in depth--the relationship between handle and object In the Windows architecture, many objects are presented to developers in the form of handles. such as window handles (HWND), drawing Devices (HDC), and so on. Then most of the API functions are focused around these handles. Like Showwindow,setwindowtext,textout and so on. The first parameter of these API functions is usually the handle. But in the C + + system, this kind of access to the details of things is often contrary to its packaging spirit. So MFC does a lot of encapsulation classes to hide these details. Came into being is CWND,CDC and other classes. With these classes of exposure, you can manipulate the handle directly without caring about him.
However, our theme for today is not here. The following is the real content.
According to the C + + theory, the creation and destruction of the encapsulated handle should be done by the class itself, and the outside world is not aware of the details. But in MFC, is that really the case? At least CWnd is not so, HWND is not entirely under the control of CWnd. As I mentioned in the previous chapter, there are a number of global variables in MFC, one of which is a map table of HWND and CWnd. The location of this map table is not easy to find, the previous chapter mentioned the global object afx_module_state, he has a member of the type is afx_module_thread_state, in the interior of Afx_module_thread_state, There is a group of map tables, and M_pmaphwnd is one of them:
Chandlemap M_pmaphwnd;
chandlemap* M_pmaphmenu;
chandlemap* M_pmaphdc;
chandlemap* M_pmaphgdiobj;
chandlemap* m_pmaphimagelist;
Did you see it? It's a bit of a hassle to find, but MFC provides a global function Afxmaphwnd, which makes it easy to get the map table wherever it is called.
So, what does this watch do? In fact, it's very simple, although in MFC, objects are dealing with objects. But MFC also has to deal with Windows systems. Windows gives you only the handle, so how to find the corresponding class through these handles? You can easily solve this problem by using the map table.
For example, in Windows message mechanism, when WndProc receives a message, it will only get a target window of HWND hwnd, how to find the matching class? Just search from the M_pmaphwnd. By invoking CWnd's static member function fromhandlepermanent, we can easily find the CWnd class corresponding to the HWND from the map table.
The implementation of Fromhandlepermanent is also very simple. The M_pmaphwnd is first found through Afxmaphwnd, and then the CWnd pointer corresponding to the HWND is searched by M_pmaphwnd's member function lookuppermanent, and finally returned to him.
When was the M_pmaphwnd created? When the first window is created, the attach function of CWnd is called, and the following is the specific function implementation:
BOOL Cwnd::attach (HWND hwndnew)
{
if (hwndnew = = NULL) return FALSE;
chandlemap* PMap = Afxmaphwnd (TRUE); Create map if not exist
Pmap->setpermanent (m_hwnd = hwndnew, this);
return TRUE;
}
Note the passed-in argument to call Afxmaphwnd is true, which means that when the map does not exist, it is created. Let's look at the implementation of Afxmaphwnd:
chandlemap* PASCAL Afxmaphwnd (BOOL bcreate)
{
afx_module_thread_state* pState = Afxgetmodulethreadstate ();
if (Pstate->m_pmaphwnd = = NULL && bcreate)
{
Pstate->m_pmaphwnd = new Chandlemap (Runtime_class (CWnd),
Constructdestruct<cwnd>::construct, Constructdestruct<cwnd>::D estruct,
Offsetof (CWnd, m_hwnd));
}
Return pstate->m_pmaphwnd;
}
Afxmaphwnd first find this pointer from the place we said before, and if the pointer is empty and bcreate is true, a new chandlemap is created. It's very simple, isn't it?
Parsing MFC in depth--the relationship between handle and object