线程中无法弹出对话框,如何传递消息?
1.在BEGIN_MESSAGE_MAP 和END_MESSAGE_MAP之间添加消息处理函数比如:ON_MESSAGE(WM_PostMessage, OnPostMessage)
2.定义WM_PostMessage 比如:#define WM_PostMessage (WM_USER + 333)
3.添加处理函数
LRESULT OnPostMessage(WPARAM wParam, LPARAM lParam);
LRESULT CYourClassName::OnPostMessage(WPARAM wParam, LPARAM lParam)
{
CString csWPARAM=(CString)((BSTR)wParam);
CString csLPARAM=(CString)((BSTR)lParam);
SysFreeString((BSTR)wParam);
SysFreeString((BSTR)lParam);
//AfxMessageBox(csWPARAM);
//AfxMessageBox(csLPARAM);
//if(csWPARAM.CompareNoCase(_T("ZHPC自定义传递消息1"))==0)
//{
// AfxMessageBox(csLPARAM);
// }
// else if(csWPARAM.CompareNoCase(_T("ZHPC自定义传递消息2"))==0)
//{
// AfxMessageBox(csLPARAM);
// }
// .....
return true;
}
4.发送传递消息
// CString csWPARAM=_T("ZHPC连接服务器成功");
// CString csLPARAM=_T("ZHPC收到登录请求,收到公钥 随机数");
// PostMessage(WM_PostMessage, (WPARAM)csWPARAM.AllocSysString(), (LPARAM)csLPARAM.AllocSysString());