How to set console font to Raster Font, programmatically?(如何以编程方式将控制台字体设置为光栅字体?)
问题描述
如何确保命令提示符的当前字体在运行时是默认的光栅字体?我正在使用 C++ 和 WinApi.
How do I make sure that the command prompt's current font is the default Raster Font, at runtime? I'm using C++ with WinApi.
目前我已经使用了 GetConsoleFontEx(); 和 SetConsoleFontEx();,但我无法为 找到正确的值CONSOLE_FONT_INFOEX 的 FaceName 属性.我在网上找到了一些字体设置为 Lucida 和/或 Consolas 的示例,但这不是我想要的.
For now I've used GetConsoleFontEx(); and SetConsoleFontEx();, but I haven't been able to find the right value for the CONSOLE_FONT_INFOEX's FaceName property. I found a few examples online where the font was set to Lucida and/or Consolas, but that's not what I'm looking for.
这是我当前的代码片段:
Here's a snippet of my current code:
COORD fs = {8, 8};
CONSOLE_FONT_INFOEX cfie = {0};
cfie.cbSize = sizeof(cfie);
GetCurrentConsoleFontEx(hOut, 0, &cfie);
char fn[] = "Raster"; // Not really doing anything
strcpy_s((char*)cfie.FaceName, 32, fn); // Not sure if this is right
cfie.dwFontSize.X = fs.X;
cfie.dwFontSize.Y = fs.Y;
SetCurrentConsoleFontEx(hOut, 0, &cfie);
我测试了SetCurrentConsoleFontEx()的返回值,非零,表示调用成功.不过字体没有变化.
I have tested the return value of SetCurrentConsoleFontEx(), and it's non-zero, indicating a successful call. The font does not change, though.
推荐答案
适配SetCurrentConsoleFontEx() 的 85%29.aspx" rel="noreferrer">MS 示例,这似乎有效.请注意,当按下提示 Enter 时,整个控制台会更改字体.
Adapting the MS example of SetCurrentConsoleFontEx(), this seems to work. Note that when the cue Enter is pressed, the whole console changes font.
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** args)
{
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 20;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
printf("A quick brown fox jumps over the lazy dog
");
printf("Setting to Lucida Console: press <Enter> ");
getchar();
wcscpy(cfi.FaceName, L"Lucida Console");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
printf("Setting to Consolas: press <Enter> ");
getchar();
wcscpy(cfi.FaceName, L"Consolas");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
printf("Press <Enter> to exit");
getchar();
return 0;
}
这篇关于如何以编程方式将控制台字体设置为光栅字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式将控制台字体设置为光栅字体?
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
