【方案一】比较原始的方法,思路比较容易理解。
@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
要删除注册表项才能还原了
到此这篇关于命令行窗口自动居中之批处理版实现代码(娱乐)的文章就介绍到这了,更多相关命令行窗口自动居中内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论