delphi - 如何使用AllowSetForegroundWindow?

标签 delphi winapi window

我在不同的应用程序中有两个窗口。第一个应用程序有一个按钮,可使用其窗口句柄和进程 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/

相关文章:

android - 如何在 Android(Maybe Context)的外部类中调用 getCurrentFocus() 而不是 Activity

delphi - Delphi TClientDataSet排序(插入)问题

delphi - tcpserver x tcpclient 在运行压力测试时出现问题

c++ - 如何将 SECURITY_ATTRIBUTES 与 CreateProcess() 一起使用?

c - 如何在 Windows 7 上记录网络带宽?

java - 在属性文件中写入属性而不破坏文件中的窗口路径

Delphi MapiSendMail 在 Windows 7 64 位上崩溃

德尔福。如何在调用另一个模态表单后立即关闭模态表单

windows - 获取进程桌面

c++ - 最大化的 WS_POPUP 窗口位于任务栏前面