积极答复者
mfc中postmessage和sendmessage

问题
-
我写个mfc程序,用对话框的edit box接受输入文字,然后点击idok,jl自定义消息到CMyView类处理函数,然后drawtext,用send没问题,用post点idok后程序崩溃
修改CTextDoc::OnGetText
将原来的代码:
pView->SendMessage(WM_CHANGE_TEXT, 0, (LPARAM)&dlg.m_szText);
修改为:
pView->PostMessage(WM_CHANGE_TEXT, 0, (LPARAM)&dlg.m_szText);void CTextDoc::OnGetText()
{
// TODO: Add your command handler code here
CTextDlg dlg;
if(dlg.DoModal() == IDOK)
{
POSITION pos = GetFirstViewPosition();
CView *pView = GetNextView(pos);
ASSERT_VALID(pView);
/////////////////////////////////////////////////////
pView->SendMessage(WM_CHANGE_TEXT, 0, (LPARAM)&dlg.m_szText);
/////////////////////////////////////////////////////
UpdateAllViews(NULL);
}
}
答案
-
是,如果你要传地址的话,传一个生存期比较久一点的变量的地址。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Kira Qian 2010年6月15日 5:42
全部回复
-
-
是,如果你要传地址的话,传一个生存期比较久一点的变量的地址。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Kira Qian 2010年6月15日 5:42
-
如果我这样改,然后在OnChangeText(wparam,lparam)中delete (CTextDlg*) lparam,会有什么问题
(实验后发现OnChangeText未能响应,消息跑到哪去了?)void CHellosDoc::OnGetText()
{
// TODO: Add your command handler code here
CTextDlg *pdlg=new CTextDlg;
if(pdlg->DoModal() == IDOK)
{
::PostMessage(pdlg->GetSafeHwnd(),WM_CHANGE_TEXT, 0, (LPARAM)pdlg);
UpdateAllViews(NULL);
}
} -
不知道你传一个对话框的地址做什么,这个对话框又没数据。PostMessage到这个对话框没用,对话框还没有窗口句柄,收不到信息的。看你的需求似乎一个非模态对话框就可以了,不用线程?
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP