用 powershell 实现的两个版本(其核心代码是搬运c#),一版是把ps代码全部注入 for/f 的命令表达式中,二版是用 bat+ps 混编方法。代码中的 'notepad' 字段是‘记事本’程序名,可由楼主自定义...
以下代码存为 test-1.bat 运行- @echo off & for /f "delims=" %%v in (
- '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)" '
- ) do echo,%%v
- pause&exit/b
复制代码
以下代码存为 test-2.bat 运行- <# ::
- @echo off & for /f "delims=" %%v in ('powershell "iex(${%~f0}|out-string)" ') do echo,%%v&pause&exit/b
- #>
- 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);
- '窗口位置:x1,y1='+$r.x1+','+$r.y1+'; x2,y2='+$r.x2+','+$r.y2;
复制代码