这是如何发送消息,SendMessage是阻塞的需要等待处理完成以后返回,如果不等待可以使用postmessage
HWND MainHWND=FindWindowA("TMainForm","首页");
if(MainHWND != 0)
{
char ShutdownMessage[100]="Shutdown";
COPYDATASTRUCT cds;
cds.dwData = 0;//可以是任意值
cds.cbData = sizeof(ShutdownMessage) + 1;//指定lpData内存区域的字节数
cds.lpData = ShutdownMessage;//发送给目标窗口所在进程的数据
SendMessage(MainHWND, WM_COPYDATA, 0, (LPARAM)&cds);
}
这是如何接收消息,需要重载WndProc方法(也可以选择重载WindowProc、或者使用MESSAGE_MAP和ApplicationEvents控件,可参考https://www.cnblogs.com/findumars/p/5597286.html)
void __fastcall TMainForm::WndProc(Messages::TMessage& Message)//Tmainform是继承tform的一个类
{
switch (Message.Msg)
{
case WM_COPYDATA:
{
COPYDATASTRUCT* cds = new COPYDATASTRUCT();
cds=(COPYDATASTRUCT*)Message.LParam;
char strResult[100];
sprintf(strResult,"%s",(char*)cds->lpData);
MessageBox(NULL,strResult,"",0);
}
break;
default:
TForm::WndProc(Message);
}
}
private:
void __fastcall WndProc(Messages::TMessage& Message);