如何使用AllowSetForegroundWindow?
我在不同的应用程序中有两个窗口。第一个应用程序有一个按钮,可使用其窗口句柄和进程 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;
当我启动第一个应用程序并按下按钮时,第二个应用程序启动。当我按下第二个应用程序上的按钮时,它显示ERROR
。
我在这里做错了什么?
I have two windows in separate applications. The first application has a button that starts the second application with its window handle and process id:
procedure TForm1.Button1Click(Sender: TObject);
begin
WinExec(PChar('Second.exe ' + IntToStr(Handle) + ' ' + IntToStr(GetCurrentProcessId)), SW_SHOWDEFAULT);
end;
The second application also has a button which should set the foreground window to the first application:
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;
The first application has a message handler that handles WM_APP + 1
like this:
procedure TForm1.WWAppPlusOne(var Msg: TMsg);
begin
Application.BringToFront;
end;
When I start the first application and press on the button, the second application starts. When I press the button on the second application it shows ERROR
.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
AllowSetForegroundWindow
声明不正确。您省略了调用约定。您使用的数据类型也是错误的,尽管目前这对您来说可能是良性的。它应该看起来像这样:
Your declaration of
AllowSetForegroundWindow
is incorrect. You have omitted the calling convention. The data types you have used are wrong too, although that's probably benign for you at the moment.It should look like this: