如何获取同一应用程序生成的所有 windows 消息?
How to get all windows message generated from the same application?
比如我现在有一个MFC应用程序。用户可以单击用户名文本字段,然后输入用户名。然后用户可以单击密码文本字段,然后输入密码。然后用户可以单击登录按钮。
在MFC应用中,VS 2013会为我生成相应的回调函数,像这样:
afx_msg void OnBnClickedOk();
afx_msg void OnBnClickedCancel();
现在我期望:
当用户单击 "Left button" 时,我可以将 "left button click" 打印到日志文件中,当用户单击 "cancel button" 时,我可以将 "cancel button click" 打印到日志文件中。
我知道我可以在每个回调函数中做到这一点。
喜欢:
void LoginDialog::OnBnClickedOk()
{
printToLog("Left Button click");
}
但问题是,这个应用非常庞大,至少有几百个回调函数。我不能把这个 "printToLog" 函数放在每个回调函数中。
因此,是否有一个函数可以接收所有这些生成的 windows 消息?如果有这样的函数,我可以在那个回调函数中添加我的 printToLog 函数。
我上网查了一下,https://msdn.microsoft.com/en-us/library/windows/desktop/ms632593(v=vs.85).aspx
但是这个link里的东西不是我需要的。我不能过多修改现有代码。
您需要超载 window class 的 PreTranslateMessage()
。它是 CWnd
.
的虚函数
步骤:
- 在rc文件
中右击你的window
- 点击
Class Wizard
- 在弹出的对话框中,切换到
Virtual Functions
标签,搜索PreTranslateMessage
- Select函数,点击对话框右侧的
Add Function
按钮
- 在添加的功能中处理消息
看起来像:
BOOL CEventFilterDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CDialogEx::PreTranslateMessage(pMsg);
}
比如我现在有一个MFC应用程序。用户可以单击用户名文本字段,然后输入用户名。然后用户可以单击密码文本字段,然后输入密码。然后用户可以单击登录按钮。 在MFC应用中,VS 2013会为我生成相应的回调函数,像这样:
afx_msg void OnBnClickedOk();
afx_msg void OnBnClickedCancel();
现在我期望: 当用户单击 "Left button" 时,我可以将 "left button click" 打印到日志文件中,当用户单击 "cancel button" 时,我可以将 "cancel button click" 打印到日志文件中。 我知道我可以在每个回调函数中做到这一点。 喜欢:
void LoginDialog::OnBnClickedOk()
{
printToLog("Left Button click");
}
但问题是,这个应用非常庞大,至少有几百个回调函数。我不能把这个 "printToLog" 函数放在每个回调函数中。 因此,是否有一个函数可以接收所有这些生成的 windows 消息?如果有这样的函数,我可以在那个回调函数中添加我的 printToLog 函数。 我上网查了一下,https://msdn.microsoft.com/en-us/library/windows/desktop/ms632593(v=vs.85).aspx 但是这个link里的东西不是我需要的。我不能过多修改现有代码。
您需要超载 window class 的 PreTranslateMessage()
。它是 CWnd
.
步骤:
- 在rc文件 中右击你的window
- 点击
Class Wizard
- 在弹出的对话框中,切换到
Virtual Functions
标签,搜索PreTranslateMessage
- Select函数,点击对话框右侧的
Add Function
按钮 - 在添加的功能中处理消息
看起来像:
BOOL CEventFilterDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CDialogEx::PreTranslateMessage(pMsg);
}