C# 在 .NET 的控制台窗口中更改字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1802600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing font in a Console window in .NET
提问by theraneman
I have built a neat little Console app which basically interacts with ASP.NET projects on a users machine. I have a really trivial need, all I need to do is before I show the Console window, I need to have it a black background, a lime green foreground and a Lucida font. I could achieve the color part by using the static methods of Console class. Although there is nothing in the class which talks about changing fonts? Has anyone been able to change Console font programatically?
我已经构建了一个简洁的小控制台应用程序,它基本上与用户机器上的 ASP.NET 项目进行交互。我有一个非常微不足道的需求,我需要做的就是在显示控制台窗口之前,我需要将其设置为黑色背景、柠檬绿色前景和 Lucida 字体。我可以通过使用 Console 类的静态方法来实现颜色部分。虽然课堂上没有任何关于更改字体的内容?有没有人能够以编程方式更改控制台字体?
Any help appreciated.
任何帮助表示赞赏。
回答by Paul Turner
The console window is a pretty basic environment. Whilst it's possible to change the font in most recent versions of Windows using unmanaged Win32 API calls, it's not supported through a managed API.
控制台窗口是一个非常基本的环境。虽然可以使用非托管 Win32 API 调用在最新版本的 Windows 中更改字体,但不支持托管 API。
Font colour, however, can be set using the Console.ForegroundColor
property.
但是,可以使用该Console.ForegroundColor
属性设置字体颜色。
Arguably, you shouldn't be trying to exert so much control over a console window as it's intended to be a simple text-only interface. If you want to have absolute control over the typeface used, you should consider upgrading to a Windows application, as this will let you trivially create a text window and do whatever you want to the fonts and colours in use.
可以说,您不应该试图对控制台窗口施加太多控制,因为它旨在成为一个简单的纯文本界面。如果您想对所使用的字体拥有绝对的控制权,您应该考虑升级到 Windows 应用程序,因为这将让您轻松创建一个文本窗口,并对所使用的字体和颜色进行任何您想做的事情。
回答by OregonGhost
Please don't do that on an application that is meant to be used from other users, unless they request this feature. Consolas is the only font for consoles on my system :p
请不要在旨在供其他用户使用的应用程序上执行此操作,除非他们请求此功能。Consolas 是我系统上唯一的控制台字体:p
You can try to change the properties of the link that opens the console (or the executable), but that is a local setting if I'm right. This is likely the thing you want if it's just for running the app on your system. I'm not sure how you can change these programmatically.
您可以尝试更改打开控制台(或可执行文件)的链接的属性,但如果我是对的,那是本地设置。如果它只是为了在您的系统上运行应用程序,这可能是您想要的。我不确定如何以编程方式更改这些。
Since Vista, there's SetCurrentConsoleFontEx
, which may do what you want, if you can get it to work with the Console class. Did I mention I will hate you for that if the app ever comes to my system? ;)
从 Vista 开始,SetCurrentConsoleFontEx
如果您可以让它与 Console 类一起工作,它可能会做您想做的事情。我有没有提到如果应用程序出现在我的系统中我会恨你?;)