控制台应用程序-更改文字颜色 [英] Console application- text color changing
本文介绍了控制台应用程序-更改文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了这段代码来更改控制台应用程序中文本的颜色
您能解释一下工作方式吗(逐行)...
I got this code to change the color of the text in console application
Can you explain how is working(line by line)...
[DllImport("kernel32.dll")]
public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput,int wAttributes);
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(uint nStdHandle);
uint STD_OUTPUT_HANDLE = 0xfffffff5;
IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,3);
//制作不同的颜色
推荐答案
首先,您完全不需要使用P/Invoke .它将终止平台兼容性.您可以运行彩色控制台代码,该代码可以在许多不同的平台上运行,例如Linux,Max OS X(例如,通过Mono)-无需重新编译.通常,您可以同时为字体背景和前景使用16种颜色.
本文介绍了如何.我试过了-它有效.请参阅:
http://www.dotnetperls.com/console-color [
First of all, you absolutely don''t need to use P/Invoke. It would kill platform compatibility. You can run the multi-colored console code which would run on many different platforms, such as Linux, Max OS X (for example, via Mono) — without recompilation. Usually, you can use 16 colors for both font background and foreground at the same time.
This article explains how. I tried it — it works. Please see:
http://www.dotnetperls.com/console-color[^].
—SA
无需调用kernel API
.只需将所需的颜色设置为Console.ForegroundColor
属性.
No need to callkernel API
. Simply set the desired color toConsole.ForegroundColor
property.
这篇关于控制台应用程序-更改文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文