想通过sendmessage来传送一个消息,它的参数为字符串,怎么办?

解决方案 »

  1.   

    可以用全局的字符串变量,也可以传递指针(LPARAM和WPARAM)
      

  2.   

    WM_COPYDATA:
        CString strWindowTitle = _T("Window Name");
        CString strDataToSend  = _T("This is a message to send");
        
    LRESULT copyDataResult;
    CWnd *pOtherWnd = CWnd::FindWindow(NULL, strWindowTitle); if (pOtherWnd)
    {
    COPYDATASTRUCT cpd;
    cpd.dwData = 0;
    cpd.cbData = strDataToSend.GetLength();
    cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData);
    copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,
                                                    (WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),
                                                    (LPARAM)&cpd);
    strDataToSend.ReleaseBuffer();
    // copyDataResult has value returned by other app


    else 
    {
    AfxMessageBox("Unable to find other app.");
    }
    }The other app should handle the WM_COPYDATA message in the following manner 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);
    }
      

  3.   

    CString strSource = "source";
    WPARAM wParam = (WPARAM)strSource.GetBuffer(strSource.GetLength());
    strSource.ReleaseBuffer();CString strFileName = "file name";
    LPARAM lParam=(LPARAM)strFileName.GetBuffer(strFileName.GetLength());
    strFileName.ReleaseBuffer();
    SendMessage(WM_SHOWFRM_DELETEITEM, wParam, lParam);
      

  4.   

    同意楼上,WM_COPYDATA是在多个进程间通讯用的。