Windows消息:如何自定义窗口消息与线程消息

2025-04-08 07:51:04
推荐回答(1个)
回答1:

自定义消息
一、自定义窗口消息
#define WM_MY_MSG WM_USER + 0x100
afx_msg LRESULT OnMyMsg(WPARAM, LPARAM);
LRESULT CTestDlg::OnMyMsg(WPARAM wParam, LPARAM lParam)
{...}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
...
ON_MESSAGE(WM_MY_MSG, &CTestDlg::OnMyMsg)
END_MESSAGE_MAP()
二、自定义线程消息
#define WM_MY_THRD_MSG WM_USER + 100
afx_msg void OnMyThrdMsg(WPARAM, LPARAM);
void CTestApp::OnMyThrdMsg(WPARAM wParam, LPARAM lParam)
{...}
BEGIN_MESSAGE_MAP(CTestApp, CWinAppEx)
...
ON_THREAD_MESSAGE(WM_MY_THRD_MSG, &CTestApp::OnMyThrdMsg)
END_MESSAGE_MAP()
那怎样在当前线程中触发消息呢?有以下两种方法:
::PostMessage(NULL, WM_MY_THRD_MSG, 0, 0);
::PostThreadMessage(::GetCurrentThreadId(), WM_MY_THRD_MSG, 0, 0);