ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

在Windows 10的cmd输出彩色文本

2021-12-10 07:31:06  阅读:410  来源: 互联网

标签:10 kernel32 cmd FOREGROUND Windows text contains color define


# termcolor outputs stuff like '\033[0m'. It doesn't work in Windows 10's cmd.

import sys
from ctypes import *
user32 = CDLL('user32.dll'); kernel32 = CDLL('kernel32.dll')

# https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute
# HANDLE WINAPI GetStdHandle(DWORD nStdHandle)
# BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes)
# WINCON.H:
#define FOREGROUND_BLUE      0x0001 // text color contains blue.
#define FOREGROUND_GREEN     0x0002 // text color contains green.
#define FOREGROUND_RED       0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define BACKGROUND_BLUE      0x0010 // background color contains blue.
#define BACKGROUND_GREEN     0x0020 // background color contains green.
#define BACKGROUND_RED       0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.
STD_OUTPUT_HANDLE = -11
hStdOut = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

class Color:
    def __init__(m, fg_str_or_color, bg = ''):
        if isinstance(fg_str_or_color, int):
            sys.stdout.flush()
            kernel32.SetConsoleTextAttribute(hStdOut, fg_str_or_color)
            return
        def f(s):
            t = { 'b':1, 'B':8+1, 'g':2, 'G':8+2, 'r':4, 'R':8+4 }
            d = 0
            for c in s: d |= t.get(c, 0)
            #print(s, hex(d))
            return d
        m.__init__(f(fg_str_or_color) | (f(bg) << 4))

if __name__ == '__main__':
    Color('G'); print('Green ', end=''); Color('rgb'); print('gray')
    Color('RGB', 'G'); print('Hello'), Color('rgb'), print('world')

# 如果有人想做个cmdcolor包,请把代码拿去自便。甚至可以扩展为WFP - Windows Fun Pack :-)
# 比如一个线程空白DialogBox,另一个干事情。GetWindowDC()后就可以画画了。哪怕GetDC(NULL)
# 在屏幕上画呢。pip install pywin32; D:\Python39\Lib\site-packages\PyWin32.chm
# 简历里一句"开发过一个python wheel,在github上有n颗星"?I admit我爱幻想。
# win32console里一堆函数,如SetConsoleTitle,就是没有SetConsoleTextAttribute。
# 四子棋: https://www.cnblogs.com/funwithwords/p/15626636.html
# 画线和实心椭圆…… GDI
#
# Copyright (c) Some Guy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Author: Some Guy <someguy@somewhere>

 

标签:10,kernel32,cmd,FOREGROUND,Windows,text,contains,color,define
来源: https://www.cnblogs.com/funwithwords/p/15670028.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有