【方案一】比较原始的方法,思路比较容易理解。
@echo off
::Batcher@bbs.bathome.net
::2009-03-04
::命令行窗口自动居中之批处理版@CMD@XP
::http://bbs.bathome.net/thread-3582-1-1.html
setlocal enabledelayedexpansion
::指定命令行窗口字体大小(默认是8*16)
set FontWidth=8
set FontHeight=16
::指定命令行窗口大小(默认是80*25)
set WinCol=80
set WinRow=25
::ClearOldConf
for /f "tokens=3 delims=\" %%a in ('reg query HKEY_CURRENT_USER\Console^|findstr "_system32_cmd.exe"') do (
if "%%a" neq "" (
reg delete HKEY_CURRENT_USER\Console\%%a /va /f
)
)
::SetFontSize
call :Dec2Hex %FontWidth% FontWidthH
call :Dec2Hex %FontHeight% FontHeightH
set FontWidthH=0000%FontWidthH%
set FontWidthH=%FontWidthH:~-4%
reg add HKEY_CURRENT_USER\Console /v FontSize /t reg_dword /d 0x%FontHeightH%%FontWidthH% /f
::SetWinSize
call :Dec2Hex %WinCol% WinColH
call :Dec2Hex %WinRow% WinRowtH
set WinColH=0000%WinColH%
set WinColH=%WinColH:~-4%
reg add HKEY_CURRENT_USER\Console /v WindowSize /t reg_dword /d 0x%WinRowtH%%WinColH% /f
::GetWinSize
set /a WinWidth=WinCol*FontWidth+33
set /a WinHeight=WinRow*FontHeight+45
::GetSrcSize
for /f "tokens=2 delims==" %%a in ('wmic desktopmonitor get screenwidth /value') do (
set ScrWidth=%%a
goto :GetSrcHeight
)
:GetSrcHeight
for /f "tokens=2 delims==" %%a in ('wmic desktopmonitor get screenheight /value') do (
set ScrHeight=%%a
goto :GetPosition
)
:GetPosition
set /a PosLeft=(ScrWidth-WinWidth)/2
set /a PosTop=(ScrHeight-WinHeight)/2
call :Dec2Hex %PosLeft% PosLeftH
call :Dec2Hex %PosTop% PosTopH
set PosLeftH=0000%PosLeftH%
set PosLeftH=%PosLeftH:~-4%
set PosTopH=0000%PosTopH%
set PosTopH=%PosTopH:~-4%
reg add HKEY_CURRENT_USER\Console /v WindowPosition /t reg_dword /d 0x%PosTopH%%PosLeftH% /f
start cmd
goto :eof
:Dec2Hex
set num=%1
set str=0123456789ABCDEF
set numDiv=%num%
set numConn=
:LoopD2H
set /a numMod=numDiv%%16
set numMod=!str:~%numMod%,1!
set /a numDiv/=16
set numConn=%numMod%%numConn%
if %numDiv% equ 0 (
set %2=%numConn%
goto :eof
)
goto :LoopD2H
【方案二】采用18楼pusofalse兄的方法:移位+或,可以缩短代码、提高执行效率。
@echo off
::Batcher@bbs.bathome.net
::2009-03-17
::命令行窗口自动居中之批处理版@CMD@XP
::http://bbs.bathome.net/thread-3582-1-1.html
setlocal enabledelayedexpansion
::指定命令行窗口字体大小(默认是8*16)
set FontWidth=8
set FontHeight=16
::指定命令行窗口大小(默认是80*25)
set WinCol=80
set WinRow=25
::SetFontSize
set /a "FontSizeD=FontHeight<<16|FontWidth"
call :Dec2Hex %FontSizeD% FontSizeH
reg add HKEY_CURRENT_USER\Console /v FontSize /t reg_dword /d 0x%FontSizeH% /f
::SetWinSize
set /a "WinSizeD=WinRow<<16|WinCol"
call :Dec2Hex %WinSizeD% WinSizeH
reg add HKEY_CURRENT_USER\Console /v WindowSize /t reg_dword /d 0x%WinSizeH% /f
::GetWinSize
set /a WinWidth=WinCol*FontWidth+33
set /a WinHeight=WinRow*FontHeight+45
::GetSrcSize
for /f "tokens=2 delims==" %%a in ('wmic desktopmonitor get screenwidth /value') do (
set ScrWidth=%%a
goto :GetSrcHeight
)
:GetSrcHeight
for /f "tokens=2 delims==" %%a in ('wmic desktopmonitor get screenheight /value') do (
set ScrHeight=%%a
goto :GetPosition
)
:GetPosition
set /a PosLeft=(ScrWidth-WinWidth)/2
set /a PosTop=(ScrHeight-WinHeight)/2
echo %PosLeft% %PosTop%
set /a "PosD=PosTop<<16|PosLeft"
call :Dec2Hex %PosD% PosH
reg add HKEY_CURRENT_USER\Console /v WindowPosition /t reg_dword /d 0x%PosH% /f
start cmd
goto :eof
:Dec2Hex
set num=%1
set str=0123456789ABCDEF
set numDiv=%num%
set numConn=
:LoopD2H
set /a numMod=numDiv%%16
set numMod=!str:~%numMod%,1!
set /a numDiv/=16
set numConn=%numMod%%numConn%
if %numDiv% equ 0 (
set %2=%numConn%
goto :eof
)
goto :LoopD2H
要删除注册表项才能还原了
到此这篇关于命令行窗口自动居中之批处理版实现代码(娱乐)的文章就介绍到这了,更多相关命令行窗口自动居中内容请搜索aitechtogether.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持aitechtogether.com!