【问题标题】:Check window position in batch批量检查窗口位置
【发布时间】:2022-01-17 03:41:19
【问题描述】:

在批处理脚本中,如何在不使用第三方软件(如Cmdow)的情况下获取窗口从头到尾的 X 和 Y

【问题讨论】:

标签: batch-file


【解决方案1】:

好的,结合以下的答案:

我想出了这个:

<# : batch portion (begins PowerShell multi-line comment block)

set TITLE=Untitled - Notepad
for /f "tokens=1,2,3,4" %%a in ('powershell -NoProfile -NoLogo "iex (${%~f0} | out-string)"') DO (
    set LEFT=%%a
    set TOP=%%b
    set RIGHT=%%c
    set BOTTOM=%%d
)
echo LEFT=%LEFT% TOP=%TOP% RIGHT=%RIGHT% BOTTOM=%BOTTOM%

@GOTO :EOF
: end batch / begin PowerShell #>

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
}
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}
"@

$Handle = Get-Process | Where-Object { $_.MainWindowTitle -match $env:TITLE } | ForEach-Object { $_.MainWindowHandle }
if ( $Handle -is [System.Array] ) { $Handle = $Handle[0] }
$WindowRect = New-Object RECT
$GotWindowRect = [Window]::GetWindowRect($Handle, [ref]$WindowRect)
Write-Host $WindowRect.Left $WindowRect.Top $WindowRect.Right $WindowRect.Bottom

【讨论】:

  • 谢谢!我遍历批次,它很慢,但它们可能不是 C 的捷径。:Loop &lt;#....%BOTTOM% GOTO Loop
【解决方案2】:

我使用以下:

您可以通过更改以下行中的线来设置位置cscript //nologo "%temp%\pos.vbs" "%~F0" 75 15

注意:在这种情况下,距左侧 75x 像素,距顶部 15x 像素。

@echo off &cls
mode con: cols=70 lines=15 &color f0

:: - Position the CMD Window Using .VBS -----------------------------------------
:: == MUST be at the beginning of the Batch =====================================
   IF "%~1" == "RestartedByVBS" Goto :Code

   :: Create the VBScript, if not exist
   if not exist "%temp%\pos.vbs" (
      (for /F "tokens=1*" %%a in ('findstr "^VBS:" ^< "%~F0"') do (
         echo(%%b
      )) > "%temp%\pos.vbs"
   )
   cscript //nologo "%temp%\pos.vbs" "%~F0" 75 15
   exit /b
:code
if exist "%temp%\pos.vbs" ( del /q "%temp%\pos.vbs" )
:: ------------------------------------------------------------------------------

echo. The rest of your batch script here.

echo. Press any key to exit &>nul timeout /t -1 &exit /B








:: - Position the CMD Window Using .VBS -----------------------------------------
:Pos <BatchFileName> <X_Coordinate> <Y_Coordinate>

:: This Function will take three inputs: The name of the Batch file to execute
:: and the X and Y Coordinates to Position its CMD window

VBS: Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
VBS: Set objConfig = objWMIService.Get("Win32_ProcessStartup")
VBS: objConfig.SpawnInstance_
VBS: objConfig.X = WScript.Arguments(1)
VBS: objConfig.Y = WScript.Arguments(2)
VBS: Set objNewProcess = objWMIService.Get("Win32_Process")
VBS: intReturn = objNewProcess.Create( chr(34) & WScript.Arguments(0) &chr(34)& " RestartedByVBS", Null, objConfig, intProcessID)
:: ------------------------------------------------------------------------------

免责声明:最初发布在此线程中:Positioning CMD Window

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    相关资源
    最近更新 更多