先说明一下:
要将一个结构体从CTestDlg传到CDetailDlg并显示相关内容,其中结构体如下
struct SMSG
{
CString sName;
CString sIP;
CString sContent[20];
int iNum;//计数
};
我的做法:
先在TestDlg中定义SMSG msgs;并完善结构体的信息。然后按一个按钮,先生成一个非模态对话框DetailDlg,然后::PostMessage(hWindow,WM_SHOWMSG,0,(LPARAM)&msgs);//hWindow是DetailDlg的句柄,且经检测,此时msgs中的内容是完好无损的在DetailDlg.h中定义一个自定义消息,并添加处理函数
#define WM_SHOWMSG WM_USER+105
afx_msg void OnShowMsg(WPARAM wParam,LPARAM lParam);
ON_MESSAGE(WM_SHOWMSG,OnShowMsg)void CDetailDlg::OnShowMsg(WPARAM wParam,LPARAM lParam)
{
SMSG* p=(SMSG*)&lParam;
m_strName=p->sName;
m_strIP=p->sIP;
m_iNum=p->iNum;//m_strName、m_strIP、m_iNum是3个编辑框关联的变量
for(int i=0;i<m_iNum;i++)
{
m_str=p->sContent[i];
m_list.AddString(m_str);//将每条内容加到列表框
}
UpdateData(FALSE);
}结果在DetailDlg里显示的内容都是乱码怎么解决?
要将一个结构体从CTestDlg传到CDetailDlg并显示相关内容,其中结构体如下
struct SMSG
{
CString sName;
CString sIP;
CString sContent[20];
int iNum;//计数
};
我的做法:
先在TestDlg中定义SMSG msgs;并完善结构体的信息。然后按一个按钮,先生成一个非模态对话框DetailDlg,然后::PostMessage(hWindow,WM_SHOWMSG,0,(LPARAM)&msgs);//hWindow是DetailDlg的句柄,且经检测,此时msgs中的内容是完好无损的在DetailDlg.h中定义一个自定义消息,并添加处理函数
#define WM_SHOWMSG WM_USER+105
afx_msg void OnShowMsg(WPARAM wParam,LPARAM lParam);
ON_MESSAGE(WM_SHOWMSG,OnShowMsg)void CDetailDlg::OnShowMsg(WPARAM wParam,LPARAM lParam)
{
SMSG* p=(SMSG*)&lParam;
m_strName=p->sName;
m_strIP=p->sIP;
m_iNum=p->iNum;//m_strName、m_strIP、m_iNum是3个编辑框关联的变量
for(int i=0;i<m_iNum;i++)
{
m_str=p->sContent[i];
m_list.AddString(m_str);//将每条内容加到列表框
}
UpdateData(FALSE);
}结果在DetailDlg里显示的内容都是乱码怎么解决?
改成 SMSG* p=(SMSG*)lParam;
SMSG* msgs=new SMSG;
完善好信息后
::PostMessage(hWindow,WM_SHOWMSG,0,(LPARAM)msgs);//此时的msgs指向的内容也是完好无损的
但是结果还是乱码
{
m_str=p->sContent[i];//调试的时候,到这里就终止了,不能继续
m_list.AddString(m_str);//将每条内容加到列表框
}
If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters cannot include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used.
好消息好消息,问题解决了,解决办法:
在4楼的基础上,把::PostMessage改成::SendMessage就行了,因为我看到8楼大哥提供的信息里没有说SendMessage会怎么怎么样,哈哈我的思想被禁锢了,我总是认为SMSG* p=(SMSG*)&lParam;这里面是一定要有'&'的先吃饭,回来结帖