[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统相关] [已解决]批处理如何检测程序是否在前台运行?

本帖最后由 hentai87 于 2019-3-5 12:22 编辑

搜了好久,一个相关的命令都找不到

本帖最后由 ivor 于 2019-3-3 10:15 编辑

powershell
  1. #检测指定cmd窗口是否为活动窗口,修改("ConsoleWindowClass","命令提示符") 检测指定程序
  2. Add-Type @"
  3.   using System.Runtime.InteropServices;
  4.   public class WinApi {
  5.      [DllImport("user32.dll")]
  6.      public static extern int FindWindow(string lpClassName, string lpWindowName);
  7.      [DllImport("user32.dll")]
  8.      public static extern int GetForegroundWindow();
  9.   }
  10. "@
  11. while($true){
  12.     if([WinApi]::FindWindow("ConsoleWindowClass","命令提示符") -ne [WinApi]::GetForegroundWindow()){
  13.         #code here
  14.         Write-Host cmd is not font window.
  15.     }else{
  16.         #code here
  17.         Write-Host cmd is font window.
  18.     }
  19.     sleep 1
  20. }
复制代码
1

评分人数

TOP

本帖最后由 hentai87 于 2019-2-24 12:46 编辑

回复 2# ivor
似乎只支持非管理员权限运行的程序

TOP

本帖最后由 hentai87 于 2019-2-24 12:46 编辑

回复 2# ivor
怎么对管理员程序声效,权限运行也没用

TOP

回复 4# hentai87


    非也非也!只是因为管理员启动的cmd,窗口标题是:管理员:  命令提示符

TOP

本帖最后由 hentai87 于 2019-3-5 12:23 编辑

回复 5# ivor


CMD测试通过。

TOP

本帖最后由 ivor 于 2019-3-4 13:10 编辑

回复 6# hentai87
  1. #检测指定cmd窗口是否为活动窗口,修改("ConsoleWindowClass","命令提示符") 检测指定程序
  2. Add-Type @"
  3.   using System;
  4.   using System.Threading;
  5.   using System.Runtime.InteropServices;
  6.   public class WinApi {
  7.      [DllImport("user32.dll")]
  8.      public static extern int FindWindow(string lpClassName, string lpWindowName);
  9.      [DllImport("user32.dll")]
  10.      public static extern int GetForegroundWindow();
  11.      public static Thread t = new Thread(CountDown);
  12.      
  13.      public static int time = 120;
  14.      public static void CountDown()
  15.         {
  16.             RefreshTitle();
  17.             Environment.Exit(0);
  18.         }
  19.         public static void RefreshTitle(){
  20.             while(time != 0){
  21.                 Thread.Sleep(1000);
  22.                  Console.Title = "关闭倒计时:" + (time -= 1);
  23.             }
  24.         }
  25.   }
  26. "@
  27. #启动后台线程关闭程序倒计时  2分钟
  28. [WinApi]::t.Start()
  29. #关闭线程关闭程序倒计时
  30. #[WinApi]::t.Stop()
  31. while($true){
  32.     if([WinApi]::FindWindow("ConsoleWindowClass","管理员: 命令提示符") -ne [WinApi]::GetForegroundWindow()){
  33.         #code here
  34.         Write-Host cmd is not font window.
  35.     }else{
  36.         #code here
  37.         Write-Host cmd is font window.
  38.     }
  39.     sleep 1
  40. }
复制代码
1

评分人数

TOP

返回列表