WM_COPYDATA的使用

接收数据的一方

ON_WM_COPYDATA()

afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);

代码片段:

BOOL CClientDemoDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) { if (pCopyDataStruct != NULL) { LPCTSTR pszText = (LPCTSTR)(pCopyDataStruct->lpData); DWORD dwLength = (DWORD)(pCopyDataStruct->cbData); TCHAR szRecvText[1024] = {0}; memcpy(szRecvText, pszText, dwLength); MessageBox(szRecvText, _T("Test"), MB_OK); } return CDialog::OnCopyData(pWnd, pCopyDataStruct); }

 

发送数据的一方

代码片段:

CString strWindowTitle = _T("ClientDemo"); CString strDataToSend = _T("Hello, Everyone fsdfsdfsdfdsfds,this is a test demo!"); HWND hOtherWnd = ::FindWindow(NULL, strWindowTitle.GetBuffer(0)); if (hOtherWnd != NULL && ::IsWindow(hOtherWnd)) { COPYDATASTRUCT cpd; cpd.dwData = 0; cpd.cbData = strDataToSend.GetLength() * sizeof(TCHAR); cpd.lpData = (void*)strDataToSend.GetBuffer(0); HRESULT hResult = ::SendMessage(hOtherWnd, WM_COPYDATA, (WPARAM)(AfxGetApp()->m_pMainWnd), (LPARAM)&cpd); strDataToSend.ReleaseBuffer(); }

 

OK了,就这么简单

你可能感兴趣的:(windows开发)