OEM Alt codes的行爲對代碼1-31和127有誤導性。系統的OEM代碼頁是ASCII的超集。但是,此代碼範圍不輸入ASCII控制字符,而是舊IBM PC系統有時會顯示和打印ASCII控制字符的字符。例如,Alt + 1輸入「☺」(U + 263A,白色笑臉)而不是ASCII SOH(U + 0001,標題開始)。爲了實現這一點,當將這些字符編碼到OEM代碼頁時,WideCharToMultiByte
的默認行爲是使用相應的ASCII控制字符作爲最佳編碼編碼。所以文本編輯器可能不會警告你,你看到的不是你會得到的。
控制檯不顯示控制字符作爲舊的IBM PC圖形字符。它解釋了其中的一些例如U + 0009(水平製表符 - 提前光標8個空格)和U + 000D(回車 - 將光標前移到下一行的開頭),但其中大部分都顯示爲當前字體的字形用於未映射的字符,例如盒裝問號或空盒子。如果要將「☺」打印到控制檯,則應使用控制檯的寬字符API,該API可與Windows本機Unicode(UTF-16LE)字符串一起使用。
幸運的是,CMD是一個Unicode應用程序,所以打印「☺」到控制檯沒有問題。問題是用於批處理腳本的編碼。唯一的通用解決方案是將批處理文件保存爲不帶BOM(字節順序標記)的UTF-8。你需要一個IDE或者文本編輯器來保存沒有BOM的UTF-8,比如Notepad ++。
CMD使用舊控制檯代碼頁逐行解碼批處理腳本,您可以使用命令chcp.com 65001
更改爲UTF-8。我建議永久更改爲UTF-8,因爲根據Windows版本的不同,控制檯會出現各種代碼頁65001的錯誤。您可以暫時切換到UTF-8以將非ASCII字符串作爲環境變量加載。否則,請將批處理文件的其餘部分嚴格保留爲7位ASCII,以避免本地化問題。
下面是一個例子。有可能比創建所有這些局部環境變量更普遍的方式來做這件事,但它只是表明它原則上工作。
graphics_ctl.bat
@echo off
REM ASCII Control-Character Graphics
REM File encoding: UTF-8 (no BOM)
REM Set the console to codepage 65001 (UTF-8).
for /f "tokens=2 delims=:" %%a in ('chcp.com') do set "CONSOLE_CODEPAGE=%%a"
set "CONSOLE_CODEPAGE=%CONSOLE_CODEPAGE: =%"
chcp.com 65001 >NUL
set "GCTL_WHITE_SMILING_FACE=☺"
set "GCTL_BLACK_SMILING_FACE=☻"
set "GCTL_BLACK_HEART_SUIT=♥"
set "GCTL_BLACK_DIAMOND_SUIT=♦"
set "GCTL_BLACK_CLUB_SUIT=♣"
set "GCTL_BLACK_SPADE_SUIT=♠"
set "GCTL_BULLET=•"
set "GCTL_INVERSE_BULLET=◘"
set "GCTL_WHITE_CIRCLE=○"
set "GCTL_INVERSE_WHITE_CIRCLE=◙"
set "GCTL_MALE_SIGN=♂"
set "GCTL_FEMALE_SIGN=♀"
set "GCTL_EIGHTH_NOTE=♪"
set "GCTL_BEAMED_EIGHTH_NOTES=♫"
set "GCTL_WHITE_SUN_WITH_RAYS=☼"
set "GCTL_BLACK_RIGHT_POINTER=►"
set "GCTL_BLACK_LEFT_POINTER=◄"
set "GCTL_UP_DOWN_ARROW=↕"
set "GCTL_DOUBLE_EXCLAMATION_MARK=‼"
set "GCTL_PILCROW_SIGN=¶"
set "GCTL_SECTION_SIGN=§"
set "GCTL_BLACK_RECTANGLE=▬"
set "GCTL_UP_DOWN_ARROW_WITH_BASE=↨"
set "GCTL_UP_ARROW=↑"
set "GCTL_DOWN_ARROW=↓"
set "GCTL_RIGHT_ARROW=→"
set "GCTL_LEFT_ARROW=←"
set "GCTL_RIGHT_ANGLE=∟"
set "GCTL_LEFT_RIGHT_ARROW=↔"
set "GCTL_BLACK_UP_TRIANGLE=▲"
set "GCTL_BLACK_DOWN_TRIANGLE=▼"
set "GCTL_HOUSE=⌂"
REM Restore the previous console codepage.
chcp.com %CONSOLE_CODEPAGE% >NUL
graphics_box.bat
@echo off
REM Box-Drawing Graphics
REM File encoding: UTF-8 (no BOM)
REM Set the console to codepage 65001 (UTF-8).
for /f "tokens=2 delims=:" %%a in ('chcp.com') do set "CONSOLE_CODEPAGE=%%a"
set "CONSOLE_CODEPAGE=%CONSOLE_CODEPAGE: =%"
chcp.com 65001 >NUL
set "GBOX_LIGHT_SHADE=░"
set "GBOX_MEDIUM_SHADE=▒"
set "GBOX_DARK_SHADE=▓"
set "GBOX_LIGHT_VERTICAL=│"
set "GBOX_LIGHT_VERTICAL_AND_LEFT=┤"
set "GBOX_VERTICAL_SINGLE_AND_LEFT_DOUBLE=╡"
set "GBOX_VERTICAL_DOUBLE_AND_LEFT_SINGLE=╢"
set "GBOX_DOWN_DOUBLE_AND_LEFT_SINGLE=╖"
set "GBOX_DOWN_SINGLE_AND_LEFT_DOUBLE=╕"
set "GBOX_DOUBLE_VERTICAL_AND_LEFT=╣"
set "GBOX_DOUBLE_VERTICAL=║"
set "GBOX_DOUBLE_DOWN_AND_LEFT=╗"
set "GBOX_DOUBLE_UP_AND_LEFT=╝"
set "GBOX_UP_DOUBLE_AND_LEFT_SINGLE=╜"
set "GBOX_UP_SINGLE_AND_LEFT_DOUBLE=╛"
set "GBOX_LIGHT_DOWN_AND_LEFT=┐"
set "GBOX_LIGHT_UP_AND_RIGHT=└"
set "GBOX_LIGHT_UP_AND_HORIZONTAL=┴"
set "GBOX_LIGHT_DOWN_AND_HORIZONTAL=┬"
set "GBOX_LIGHT_VERTICAL_AND_RIGHT=├"
set "GBOX_LIGHT_HORIZONTAL=─"
set "GBOX_LIGHT_VERTICAL_AND_HORIZONTAL=┼"
set "GBOX_VERTICAL_SINGLE_AND_RIGHT_DOUBLE=╞"
set "GBOX_VERTICAL_DOUBLE_AND_RIGHT_SINGLE=╟"
set "GBOX_DOUBLE_UP_AND_RIGHT=╚"
set "GBOX_DOUBLE_DOWN_AND_RIGHT=╔"
set "GBOX_DOUBLE_UP_AND_HORIZONTAL=╩"
set "GBOX_DOUBLE_DOWN_AND_HORIZONTAL=╦"
set "GBOX_DOUBLE_VERTICAL_AND_RIGHT=╠"
set "GBOX_DOUBLE_HORIZONTAL=═"
set "GBOX_DOUBLE_VERTICAL_AND_HORIZONTAL=╬"
set "GBOX_UP_SINGLE_AND_HORIZONTAL_DOUBLE=╧"
set "GBOX_UP_DOUBLE_AND_HORIZONTAL_SINGLE=╨"
set "GBOX_DOWN_SINGLE_AND_HORIZONTAL_DOUBLE=╤"
set "GBOX_DOWN_DOUBLE_AND_HORIZONTAL_SINGLE=╥"
set "GBOX_UP_DOUBLE_AND_RIGHT_SINGLE=╙"
set "GBOX_UP_SINGLE_AND_RIGHT_DOUBLE=╘"
set "GBOX_DOWN_SINGLE_AND_RIGHT_DOUBLE=╒"
set "GBOX_DOWN_DOUBLE_AND_RIGHT_SINGLE=╓"
set "GBOX_VERTICAL_DOUBLE_AND_HORIZONTAL_SINGLE=╫"
set "GBOX_VERTICAL_SINGLE_AND_HORIZONTAL_DOUBLE=╪"
set "GBOX_LIGHT_UP_AND_LEFT=┘"
set "GBOX_LIGHT_DOWN_AND_RIGHT=┌"
set "GBOX_FULL_BLOCK=█"
set "GBOX_LOWER_HALF_BLOCK=▄"
set "GBOX_LEFT_HALF_BLOCK=▌"
set "GBOX_RIGHT_HALF_BLOCK=▐"
set "GBOX_UPPER_HALF_BLOCK=▀"
REM Restore the previous console codepage.
chcp.com %CONSOLE_CODEPAGE% >NUL
graphics_test.bat
@echo off
setlocal
set "scriptdir=%~dp0"
call "%scriptdir%\graphics_ctl.bat"
call "%scriptdir%\graphics_box.bat"
echo.
echo ASCII Control-Character Graphics
echo ----------------------------------------------
echo GCTL_WHITE_SMILING_FACE = %GCTL_WHITE_SMILING_FACE%
echo GCTL_BLACK_SMILING_FACE = %GCTL_BLACK_SMILING_FACE%
echo GCTL_BLACK_HEART_SUIT = %GCTL_BLACK_HEART_SUIT%
echo GCTL_BLACK_DIAMOND_SUIT = %GCTL_BLACK_DIAMOND_SUIT%
echo GCTL_BLACK_CLUB_SUIT = %GCTL_BLACK_CLUB_SUIT%
echo GCTL_BLACK_SPADE_SUIT = %GCTL_BLACK_SPADE_SUIT%
echo GCTL_BULLET = %GCTL_BULLET%
echo GCTL_INVERSE_BULLET = %GCTL_INVERSE_BULLET%
echo GCTL_WHITE_CIRCLE = %GCTL_WHITE_CIRCLE%
echo GCTL_INVERSE_WHITE_CIRCLE = %GCTL_INVERSE_WHITE_CIRCLE%
echo GCTL_MALE_SIGN = %GCTL_MALE_SIGN%
echo GCTL_FEMALE_SIGN = %GCTL_FEMALE_SIGN%
echo GCTL_EIGHTH_NOTE = %GCTL_EIGHTH_NOTE%
echo GCTL_BEAMED_EIGHTH_NOTES = %GCTL_BEAMED_EIGHTH_NOTES%
echo GCTL_WHITE_SUN_WITH_RAYS = %GCTL_WHITE_SUN_WITH_RAYS%
echo GCTL_BLACK_RIGHT_POINTER = %GCTL_BLACK_RIGHT_POINTER%
echo GCTL_BLACK_LEFT_POINTER = %GCTL_BLACK_LEFT_POINTER%
echo GCTL_UP_DOWN_ARROW = %GCTL_UP_DOWN_ARROW%
echo GCTL_DOUBLE_EXCLAMATION_MARK = %GCTL_DOUBLE_EXCLAMATION_MARK%
echo GCTL_PILCROW_SIGN = %GCTL_PILCROW_SIGN%
echo GCTL_SECTION_SIGN = %GCTL_SECTION_SIGN%
echo GCTL_BLACK_RECTANGLE = %GCTL_BLACK_RECTANGLE%
echo GCTL_UP_DOWN_ARROW_WITH_BASE = %GCTL_UP_DOWN_ARROW_WITH_BASE%
echo GCTL_UP_ARROW = %GCTL_UP_ARROW%
echo GCTL_DOWN_ARROW = %GCTL_DOWN_ARROW%
echo GCTL_RIGHT_ARROW = %GCTL_RIGHT_ARROW%
echo GCTL_LEFT_ARROW = %GCTL_LEFT_ARROW%
echo GCTL_RIGHT_ANGLE = %GCTL_RIGHT_ANGLE%
echo GCTL_LEFT_RIGHT_ARROW = %GCTL_LEFT_RIGHT_ARROW%
echo GCTL_BLACK_UP_TRIANGLE = %GCTL_BLACK_UP_TRIANGLE%
echo GCTL_BLACK_DOWN_TRIANGLE = %GCTL_BLACK_DOWN_TRIANGLE%
echo GCTL_HOUSE = %GCTL_HOUSE%
echo.
echo Box-Drawing Graphics
echo ----------------------------------------------
echo GBOX_LIGHT_SHADE = %GBOX_LIGHT_SHADE%
echo GBOX_MEDIUM_SHADE = %GBOX_MEDIUM_SHADE%
echo GBOX_DARK_SHADE = %GBOX_DARK_SHADE%
echo GBOX_LIGHT_VERTICAL = %GBOX_LIGHT_VERTICAL%
echo GBOX_LIGHT_VERTICAL_AND_LEFT = %GBOX_LIGHT_VERTICAL_AND_LEFT%
echo GBOX_VERTICAL_SINGLE_AND_LEFT_DOUBLE = %GBOX_VERTICAL_SINGLE_AND_LEFT_DOUBLE%
echo GBOX_VERTICAL_DOUBLE_AND_LEFT_SINGLE = %GBOX_VERTICAL_DOUBLE_AND_LEFT_SINGLE%
echo GBOX_DOWN_DOUBLE_AND_LEFT_SINGLE = %GBOX_DOWN_DOUBLE_AND_LEFT_SINGLE%
echo GBOX_DOWN_SINGLE_AND_LEFT_DOUBLE = %GBOX_DOWN_SINGLE_AND_LEFT_DOUBLE%
echo GBOX_DOUBLE_VERTICAL_AND_LEFT = %GBOX_DOUBLE_VERTICAL_AND_LEFT%
echo GBOX_DOUBLE_VERTICAL = %GBOX_DOUBLE_VERTICAL%
echo GBOX_DOUBLE_DOWN_AND_LEFT = %GBOX_DOUBLE_DOWN_AND_LEFT%
echo GBOX_DOUBLE_UP_AND_LEFT = %GBOX_DOUBLE_UP_AND_LEFT%
echo GBOX_UP_DOUBLE_AND_LEFT_SINGLE = %GBOX_UP_DOUBLE_AND_LEFT_SINGLE%
echo GBOX_UP_SINGLE_AND_LEFT_DOUBLE = %GBOX_UP_SINGLE_AND_LEFT_DOUBLE%
echo GBOX_LIGHT_DOWN_AND_LEFT = %GBOX_LIGHT_DOWN_AND_LEFT%
echo GBOX_LIGHT_UP_AND_RIGHT = %GBOX_LIGHT_UP_AND_RIGHT%
echo GBOX_LIGHT_UP_AND_HORIZONTAL = %GBOX_LIGHT_UP_AND_HORIZONTAL%
echo GBOX_LIGHT_DOWN_AND_HORIZONTAL = %GBOX_LIGHT_DOWN_AND_HORIZONTAL%
echo GBOX_LIGHT_VERTICAL_AND_RIGHT = %GBOX_LIGHT_VERTICAL_AND_RIGHT%
echo GBOX_LIGHT_HORIZONTAL = %GBOX_LIGHT_HORIZONTAL%
echo GBOX_LIGHT_VERTICAL_AND_HORIZONTAL = %GBOX_LIGHT_VERTICAL_AND_HORIZONTAL%
echo GBOX_VERTICAL_SINGLE_AND_RIGHT_DOUBLE = %GBOX_VERTICAL_SINGLE_AND_RIGHT_DOUBLE%
echo GBOX_VERTICAL_DOUBLE_AND_RIGHT_SINGLE = %GBOX_VERTICAL_DOUBLE_AND_RIGHT_SINGLE%
echo GBOX_DOUBLE_UP_AND_RIGHT = %GBOX_DOUBLE_UP_AND_RIGHT%
echo GBOX_DOUBLE_DOWN_AND_RIGHT = %GBOX_DOUBLE_DOWN_AND_RIGHT%
echo GBOX_DOUBLE_UP_AND_HORIZONTAL = %GBOX_DOUBLE_UP_AND_HORIZONTAL%
echo GBOX_DOUBLE_DOWN_AND_HORIZONTAL = %GBOX_DOUBLE_DOWN_AND_HORIZONTAL%
echo GBOX_DOUBLE_VERTICAL_AND_RIGHT = %GBOX_DOUBLE_VERTICAL_AND_RIGHT%
echo GBOX_DOUBLE_HORIZONTAL = %GBOX_DOUBLE_HORIZONTAL%
echo GBOX_DOUBLE_VERTICAL_AND_HORIZONTAL = %GBOX_DOUBLE_VERTICAL_AND_HORIZONTAL%
echo GBOX_UP_SINGLE_AND_HORIZONTAL_DOUBLE = %GBOX_UP_SINGLE_AND_HORIZONTAL_DOUBLE%
echo GBOX_UP_DOUBLE_AND_HORIZONTAL_SINGLE = %GBOX_UP_DOUBLE_AND_HORIZONTAL_SINGLE%
echo GBOX_DOWN_SINGLE_AND_HORIZONTAL_DOUBLE = %GBOX_DOWN_SINGLE_AND_HORIZONTAL_DOUBLE%
echo GBOX_DOWN_DOUBLE_AND_HORIZONTAL_SINGLE = %GBOX_DOWN_DOUBLE_AND_HORIZONTAL_SINGLE%
echo GBOX_UP_DOUBLE_AND_RIGHT_SINGLE = %GBOX_UP_DOUBLE_AND_RIGHT_SINGLE%
echo GBOX_UP_SINGLE_AND_RIGHT_DOUBLE = %GBOX_UP_SINGLE_AND_RIGHT_DOUBLE%
echo GBOX_DOWN_SINGLE_AND_RIGHT_DOUBLE = %GBOX_DOWN_SINGLE_AND_RIGHT_DOUBLE%
echo GBOX_DOWN_DOUBLE_AND_RIGHT_SINGLE = %GBOX_DOWN_DOUBLE_AND_RIGHT_SINGLE%
echo GBOX_VERTICAL_DOUBLE_AND_HORIZONTAL_SINGLE = %GBOX_VERTICAL_DOUBLE_AND_HORIZONTAL_SINGLE%
echo GBOX_VERTICAL_SINGLE_AND_HORIZONTAL_DOUBLE = %GBOX_VERTICAL_SINGLE_AND_HORIZONTAL_DOUBLE%
echo GBOX_LIGHT_UP_AND_LEFT = %GBOX_LIGHT_UP_AND_LEFT%
echo GBOX_LIGHT_DOWN_AND_RIGHT = %GBOX_LIGHT_DOWN_AND_RIGHT%
echo GBOX_FULL_BLOCK = %GBOX_FULL_BLOCK%
echo GBOX_LOWER_HALF_BLOCK = %GBOX_LOWER_HALF_BLOCK%
echo GBOX_LEFT_HALF_BLOCK = %GBOX_LEFT_HALF_BLOCK%
echo GBOX_RIGHT_HALF_BLOCK = %GBOX_RIGHT_HALF_BLOCK%
echo GBOX_UPPER_HALF_BLOCK = %GBOX_UPPER_HALF_BLOCK%
謝謝!這真的幫了我很多! ☺ –