Implementation of pretranslatemessage in MFC

Source: Internet
Author: User

(I accidentally deleted this article, and now I will flip it out and re-post it)

In MFC, pretranslatemessage is a very important virtual function. The role of this function is not discussed here. It is involved in many places. Here we will only talk about its implementation mechanism.
When talking about the implementation of pretranslatemessage, we have to talk about the implementation of the message loop of MFC. MFC uses the pumpmessage function in the cwinapp class to Implement Message loops. However, the actual message loop code is located in cwinthread, and cwinapp only inherits from cwinthread. The simplified code is as follows:

Bool cwinthread: pumpmessage ()
...{
_ Afx_thread_state * pstate = afxgetthreadstate ();

: Getmessage (& (pstate-> m_msgcur), null ))
 
If (! Afxpretranslatemessage (& (pstate-> m_msgcur )))
...{
: Translatemessage (& (pstate-> m_msgcur ));
: Dispatchmessage (& (pstate-> m_msgcur ));
}
Return true;
}

We can see that pumpmessage will call afxpretranslatemessage before the actual translatemessage and dispatchmessage occur, and afxpretranslatemessage will call cwnd: descripretranslatetree (although other functions will be called, this is the most critical ), the Code is as follows:

Bool Pascal cwnd: FIG (hwnd hwndstop, MSG * PMSG)
...{
Assert (hwndstop = NULL |: iswindow (hwndstop ));
Assert (PMSG! = NULL );

// Walk from the target window up to the hwndstop window checking
// If any window wants to translate this message

For (hwnd = PMSG-> hwnd; hwnd! = NULL; hwnd =: getparent (hwnd ))
...{
Cwnd * pwnd = cwnd: fromhandlepermanent (hwnd );
If (pwnd! = NULL)
...{
// Target window is a C ++ window
If (pwnd-> pretranslatemessage (PMSG ))
Return true; // trapped by target window (eg: accelerators)
}

// Got to hwndstop window without interest
If (hwnd = hwndstop)
Break;
}
Return false; // no special processing
}

As you can see, the code is still very direct. Traverse the received message window layer by layer and call pretranslatemessage to check whether the return value is true. If yes, the process ends. Otherwise, the process continues.
Here is a key: cwnd * pwnd = cwnd: fromhandlepermanent (hwnd) the code obtains the permanent handle table from the current afxmodulethreadstate to find the cwnd object corresponding to hwnd. A common problem with pretranslatemessage is related to this: if you write an mfc dll and call the modeless dialog in this mfc dll from another MFC main project, the pretranslatemessage of modeless dialog will not be called. Because the mfc dll and the MFC project have different afxmodulethreadstate, the modeless cdialog object created in the mfc dll is not in the handle table of the MFC project (cwnd: fromhandlepermanent returns NULL ), therefore, although the pretranslatemessage of the cwinapp In the MFC main project will be called (note that the message loop of the dialog is in the MFC main project ), however, the modeless cdialog pretranslatemessage function created in the mfc dll is not called. Special processing is required. There are two methods:
One is to directly call the cwinapp: pretranslatemessage of the mfc dll in the MFC main project to call the cwinapp: pretranslatemessage (this can be done by export a special function in the mfc dll ). The other method is to use a hook. In the hook message processing function, you can determine whether the target window is the current window with focus. If yes, you can call the pretranslatemessage function of the target window directly (provided that you have a pointer to save this object ).

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.