How can I get the current active window at the time a batch script is run?

前端 未结 2 1475
难免孤独
难免孤独 2020-12-06 19:59

I have a batch script I want to run with hotkeys, and this script is supposed to make some actions in the active window (for example, creating a particular set of folders, o

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 20:24

    You can lookup which process got the window in foreground using pinvoke of user32.dll. I've used this trick for system.window.forms.sendkeys method in a script:

    Add-Type @"
      using System;
      using System.Runtime.InteropServices;
      public class Tricks {
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
    }
    "@
    
    $a = [tricks]::GetForegroundWindow()
    
    get-process | ? { $_.mainwindowhandle -eq $a } # in my case:
    
    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
    -------  ------    -----      ----- -----   ------     -- -----------
    
        161       7    13984      15820    91     9,75   7720 Console
    

提交回复
热议问题