如何激活其他应用程序的window

How to activate window of other application

我有 HTTP 服务器应用程序(运行 作为桌面应用程序的服务应用程序),运行 的另一个应用程序,稍后应该激活另一个应用程序的 window。计算机 X 包含所有应用程序。当我从电脑Y运行http://192.168.16.21:225/command/TTaskType.ttTestTask from Opera web browser from computer X, my WinActivate function works. When I run http://192.168.16.21:225/command/TTaskType.ttTestTask时,window没有激活。 None 调试输出 returns 错误(我打印了 FLastErrorY)。我什至尝试以管理员身份 运行 HTTP 服务器。 我还能尝试什么

function WinActivate(const AWinTitle: string): boolean;
var
  _WindowHandle: HWND;
  _KeyboardState: TKeyboardState;
begin
  ResetError;
  _WindowHandle := FindWindow(nil, PWideChar(AWinTitle));
  FLastError := GetLastError;
  SetCursorPos(10, 12);
  OutputDebugString(PWideChar('FLastError1: ' + IntTostr(FLastError) +
    ', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
  if _WindowHandle <> 0 then
  begin
    //ShowWindow(_WindowHandle, SW_MINIMIZE);
    if IsIconic(_WindowHandle) then
    begin
      ShowWindow(_WindowHandle, SW_RESTORE);
      ResetError;
      Result := IsIconic(_WindowHandle);
      if Result then
        Result := WinWaitActive(AWinTitle, 1000);
    end
    else
      Result := SetForegroundWindow(_WindowHandle);

    OutputDebugString(PWideChar('FLastErrorX: ' + IntTostr(FLastError) +
      ', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
    if not Result then
    begin
      FLastError := GetLastError;
      OutputDebugString(PWideChar('FLastError2: ' + IntTostr(FLastError) +
        ', _WindowHandle: ' + Format('%.8X', [_WindowHandle])));

      // Applications might lock focus, so, hack it around
      GetKeyBoardState(_KeyboardState);

      ShowWindow(_WindowHandle, SW_SHOWNORMAL);
      SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_UPDATEINIFILE);

      if _KeyboardState[VK_MENU] <> 1 then
        keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);

      ResetError;
      if not SetForegroundWindow(_WindowHandle) then
      begin
        FLastError := GetLastError;
        if not SetForegroundWindow(_WindowHandle) then
  begin
    FLastError := GetLastError;
    OutputDebugString(PWideChar('FLastErrorY: ' + IntTostr(FLastError) +
      ', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
  end;
      end;

      keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);

      OutputDebugString(PWideChar('FLastError3: ' + IntTostr(FLastError) +
        ', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));

      Result := FLastError = 0;
    end;

    if not Result then
    begin
      SetWindowPos(_WindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
      SetWindowPos(_WindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
    end;
  end;

  Result := WinWaitActive(AWinTitle, 1000);
  OutputDebugString(PWideChar('Dabar'));
end;

所以,我发现了问题所在。我知道 AutoHotkey 具有 window 激活功能。我已经下载了它的源代码,window 激活码写得很好,评论也很好。所以我使用 AutoHotkey 构建了一个测试应用程序,但我的 window 没有被激活。我发现这是因为没有活跃用户登录。