C++中添加自定义消息的响应 (BEGIN_MESSAGE_MAP ON_MESSAGE)
1首先要在窗口类的cpp文件手动添加消息响应机制,
ON_MESSAGE(ID,Handler)
LRESULT Handler(WPARAM,LPARAM);//响应的函数
- BEGIN_MESSAGE_MAP(CShellDlg, CDialog)
- //{{AFX_MSG_MAP(CShellDlg)
- ON_MESSAGE(WM_SENDOFFLINE, OnSendOffline) //自定义消息
- ON_MESSAGE(WM_SWIPECARD, OnSwipeCard) //自定义消息
- ON_MESSAGE(WM_MODEM, OnModem)
- ON_MESSAGE(WM_PINPAD, OnPinPad)
- ON_MESSAGE(WM_TRANSACTION, OnTransaction)
- ON_MESSAGE(WM_THREAD_ERROR, OnThreadError)
- ON_MESSAGE(WM_SENDRECALL, OnSendRecall)
- ON_MESSAGE(WM_CHECKTXN, OnOnCheckTxn) //自定义消息
- ON_WM_TIMER()
- ON_WM_KEYDOWN()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
2 其次在窗口类h文件添加如下代码;
- // Generated message map functions
- //{{AFX_MSG(CShellDlg)
- virtual BOOL OnInitDialog();
- afx_msg void OnTimer(UINT nIDEvent);
- afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
- afx_msg void OnDestroy();
- afx_msg LONG OnSwipeCard(WPARAM wParam,LPARAM lParam);
- afx_msg LONG OnModem(WPARAM wParam, LPARAM lParam);
- afx_msg LONG OnPinPad(WPARAM wParam, LPARAM lParam);
- afx_msg LONG OnTransaction(WPARAM wParam, LPARAM lParam);
- afx_msg LONG OnThreadError(WPARAM wParam, LPARAM lParam);
- afx_msg LONG OnSendOffline(WPARAM wParam, LPARAM lParam);
- afx_msg LONG OnSendRecall(WPARAM wParam, LPARAM lParam);
- afx_msg LONG OnOnCheckTxn(WPARAM wParam, LPARAM lParam);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
3 实现响应函数,
- LONG XXXDlg::OnOnCheckTxn(WPARAM wParam, LPARAM lParam)
- {
- dwCurTranType =0;
- .....
- //这部分添加自己响应代码
- return 0;
- }
4 最后在自己需要的地方调用自己定义的消息(消息响应)
MyMsg(pDlg->m_hWnd, WM_CHECKTXN, (WPARAM)TXN_DOWNTYPE, 0);
- //消息转发
- BOOL MyMsg(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- BOOL bRet = FALSE;
- bRet = PostMessage(hWnd, uMsg, wParam, lParam);
- return bRet;
- }
这样就全部OK了,等有时间把测试代码在整理出来