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

Windows 10 如何激活其他应用程序的窗口,windows-10,delphi-xe2,httpserver,Windows 10,Delphi Xe2,Httpserver,我有一个HTTP服务器应用程序(作为桌面应用程序运行的服务应用程序),它运行另一个应用程序,稍后应该激活另一个应用程序的窗口。计算机X包含所有应用程序。当我从X计算机的Opera web浏览器运行时,我的WinActivate功能工作正常。从计算机Y运行时,窗口未激活。无调试输出返回错误(我得到FLastErrorY打印)。我甚至尝试以管理员的身份运行HTTP服务器。我还能试什么 函数WinActivate(const awinittle:string):布尔值; 变量 _窗口句柄:HWND;

我有一个HTTP服务器应用程序(作为桌面应用程序运行的服务应用程序),它运行另一个应用程序,稍后应该激活另一个应用程序的窗口。计算机X包含所有应用程序。当我从X计算机的Opera web浏览器运行时,我的
WinActivate
功能工作正常。从计算机Y运行时,窗口未激活。无调试输出返回错误(我得到FLastErrorY打印)。我甚至尝试以管理员的身份运行HTTP服务器。我还能试什么

函数WinActivate(const awinittle:string):布尔值;
变量
_窗口句柄:HWND;
_键盘状态:TKeyboardState;
开始
复位误差;
_WindowHandle:=FindWindow(nil,PWideChar(AWinTitle));
FLastError:=GetLastError;
SetCursorPos(10,12);
OutputDebugString(PWideChar('FLastError1:')+IntTostr(FLastError)+
“,”窗口句柄:“+格式('%.8X',[[窗口句柄])+”+DateTimeToStr(现在));
如果窗口句柄为0,则
开始
//ShowWindow(_WindowHandle,SW_最小化);
如果是二次曲线(_WindowHandle),则
开始
ShowWindow(_WindowHandle,SW_RESTORE);
复位误差;
结果:=二次曲线(_WindowHandle);
如果结果是这样的话
结果:=WinWaitActive(AWinTitle,1000);
结束
其他的
结果:=SetForegroundWindow(_WindowHandle);
OutputDebugString(PWideChar('FLastErrorX:')+IntTostr(FLastError)+
“,”窗口句柄:“+格式('%.8X',[[窗口句柄])+”+DateTimeToStr(现在));
如果没有结果的话
开始
FLastError:=GetLastError;
OutputDebugString(PWideChar('FLastError2:')+IntTostr(FLastError)+
“,”窗口句柄:“+格式('%.8X',[”窗口句柄]);
//应用程序可能会锁定焦点,因此,对其进行破解
GetKeyBoardState(_KeyboardState);
ShowWindow(_WindowHandle,SW_SHOWNORMAL);
系统参数sinfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,0,SPIF_UpdateInFile);
如果键盘状态[VK_菜单]为1,则
keybd_事件(VK_菜单,0,KEYEVENTF_扩展键或0,0);
复位误差;
如果未设置ForeGroundWindow(_WindowHandle),则
开始
FLastError:=GetLastError;
如果未设置ForeGroundWindow(_WindowHandle),则
开始
FLastError:=GetLastError;
OutputDebugString(PWideChar('FLastErrorY:')+IntTostr(FLastError)+
“,”窗口句柄:“+格式('%.8X',[[窗口句柄])+”+DateTimeToStr(现在));
结束;
结束;
keybd_事件(VK_菜单,0,KEYEVENTF_扩展键或KEYEVENTF_键向上,0);
OutputDebugString(PWideChar('FLastError3:')+IntTostr(FLastError)+
“,”窗口句柄:“+格式('%.8X',[[窗口句柄])+”+DateTimeToStr(现在));
结果:=FLastError=0;
结束;
如果没有结果的话
开始
设置窗口位置(窗口句柄、最上面的窗口、0、0、0、0、SWP NOSIZE或SWP NOACTIVE或SWP NOMOVE);
SetWindowPos(_WindowHandle、HWND_NOTOPMOST、0、0、0、SWP_NOSIZE或SWP_NOACTIVATE或SWP NOMOVE);
结束;
结束;
结果:=WinWaitActive(AWinTitle,1000);
OutputDebugString(PWideChar('Dabar'));
结束;

所以,我发现了问题所在。我知道自动热键有窗口激活功能。我已经下载了它的源代码和窗口激活代码是良好的书面和评论。因此,我使用自动热键构建了一个测试应用程序,但我的窗口没有被激活。我发现这是因为没有活动用户登录。

因此,我发现了问题所在。我知道自动热键有窗口激活功能。我已经下载了它的源代码和窗口激活代码是良好的书面和评论。因此,我使用自动热键构建了一个测试应用程序,但我的窗口没有被激活。我发现这是因为没有活动用户登录。

阅读文档。为了使一个窗口成为前景,必须满足许多条件。另外,
SetForegroundWindow()
keybd_event()
不使用
SetLastError()
报告错误,因此
FLastError3
中的
GetLastError()
值没有意义。您的错误检查是错误的。只有在上一次调用失败时才调用GetLastError,并且文档告诉您GetLastError返回有意义的内容。尝试在桌面应用程序中运行代码。此代码可能在会话0中运行。@DavidHeffernan哪个应用程序?HTTP服务器还是应该激活窗口的服务器?后面是桌面应用程序。请阅读文档。为了使一个窗口成为前景,必须满足许多条件。另外,
SetForegroundWindow()
keybd_event()
不使用
SetLastError()
报告错误,因此
FLastError3
中的
GetLastError()
值没有意义。您的错误检查是错误的。只有在上一次调用失败时才调用GetLastError,并且文档告诉您GetLastError返回有意义的内容。尝试在桌面应用程序中运行代码。此代码可能在会话0中运行。@DavidHeffernan哪个应用程序?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;