【问题标题】:Windows Command Prompt set character sizeWindows 命令提示符设置字符大小
【发布时间】:2015-09-07 07:15:28
【问题描述】:

有设置命令提示符字符大小为 10x18 的命令吗?而且.. 我可以在 python 脚本中使用os.system() 函数来执行此操作吗?


谢谢

【问题讨论】:

    标签: python windows python-2.7 command-line command-prompt


    【解决方案1】:

    更改命令提示符的大小:

    import os
    os.system("mode con cols=18 lines=10") # cols for the width and lines for the length
    

    更改命令提示符的字符大小:

    import ctypes
    
    STD_OUTPUT_HANDLE = -11
    
    class COORD(ctypes.Structure):
        _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]
    
    class CONSOLE_FONT_INFOEX(ctypes.Structure):
        _fields_ = [("cbSize", ctypes.c_ulong),
                    ("nFont", ctypes.c_ulong),
                    ("dwFontSize", COORD)]
    
    font = CONSOLE_FONT_INFOEX()
    font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
    font.nFont = 12
    font.dwFontSize.X = 10  # in your case X=10
    font.dwFontSize.Y = 18  # and Y=18
    
    
    
    handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    ctypes.windll.kernel32.SetCurrentConsoleFontEx(
            handle,False, ctypes.byref(font))
    

    【讨论】:

    • 设置窗口的列和行而不是字符
    • 为什么要覆盖 OP 的 FontFamilyFontWeight?例如,FontFamily (54 & 0xf == 6) 的低 4 位表示 TrueType、固定间距字体。高 4 位 (48) 表示“现代”字体系列。出于某种奇怪的原因,您似乎坚定地避免致电GetCurrentConsoleFontEx
    • 将值设置为 0 只是使用任意字体索引 12 的当前值。这仍然是错误的。
    • 控制台维护了一个字体记录列表(可能是一个链表),其中一个是当前记录。此函数既更新给定nFont 索引处的记录,又将其设置为当前字体。 OP只需要修改当前字体记录的dwFontSize即可,调用ctypes.windll.kernel32.GetCurrentConsoleFontEx(handle, False, ctypes.byref(font))即可。
    猜你喜欢
    • 2014-08-30
    • 2011-04-02
    • 2012-12-08
    • 1970-01-01
    • 2014-09-13
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    相关资源
    最近更新 更多