检查是当前活动窗口桌面或否 [英] Check is current active window Desktop or no

查看:225
本文介绍了检查是当前活动窗口桌面或否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试检查当前活动窗口是桌面做某事,我在下面的代码中写下一个定时器,但GetDektopWindow& GetForegroundWindow不是相同的值:

I try to check if current active window is Desktop do something , i wrote below code in a timer but the handle value returned by GetDektopWindow & GetForegroundWindow is not same value :

  if GetForegroundWindow = GetDesktopWindow then
    // Do something

这样做如何?

推荐答案

// not defined in D2007
function GetShellWindow: HWND; stdcall; external user32;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if GetForegroundWindow = GetShellWindow then
    [..]
end;

只有我使用的非explorer shell(sharpe),它失败了。

With the only non-explorer shell I use (sharpe) it fails though.

更新:

有时,桌面的窗口层次结构是不同的(请参阅Andreas的评论)。以下镜头是Windows 7的桌面图片旋转功能激活时的Spy ++。一些WorkerW窗口接管屏幕,它是在桌面上点击时被激活的窗口。由于 GetShellWindow 返回Progman的句柄,上述测试失败。







在这一点上看起来可能是合理的以测试前台窗口是否具有shell的默认视图窗口作为其直接子节点,但是我看到多个引用指示多个WorkerW窗口可能嵌套。所以我认为下面将是一个更有效的方法:

Sometimes the window hierarchy of the desktop is different (see Andreas' comments). The below shot is Spy++'s take when Windows 7's desktop picture rotation functionality is activated. Some 'WorkerW' window takes over the screen and it is the one that gets activated when clicked on the desktop. Since GetShellWindow returns the 'Progman's handle, the above test fails.



At this point it might seem reasonable to test if the foreground window has the shell's default view window as its immediate child, however I saw multiple references that indicate multiple 'WorkerW' windows might get nested. So I think the below would be a more fail-safe approach:

procedure TForm1.Timer1Timer(Sender: TObject);

  function HasDefViewChild(Wnd: HWND): Boolean;
  begin
    Result := Wnd <> 0;
    if Result then begin
      Result := FindWindowEx(Wnd, 0, 'SHELLDLL_DefView', nil) <> 0;
      if not Result then
        Result := HasDefViewChild(FindWindowEx(Wnd, 0, 'WorkerW', nil));
    end;
  end;

begin
  if HasDefViewChild(GetForegroundWindow) then
    [...]
end;

当前景窗口为Progman时,这将起作用,因为DefView是Progman儿童。 OTOH当WorkerW是活动窗口时,如果第一个孩子不是DefView,而另一个WorkerW则代码将重复。

This will work when the foreground window is 'Progman', because then the 'DefView' is 'Progman's child. OTOH when 'WorkerW' is the active window, the code will iterate if the first child is not 'DefView' and yet another 'WorkerW' instead.

这篇关于检查是当前活动窗口桌面或否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