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

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

本帖最后由 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 也应该可行,如何实现?
谢谢。

回复 1# mokson


   powershell 先add-type 一个包含findwindow函数和getwindowrect函数和rect结构的类
然后调用findwindow找到hwnd 在new-object一个rect 让getwindowrect赋值 获取到窗口位置
你好

TOP

本帖最后由 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

评分人数

TOP

不知道WMI怎么实现,不过UI Automation可以
  1. Add-Type -AssemblyName UIAutomationClient
  2. gps "notepad"|%{$rect=([Windows.Automation.AutomationElement]::FromHandle($_.MainWindowHandle)).Current.BoundingRectangle;"窗口位置:$($rect.TopLeft,$rect.BottomRight)"}
复制代码
1

评分人数

TOP

运行成功了,感谢各位仁兄的帮助。

TOP

返回列表