BEGIN_MESSAGE_MAP 把訊息和函數指標聯絡起來
1首先要在視窗類別的cpp檔案手動添加訊息響應機制,
ON_MESSAGE(ID,Handler)
LRESULT Handler(WPARAM,LPARAM);//響應的函數
BEGIN_MESSAGE_MAP(CShellDlg, CDialog)<br />//{{AFX_MSG_MAP(CShellDlg)<br />ON_MESSAGE(WM_SENDOFFLINE, OnSendOffline) //自訂訊息<br />ON_MESSAGE(WM_SWIPECARD, OnSwipeCard) //自訂訊息<br />ON_MESSAGE(WM_MODEM, OnModem)<br />ON_MESSAGE(WM_PINPAD, OnPinPad)<br />ON_MESSAGE(WM_TRANSACTION, OnTransaction)<br />ON_MESSAGE(WM_THREAD_ERROR, OnThreadError)<br />ON_MESSAGE(WM_SENDRECALL, OnSendRecall)<br />ON_MESSAGE(WM_CHECKTXN, OnOnCheckTxn) //自訂訊息<br />ON_WM_TIMER()<br />ON_WM_KEYDOWN()<br />ON_WM_DESTROY()<br />//}}AFX_MSG_MAP<br />END_MESSAGE_MAP()
2 其次在視窗類別h檔案添加如下代碼;
// Generated message map functions<br />//{{AFX_MSG(CShellDlg)<br />virtual BOOL OnInitDialog();<br />afx_msg void OnTimer(UINT nIDEvent);<br />afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);<br />afx_msg void OnDestroy();<br />afx_msg LONG OnSwipeCard(WPARAM wParam,LPARAM lParam);<br />afx_msg LONG OnModem(WPARAM wParam, LPARAM lParam);<br />afx_msg LONG OnPinPad(WPARAM wParam, LPARAM lParam);<br />afx_msg LONG OnTransaction(WPARAM wParam, LPARAM lParam);<br />afx_msg LONG OnThreadError(WPARAM wParam, LPARAM lParam);<br />afx_msg LONG OnSendOffline(WPARAM wParam, LPARAM lParam);<br />afx_msg LONG OnSendRecall(WPARAM wParam, LPARAM lParam);<br />afx_msg LONG OnOnCheckTxn(WPARAM wParam, LPARAM lParam);<br />//}}AFX_MSG<br />DECLARE_MESSAGE_MAP()
3 實現響應函數,
LONG XXXDlg::OnOnCheckTxn(WPARAM wParam, LPARAM lParam)<br />{<br />dwCurTranType =0;<br /> .....<br /> //這部分添加自己響應代碼<br />return 0;<br />}<br />
4 最後在自己需要的地方調用自己定義的訊息(訊息響應)
MyMsg(pDlg->m_hWnd, WM_CHECKTXN, (WPARAM)TXN_DOWNTYPE, 0);
//訊息轉寄<br />BOOL MyMsg(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)<br />{<br />BOOL bRet = FALSE;<br />bRet = PostMessage(hWnd, uMsg, wParam, lParam);<br />return bRet;<br />}
這樣就全部OK了,等有時間把測試代碼在整理出來