Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

c++ - How to set console font to Raster Font, programmatically?

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.

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);

I have tested the return value of SetCurrentConsoleFontEx(), and it's non-zero, indicating a successful call. The font does not change, though.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...