How to get all windows message generated from the same application?

710 views Asked by At

For example, I have a MFC application now. Users can click the user name text field, and then input username. Then user can click password text field, then input password. Then user can click login button. In MFC application, VS 2013 will generated the corresponding callback functions for me, like this:

afx_msg void OnBnClickedOk();
afx_msg void OnBnClickedCancel();

Now I expect: When user click "Left button", I can print "left button click" to a log file, when user click "cancel button", I can print "cancel button click" to a log file. I know I can do that in each callback function. like:

void LoginDialog::OnBnClickedOk()
{
    printToLog("Left Button click");
}

But the problem is, this application is very large, it has at least hundreds of callback functions. I cannot put this "printToLog" function inside each callback function. Therefore, is there a function which can receive all of these generated windows messages ? If there is a function like this, I can just add my printToLog function in that one callback function. I searched online, https://msdn.microsoft.com/en-us/library/windows/desktop/ms632593(v=vs.85).aspx But the stuff in this link is not what I need. I cannot revise the existing code too much.

1

There are 1 answers

5
zhm On BEST ANSWER

You need to overload PreTranslateMessage() of your window class. It's a virtual function of CWnd.

Steps:

  1. Right click your window in rc file
  2. Click Class Wizard
  3. In popup dialog, switch to Virtual Functions tab, and search for PreTranslateMessage
  4. Select the function, and click Add Function button at the right side of the dialog
  5. Process messages in the added function

It looks like:

BOOL CEventFilterDlg::PreTranslateMessage(MSG* pMsg)
{
    // TODO: Add your specialized code here and/or call the base class

    return CDialogEx::PreTranslateMessage(pMsg);
}