【问题标题】:Check if desktop window is in foreground using .bat使用 .bat 检查桌面窗口是否在前台
【发布时间】:2017-05-11 06:09:30
【问题描述】:

我正在尝试创建一个快捷方式,通过它我可以切换“显示桌面”,如果程序通过桌面“隐藏桌面图标”运行,那么这是我的 .bat 文件

set mypath=%cd%
cd C:\Program Files\oneClickHD\
start ShowDesktop.lnk
if "%mypath%"=="C:\Users\admin\Desktop" goto GO
:GO
start HideDesktopIcons.exe

这段代码并没有真正起作用,因为如果我使用它的快捷方式运行它,bat 的目录不会改变,所以我想到了 3 种方法但无法实现它们这些方法是:

1) 检测是否有任何窗口打开,如果是,则切换 showdesktop.lnk 或者如果当前前景窗口不是桌面切换

2) 检查桌面是否在前台,如果是则运行 hideIcons.exe

3) 而不是使用 showdesktop.lnk 来切换显示桌面,而是使用刚刚打开桌面的东西

找到解决方案我尝试使用 nircmd 通过它我找不到如何返回活动窗口的值或检查桌面是否处于活动状态,并且有一个推荐:nircmd win min alltopnodesktop 这是唯一一个似乎可以工作的,但它最小化了所有内容,我只能看到墙纸,它只是以奇怪的方式工作。 然后我也想到了使用 vbs 脚本,但发现最好将 .bat 用于与 windows 相关的东西。 然后我也尝试使用powershell,但它似乎不在我的电脑上。

【问题讨论】:

  • 我有点疑惑。您的批处理损害了几个规则: - 包含空格的路径应始终用双引号引起来 - start 期望引号中的第一个 arg 是窗口的标题 - 转到以下语句没有任何意义,因为流程会到达它很自然。 - 桌面没有窗口,但是任何窗口都显示在画布/背景上。 - windows+m(inimize) 的快捷方式需要发送消息所以你应该澄清你真正想要的是什么。
  • a path containing spaces should always be enclosed in double quotes - 通常是正确的,但在 cd 中不需要它们(但不会造成伤害。)start expects the first arg in quotes to be the title of the window - 是的,但没有 start-with-any 的实例-quotes-OP 帖子中的任何内容。
  • IIRC,桌面确实是作为一个窗口实现的。请参阅GetDesktopWindow
  • GetDesktopWindow 不返回资源管理器的桌面窗口。桌面窗口是所有其他窗口的父窗口。在 Windows 95 之前,这是唯一一个称为桌面的窗口。它显示最小化窗口的图标。在当前的 Windows 资源管理器中,会在桌面上放置一个显示器大小的窗口。它将最小化程序的图标移出真实桌面上的屏幕并将它们放在任务栏上。如果资源管理器停止,您将只能看到真实的桌面窗口,但图标仍然在 3000、3000 (blogs.msdn.microsoft.com/oldnewthing/20041028-00/?p=37453)。
  • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review

标签: windows powershell batch-file vbscript desktop


【解决方案1】:

不支持窗口的东西比支持最少窗口的东西要好。

你需要编写一个真正的程序。这会告诉您前台窗口的句柄、类名和窗口标题。

在此处查看我的答案,了解 VBScript 等 COM 语言的方法How to find the window Title of Active(foreground) window using Window Script Host

创建一个名为 GetForegroundWindow.bas 的文件。把这个放进去。

Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  
    Public Declare Function GetForegroundWindow Lib "user32"  As Integer
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Sub Main()
        Dim hWnd as Long, WT as String, CN as String, Length as Long
        On Error Resume Next
        hWnd=GetForegroundWindow()
        WT = Space(512)
        Length = GetWindowText(hwnd, WT, 508)
        WT = Left$(WT, Length)
        If WT = "" Then WT = Chr(171) & "No Window Text " & Err.LastDllError & Chr(187)
        CN = Space(512) 
        Length = GetClassName(hwnd, CN, 508)
        CN = Left$(CN, Length)      
        If CN = "" Then CN = "Error=" & Err.LastDllError
        Console.Out.WriteLine(hWnd & "," & WT & "," & CN)
