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

自定义消息

一、自定义窗口消息

  1. #define WM_MY_MSG WM_USER + 0x100
  2. 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()

二、自定义线程消息

  1. #define WM_MY_THRD_MSG WM_USER + 100
  2. 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);
展开阅读全文

没有更多推荐了,返回首页