如何用SendMessage()函数传递字符串
-----------------------------
一、两个参数都带字符串的形式:
void CAaaDlg::OnButton1()
{
char* p1 = "Willis";
char* p2 = "VCHelp";
SendMessage(WM_CURRENTINFO, (WPARAM)p1, (LPARAM)p2 );
}
LRESULT CAaaDlg::OnCurrentInfo(WPARAM wParam, LPARAM lParam )
{
MessageBox((LPSTR)wParam,(LPSTR)lParam,0);//消息函数里接收字符串
return 0;
}
--------
二、只带一个字符串的形式:
CString strPara = _T("fggg");
::SendMessage(hWnd,WM_XXXX,0,(LPARAM)(LPCTSTR)strPara);
OnMessageXXXX(WPARAM wParam,LPARAM lParam)
{
CString strddd = (LPCTSTR)lParam; //在消息处理函数里接收字符串
}
how to use wm_copydata to pass string
使用WM_COPYDATA:
CString str = _T( "Hello" );
HWND hWndReceived = FindWindow( "Receiver", NULL );
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = strDataToSend.GetLength();
cpd.pData = (void*)str.GetBuffer(cpd.cbData);
SendMessage( hWndReceived, WM_COPYDATA, 0, (LPARAM) & cpd );
str.ReleaseBuffer();
加入映射宏:
BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
//{{AFX_MSG_MAP(CMyWnd)
...
ON_WM_COPYDATA()
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
...
映射函数:
BOOL CMyWnd::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData);
return CMyWnd::OnCopyData(pWnd, pCopyDataStruct);
}
如何用SendMessage()函数传递字符串
最新推荐文章于 2024-05-11 14:11:08 发布