|
发表于 2023-12-3 13:31:04
|
显示全部楼层
要改变屏幕的字体颜色,可以使用Windows的控制台API函数。以下是一个示例代码,可以将屏幕中的第5行到24行设置为白底黑字格式。
- import ctypes
- def set_text_color(color):
- STD_OUTPUT_HANDLE = -11
- handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
- ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
- def set_screen_colors(start_row, end_row, color):
- for row in range(start_row, end_row + 1):
- # 定位到指定行
- ctypes.windll.kernel32.SetConsoleCursorPosition(ctypes.windll.kernel32.GetStdHandle(-12), ctypes.wintypes.COORD(0, row))
-
- # 设置文本颜色和背景颜色
- set_text_color(color)
- # 输出空格填充整行
- ctypes.windll.kernel32.FillConsoleOutputCharacterW(ctypes.windll.kernel32.GetStdHandle(-11), ' ', ctypes.windll.kernel32.GetLargestConsoleWindowSize(), ctypes.wintypes.COORD(0, row), ctypes.byref(ctypes.wintypes.DWORD()))
- ctypes.windll.kernel32.FillConsoleOutputAttribute(ctypes.windll.kernel32.GetStdHandle(-11), color, ctypes.windll.kernel32.GetLargestConsoleWindowSize(), ctypes.wintypes.COORD(0, row), ctypes.byref(ctypes.wintypes.DWORD()))
- # 示例调用
- set_screen_colors(5, 24, 0x70)
复制代码
这段代码使用了ctypes来调用Windows的控制台API函数,通过设置文本颜色和背景颜色来改变屏幕的字体颜色。在示例中,使用了黑底白字的格式(0x70),你可以根据需要调整颜色参数。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 |
|