我在不同的应用程序中有两个窗口。第一个应用程序有一个按钮,可使用其窗口句柄和进程 ID 启动第二个应用程序:
procedure TForm1.Button1Click(Sender: TObject);
begin
WinExec(PChar('Second.exe ' + IntToStr(Handle) + ' ' + IntToStr(GetCurrentProcessId)), SW_SHOWDEFAULT);
end;
第二个应用程序还有一个按钮,应将前台窗口设置为第一个应用程序:
function AllowSetForegroundWindow(AHandle: HWND): Boolean; external 'user32.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
if not AllowSetForegroundWindow(StrToInt(ParamStr(2))) then begin
ShowMessage('ERROR');
Exit;
end;
SendMessage(StrToInt(ParamStr(1)), WM_APP + 1, 0, 0);
end;
第一个应用程序有一个消息处理程序,它处理 WM_APP + 1
,如下所示:
procedure TForm1.WWAppPlusOne(var Msg: TMsg);
begin
Application.BringToFront;
end;
当我启动第一个应用程序并按下按钮时,第二个应用程序启动。当我按下第二个应用程序上的按钮时,它显示错误
。
我在这里做错了什么?
最佳答案
您的 AllowSetForegroundWindow
声明不正确。您省略了调用约定。您使用的数据类型也是错误的,尽管目前这对您来说可能是良性的。
它应该看起来像这样:
function AllowSetForegroundWindow(dwProcessId: DWORD): BOOL;
stdcall; external 'user32.dll';
关于delphi - 如何使用AllowSetForegroundWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9399023/