当前位置: 代码网 > it编程>前端脚本>Dos/bat > 命令行窗口自动居中之批处理版实现代码(娱乐)

命令行窗口自动居中之批处理版实现代码(娱乐)

2024年05月15日 Dos/bat 我要评论
【方案一】比较原始的方法,思路比较容易理解。@echo off::batcher@bbs.bathome.net::2009-03-04::命令行窗口自动居中之批处理版@cmd@xp::http://

【方案一】比较原始的方法,思路比较容易理解。

@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

要删除注册表项才能还原了

到此这篇关于命令行窗口自动居中之批处理版实现代码(娱乐)的文章就介绍到这了,更多相关命令行窗口自动居中内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com