'This shows how to toggle desktop, etc
'This will work as a vbscript if you remove "as object"

    Dim ShellApp as Object
    ShellApp = CreateObject("Shell.Application")
    ShellApp.MinimizeAll()
    ShellApp.UndoMinimizeAll()
    ShellApp.ToggleDesktop()

End Sub
End Module

把上面的文件放在桌面上。启动命令提示符并键入

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /platform:anycpu /sdkpath:C:\Windows\Microsoft.NET\Framework\v4.0.30319 /target:exe /out:"%userprofile%\desktop\GetForegroundWindow.exe" "%userprofile%\desktop\GetForegroundWindow.bas" /verbose

它给出这样的输出。

C:\Windows\system32>"C:\Users\User\Desktop\GetForegroundWindow.exe"
593444,Administrator: Command Prompt - "C:\Users\User\Desktop\GetForegroundWindow.exe",ConsoleWindowClass

您可以使用For /f 循环进行解析。见For /?

C:\Windows\system32>@for /f "Tokens=1-3 Delims=," %A in ('"C:\Users\User\Desktop\GetForegroundWindow.exe"') Do @Echo %C
ConsoleWindowClass

这些是 Shell 的窗口

Order   Level   WindowText  ClassName   HWnd    ParentHWnd  ProcessID   ParentProcessID ThreadID    ModuleNameHWin  EXENameProcess

21      «No Window Text 0»  Shell_TrayWnd   41227310    52039984    12336   5532    10668   «Not Available Error=126»   explorer.exe
22          Start   Start   43979912    41227310    12336   5532    10668   «Not Available Error=126»   explorer.exe
23          «No Window Text 0»  TrayDummySearchControl  73536804    41227310    12336   5532    10668   «Not Available Error=126»   explorer.exe
24              Search Windows  Button  56497500    73536804    12336   5532    10668   «Not Available Error=126»   explorer.exe
25              «No Window Text 0»  Edit    11736392    73536804    12336   5532    10668   «Not Available Error=126»   explorer.exe
26              «No Window Text 0»  ToolbarWindow32 23991502    73536804    12336   5532    10668   «Not Available Error=126»   explorer.exe
27          Task View   TrayButton  33362614    41227310    12336   5532    10668   «Not Available Error=126»   explorer.exe
28          «No Window Text 0»  TrayNotifyWnd   34476878    41227310    12336   5532    10668   «Not Available Error=126»   explorer.exe
29              10:06 AM    TrayClockWClass 24449812    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
30              «No Window Text 0»  TrayShowDesktopButtonWClass 40899346    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
31              Tray Input Indicator    TrayInputIndicatorWClass    46273942    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
32                  «No Window Text 0»  Button  28185460    46273942    12336   5532    10668   «Not Available Error=126»   explorer.exe
33                  «No Window Text 0»  Button  25170502    46273942    12336   5532    10668   «Not Available Error=126»   explorer.exe
34              «No Window Text 0»  SysPager    21238280    34476878    12336   5532    10668   C:\Users\David Candy\Desktop\Editor\EditorSdi\Ed.exe    explorer.exe
35                  User Promoted Notification Area ToolbarWindow32 25628972    21238280    12336   5532    10668   «Not Available Error=126»   explorer.exe
36              «No Window Text 0»  Button  21107412    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
37                  «No Window Text 0»  Button  32313836    21107412    12336   5532    10668   «Not Available Error=126»   explorer.exe
38              System Promoted Notification Area   ToolbarWindow32 16781596    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
39              Notification Center TrayButton  18157900    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
40              Touch keyboard  TIPBand 21697390    34476878    12336   5532    10668   «Not Available Error=126»   explorer.exe
41          «No Window Text 0»  ReBarWindow32   9375916 41227310    12336   5532    10668   «Not Available Error=126»   explorer.exe
42              Running applications    MSTaskSwWClass  24842334    9375916 12336   5532    10668   «Not Available Error=126»   explorer.exe
43                  Running applications    MSTaskListWClass    18485304    24842334    12336   5532    10668   «Not Available Error=126»   explorer.exe
44              Favorites   ToolbarWindow32 12193824    9375916 12336   5532    10668   «Not Available Error=126»   explorer.exe
45              Desktop ToolbarWindow32 25497672    9375916 12336   5532    10668   «Not Available Error=126»   explorer.exe

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    相关资源
    最近更新 更多