如何激活其他应用程序的窗口

时间:2017-11-18 15:58:10

标签: windows-10 delphi-xe2 httpserver

我有运行其他应用程序的HTTP服务器应用程序(作为桌面应用程序运行的服务应用程序),之后应该激活另一个应用程序的窗口。计算机X包含所有应用程序当我从计算机X的Opera网络浏览器中运行 http://192.168.16.21:225/command/TTaskType.ttTestTask 时,我的WinActivate功能正常运行。当我从计算机Y运行 http://192.168.16.21:225/command/TTaskType.ttTestTask 时,窗口未激活。无调试输出返回错误(我打印出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;

1 个答案:

答案 0 :(得分:0)

所以,我发现什么是错的。我知道AutoHotkey有窗口激活功能。我已经下载了它的源代码,并且窗口激活代码编写得很好并且已经过评论。所以我使用AutoHotkey构建了一个测试应用程序,我的窗口没有被激活。我发现这是因为没有活跃的用户登录。