In Windows, Does SetCurrentConsoleFontEx change console's font size?

故事扮演 提交于 2020-07-11 05:39:30

问题


Other guys recommend the SetCurrentConsoleFontEx function but I don't know how to apply it to my project.

I want to change the font size of only some texts, not all texts.

Does SetCurrentConsoleFontEx() change the console's font size?

Or are there other ways to change it?

If there is, please show me the console function and a simple example.


回答1:


Here is an example of using SetCurrentConsoleFontEx to change the console's font size. This affects the entire console window -- so like Joachim Pileborg already said, if you want mixed font sizes in a single console window, this won't help you.

#define _WIN32_WINNT 0x500
#include <Windows.h>

// PrintChars sends ASCII characters to console output
// for demonstration purposes.
// depends only on Win32 API
static void PrintChars() {
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD num_written;
    static const char* cr_lf = "\r\n";
    for(char c=' '; c<'\x7f'; ++c) {
        WriteFile(hStdout, &c, 1, &num_written, NULL);
        if(c % 16 == 15) WriteFile(hStdout, cr_lf, 2, &num_written, NULL);
    }
    WriteFile(hStdout, cr_lf, 2, &num_written, NULL);
}

// WaitEnter blocks execution until the user
// presses the enter key.
// depends only on Win32 API
static void WaitEnter() {
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    char buffer;
    DWORD num_read;
    do {
        num_read = 0;
        ReadFile(hStdin, &buffer, 1, &num_read, NULL);
    } while(num_read && buffer != '\n');
}

int main() {
    // Display some example characters
    PrintChars();

    // Wait for the user to see how the current font looks
    WaitEnter();   

    // Get a handle to the current console screen buffer
    HANDLE hcsb = CreateFileA("CONOUT$", GENERIC_WRITE | GENERIC_READ,
        FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 

    CONSOLE_FONT_INFOEX cfi = {sizeof(cfi)};

    // Populate cfi with the screen buffer's current font info
    GetCurrentConsoleFontEx(hcsb, FALSE, &cfi);

    // Modify the font size in cfi
    cfi.dwFontSize.X *= 2;
    cfi.dwFontSize.Y *= 2;

    // Use cfi to set the screen buffer's new font
    SetCurrentConsoleFontEx(hcsb, FALSE, &cfi);

    // Wait for the user to see the difference before exiting
    WaitEnter();
    CloseHandle(hcsb);
}


来源:https://stackoverflow.com/questions/36590430/in-windows-does-setcurrentconsolefontex-change-consoles-font-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!