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

[系统相关] bat如何用wmic获取程序窗口位置的坐标信息?

[复制链接]
发表于 2024-9-22 10:47:46 | 显示全部楼层 |阅读模式
本帖最后由 mokson 于 2024-9-22 13:54 编辑

  1. @echo off
  2. echo 打开记事本
  3. start notepad.exe

  4. echo 用 wimc 命令查询记事本窗口的信息(左边距、右边距、宽度、高度)
  5. wmic path win32_window where "name=notepad.exe" get /value
复制代码
这是百度AI回复的代码,运行后没有任何结果。
查询不到相关的窗体信息,应该是 wmic 语法错误,如何修正?
还有,用 powershell 也应该可行,如何实现?
谢谢。
发表于 2024-9-22 15:05:24 | 显示全部楼层
回复 1# mokson


   powershell 先add-type 一个包含findwindow函数和getwindowrect函数和rect结构的类
然后调用findwindow找到hwnd 在new-object一个rect 让getwindowrect赋值 获取到窗口位置
发表于 2024-9-22 16:13:58 | 显示全部楼层
本帖最后由 aloha20200628 于 2024-9-22 21:06 编辑

回复 1# mokson

用 powershell 实现的两个版本(其核心代码是搬运c#),一版是把ps代码全部注入 for/f 的命令表达式中,二版是用 bat+ps 混编方法。代码中的 'notepad' 字段是‘记事本’程序名,可由楼主自定义...

以下代码存为 test-1.bat 运行

  1. @echo off & for /f "delims=" %%v in (
  2.         'powershell "Add-Type 'using System;using System.Runtime.InteropServices;public struct RC {public int x1;public int y1;public int x2;public int y2;};public class wc {[DllImport("""user32.dll""")] public static extern bool GetWindowRect(IntPtr h,ref RC r);}';$h=(Get-Process 'notepad')[0].MainWindowHandle;$r=New-Object RC;[void][wc]::GetWindowRect($h,[ref]$r);echo('窗口位置:x1,y1='+$r.x1+','+$r.y1+'; x2,y2='+$r.x2+','+$r.y2)" '
  3. ) do echo,%%v
  4. pause&exit/b
复制代码
以下代码存为 test-2.bat 运行

  1. <# ::
  2. @echo off & for /f "delims=" %%v in ('powershell "iex(${%~f0}|out-string)" ') do echo,%%v&pause&exit/b
  3. #>
  4. Add-type 'using System; using System.Runtime.InteropServices;
  5. public struct RC { public int x1; public int y1; public int x2; public int y2; };
  6. public class wc { [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h,ref RC r); }'
  7. $h=(Get-Process 'notepad')[0].MainWindowHandle;
  8. $r=New-Object RC;
  9. [void][wc]::GetWindowRect($h,[ref]$r);
  10. '窗口位置:x1,y1='+$r.x1+','+$r.y1+'; x2,y2='+$r.x2+','+$r.y2;
复制代码

评分

参与人数 2技术 +2 收起 理由
77七 + 1 感谢分享
czjt1234 + 1 厉害

查看全部评分

发表于 2024-9-22 20:38:47 | 显示全部楼层
不知道WMI怎么实现,不过UI Automation可以
  1. Add-Type -AssemblyName UIAutomationClient
  2. gps "notepad"|%{$rect=([Windows.Automation.AutomationElement]::FromHandle($_.MainWindowHandle)).Current.BoundingRectangle;"窗口位置:$($rect.TopLeft,$rect.BottomRight)"}
复制代码

评分

参与人数 1技术 +1 收起 理由
czjt1234 + 1 不明觉厉

查看全部评分

 楼主| 发表于 2024-9-25 10:11:38 | 显示全部楼层
运行成功了,感谢各位仁兄的帮助。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-17 18:21 , Processed in 0.009946 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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