2016-06-15 174 views
1

我建立了一个批处理程序,我目前正在调整,以使其更具可读性/用户友好性。批处理文件最大化当前窗口

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

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

我知道Windows键最大化是ALT +空间然后X。我有想法,也许我可以在批处理脚本中使用某种SendKeys来自动执行该操作?我没有任何运气在网上找到信息。

无论如何编程这个在同一个.bat实例内最大化?

+2

这不是DOS,它是Windows命令行!批处理脚本中没有'SendKeys'命令。但您可能对[AutoIt](http://www.autoit.com)或[AutoHotkey](http://www.autohotkey.com)感兴趣... – aschipfl

+0

对不起,[AutoIt](http:// www.autoitscript.com)链接是错误的 - 现在它是正确的... – aschipfl

+1

您可以使用'echo ... >> x.vbs'创建'.vbs'文件在该vbs中添加'sendkeys'命令&call thet与wscript。 – anishsane

回答

1

我在想这个方法:

@echo off 
set mytitle=%random% 
title %mytitle% 
echo WScript.sleep 1000 > %temp%\max-me.vbs 
echo WScript.CreateObject("WScript.Shell").AppActivate "%mytitle%" >> %temp%\max-me.vbs 
echo WScript.CreateObject("WScript.Shell").SendKeys "%% n" >> %temp%\max-me.vbs 
cscript %temp%\max-me.vbs >nul 
echo further code goes here... 
pause 

然而,出于某种原因,似乎的SendKeys不与CMD窗口工作。 下面的机制起作用,但你失去了现有的窗口&它的退出状态。

@echo off 
if not defined iammaximized (
    set iammaximized=1 
    start /max "" "%0" "%*" 
    exit 
) 
echo further code goes here... 
pause 
1

有一个叫做“cmdow.exe”的小工具(可以下载ad sourceforge.net)。但要小心,这个工具可以让你做很多你可能不想做的事情。

为了最大化currend cmd窗口:

cmdow @ /max 

为了恢复再次当前cmd窗口:

cmdow @ /res 
2

控制台窗口不被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行,然后展开垂直滚动缓冲器10000线。


下面是一个更完整的批处理+的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 
相关问题