windows - 批处理文件以最大化当前窗口

标签 windows batch-file

我构建了一个批处理程序,目前正在对其进行调整以使其更具可读性/用户友好性。

我希望我的 .bat 文件在 .bat 文件本身中自动设置为最大化。

我在网上阅读了有关 START/MAX 的内容,但这只会打开命令提示符窗口的一个新实例。我不想有两个 .bat 文件只是为了最大化一个。

我知道 Windows 的最大化键是 ALT+SPACE 然后是 X。我想到了也许我可以在批处理脚本中使用某种 SendKeys 来实现自动化?我在网上找不到任何信息。

有没有办法在同一个 .bat 实例中对其进行编程以最大化?

最佳答案

控制台窗口不是以像素为单位的w*h来衡量的,而是以行和列为单位。在某种程度上,物理尺寸将取决于用户定义的字体和大小。

最简单的解决方案是使用 mode 命令增加行数和/或列数。您还可以使用 PowerShell 帮助程序增加滚动缓冲区。下面是一个批处理函数,我用过几次来操作所有这些值。

:consize <columns> <lines> <scrolllines>
:: change console window dimensions and buffer
mode con: cols=%1 lines=%2
powershell -noprofile "$W=(get-host).ui.rawui; $B=$W.buffersize; $B.height=%3; $W.buffersize=$B"
goto :EOF

:consize 函数位于脚本的底部,最后的 exit/bgoto :EOF 在主脚本运行时结束时。 See this page有关批处理函数的更多示例。

示例用法:

call :consize 80 33 10000

...会将窗口扩展到 80 列和 33 行,然后将垂直滚动缓冲区扩展到 10,000 行。


这是一个更完整的 Batch + PowerShell 混合脚本,它将窗口移动到 0,0,然后将其宽度和高度更改为屏幕将容纳的最大列数和行数。我不得不反复试验 $max.Height$max.Width 值,所以我不确定不同的显示分辨率和不同的字体大小会有什么不同影响剧本。不过,对于政府工作来说,它应该足够近了。

<# : batch portion
@echo off & setlocal

call :maximize

rem /* ###############################
rem    Your main batch code goes here.
rem    ############################### */

goto :EOF

:maximize
set "scrollLines=10000"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF

rem // end batch / begin PowerShell hybrid code #>

# Moving the window to coordinates 0,0 requires importing a function from user32.dll.
add-type user32_dll @'
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
        int x, int y, int cx, int cy, uint uFlags);
'@ -namespace System

# Walk up the process tree until we find a window handle
$id = $PID
do {
    $id = (gwmi win32_process -filter "ProcessID='$id'").ParentProcessID
    $hwnd = (ps -id $id).MainWindowHandle
} while (-not $hwnd)

# This is where the window moves!
[void][user32_dll]::SetWindowPos($hwnd, [IntPtr]::Zero, 0, 0, 0, 0, 0x41)

# Maximize the window
$console = (get-host).ui.rawui
$max = $console.MaxPhysicalWindowSize
$max.Height -= 1  # account for the titlebar
$max.Width -= 5  # account for the scrollbar
$buffer = $max
$buffer.Height = $env:scrollLines
$console.BufferSize = $buffer
$console.WindowSize = $max

关于windows - 批处理文件以最大化当前窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37828244/

相关文章:

python - 脚本是否可以打开 WSL bash shell 并向其发送命令?

windows - Windows 命令行 merge 工具

c++ - 如何在两个 C++ MFC 插件之间进行通信?

html - Mac 与 Windows 的 CSS 问题

batch-file - 如何在批处理脚本中拆分字符串

batch-file - 如何使用 .bat 文件重新启动进程

windows - 如果批处理 (.bat) 文件中存在错误级别

c++ - WinAPI:是否需要在可执行内存映射文件上调用 FlushInstructionCache?

batch-file - 如何在批处理文件中运行多个命令?

linux - 如何将 Linux shell 脚本转换为 Windows 批处理文件?