基于MFC
用于存储数据的自定义结构体:
struct MSG_STRUCT { wchar_t msg[256];};
发送端,发送按钮响应事件的代码
void CSendWM_COPYDATADlg::OnBnClickedSendmessage(){ MSG_STRUCT smsg; GetDlgItemText(IDC_MESSAGE,smsg.msg,256); HWND hTargetWnd=NULL; hTargetWnd=::FindWindow(NULL,_T("ReceiveWM_COPYDATA COPYDATASTRUCT cds; cds.cbData=sizeof(MSG_STRUCT); cds.lpData=&smsg; if(hTargetWnd==NULL) return; ::SendMessage(hTargetWnd,WM_COPYDATA,reinterpret_cast<WPARAM>(m_hWnd), reinterpret_cast<LPARAM>(&cds));}
接收端,响应WM_COPYDATA的代码
BOOL CReceiveWM_COPYDATADlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct){ // TODO: Add your message handler code here and/or call default CString cs_str; CString cs_PrivStr; GetDlgItemText(IDC_RECEIVEMSG,cs_PrivStr); ++m_ReceiveNum; MSG_STRUCT smsg; if(pCopyDataStruct->cbData==sizeof(smsg)) { memcpy_s(&smsg,sizeof(smsg),pCopyDataStruct->lpData,pCopyDataStruct->cbData); } //MessageBox(smsg.msg); cs_str.Format(_T("消息%d:%s\r\n"),m_ReceiveNum,smsg.msg); cs_str=cs_PrivStr+cs_str; SetDlgItemText(IDC_RECEIVEMSG,cs_str); return CDialog::OnCopyData(pWnd, pCopyDataStruct);}
最终效果图:
发送端
接收端:
标签:pCopyDataStruct,WM,cds,smsg,MSG,C++,str,COPYDATA,cs From: https://blog.csdn.net/2401_87429224/article/details/143064117