1. 程式人生 > >Windows訊息:如何自定義視窗訊息與執行緒訊息

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);