消息定制: 1) 建立消息号 #define MYMESSAGE WM_USER+106 2) 消息映射 ON_MESSAGE(MYMESSAGE, OnMyMessage) 3) 消息函数定义 afx_msg void OnMyMessage(WPARAM wParam,LPARAM lParam); 4) 实现消息函数 void CMainFrame::OnMyMessage(WPARAM wParam,LPARAM lParam) { CString prompt; prompt.Format("%s",lParam); AfxMessageBox(prompt); }
5) 发送消息 PostMessage异步的,需要外部定义,或者加上static. SendMessage同步的,等待消息处理完,可以在堆栈中定义. void CShhApp::OnAppAbout() { static char myParameter[]="my test"; CAboutDlg aboutDlg; aboutDlg.DoModal();
AfxGetMainWnd()->PostMessage(MYMESSAGE,0,(LPARAM)myParameter);//Asynchron // SendMessage(MYMESSAGE,1000,(LPARAM)myParameter);//Synchron }
LRESULT SendMessage( HWND hWnd, // handle of destination window 窗口句柄,例中为当前窗口,不需要此参数 UINT Msg, // message to send WPARAM wParam, // first message parameter 16位 LPARAM lParam // second message parameter 32位,一般为地址 );