同一个进程中
SendMessage(Handle,WM_SETTEXT,(WPARAM)NULL,(LPARAM)”Just a test”);
const int WM_TEST=WM_USER+10;
void __fastcall TForm1::Button2Click(TObject* Sender)
{
SendMessage(this->Handle,WM_TEST,(WPARAM)0,(LPARAM)”Justa a test”);
}
void __fastcall TForm1::WndProc(TMessage& Message)
{
case WM_TEST :
AnsiString Str=AnsiString((char*)Message.LParam);
Memo1->Lines->Add(Str);//没任何问题
break;
default:
TForm::WndPProc(Message);
break;
}
不同进程:
要在不同的进程传递数据只能使用消息WM_COPYDATA,
无法使用你自定义的消息.下面是使用WM_COPYDATA消息的代码,测试过没问题.
//发送 :
COPYDATASTRUCT cds;
AnsiString Data=Edit2->Text;//”Just a test”;
cds.dwData=sizeof(COPYDATASTRUCT);
cds.cbData=Data.Length();
cds.lpData=(LPVOID)Data.c_str();
//想目标窗口发消息WM_COPYDATA
SendMessage((HWND)Edit1->Text.ToInt(),WM_COPYDATA,(WPARAM)NULL,(LPARAM)&cds);
//接收:
if(Message.Msg==WM_COPYDATA)
{
COPYDATASTRUCT *cds;
char* DataBuf;
cds=(COPYDATASTRUCT*)Message.LParam;
DataBuf=new char[cds->cbData];
CopyMemory(DataBuf,cds->lpData,cds->cbData);
AnsiString Str=AnsiString(DataBuf);
Memo1->Lines->Add(“Str=”+Str);
delete[] DataBuf;
}