找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 27231|回复: 6

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

[复制链接]
发表于 2019-2-23 18:34:07 | 显示全部楼层 |阅读模式
本帖最后由 hentai87 于 2019-3-5 12:22 编辑

搜了好久,一个相关的命令都找不到
发表于 2019-2-23 20:48:00 | 显示全部楼层
本帖最后由 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技术 +1 收起 理由
hentai87 + 1 谢谢

查看全部评分

 楼主| 发表于 2019-2-24 11:58:40 | 显示全部楼层
本帖最后由 hentai87 于 2019-2-24 12:46 编辑

回复 2# ivor
似乎只支持非管理员权限运行的程序
 楼主| 发表于 2019-2-24 12:11:05 | 显示全部楼层
本帖最后由 hentai87 于 2019-2-24 12:46 编辑

回复 2# ivor
怎么对管理员程序声效,权限运行也没用
发表于 2019-2-24 12:49:14 | 显示全部楼层
回复 4# hentai87


    非也非也!只是因为管理员启动的cmd,窗口标题是:管理员:  命令提示符
 楼主| 发表于 2019-2-24 12:57:08 | 显示全部楼层
本帖最后由 hentai87 于 2019-3-5 12:23 编辑

回复 5# ivor


CMD测试通过。
发表于 2019-3-4 12:12:55 | 显示全部楼层
本帖最后由 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技术 +1 收起 理由
hentai87 + 1 大佬

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-19 01:18 , Processed in 0.019403 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表