在批處理腳本上,如何在不使用Cmdow等第三方軟體的情況下從視窗開始到結束獲取 X 和 Y
uj5u.com熱心網友回復:
好的,結合以下答案:
- https://superuser.com/questions/74620/is-there-a-way-to-get-access-to-a-window-handle-in-windows-using-wsh-or-wmi-or
- MPV:獲取視窗位置和大小?還是視窗移動/調整大小的事件?
- 如何在 Windows 批處理檔案中運行 PowerShell 腳本
我想出了這個:
<# : 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
uj5u.com熱心網友回復:
我使用以下內容:
您可以通過更改以下行中的線來設定位置 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)
:: ------------------------------------------------------------------------------
免責宣告:最初發布在此執行緒中:定位 CMD 視窗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420405.html
標籤:
