Changing Command Prompt Text Size C++

我们两清 提交于 2021-02-05 06:50:40

问题


I developed a console application in C++ that will run only on Windows. I want to change Command Prompt's text size when the program runs. I did some search but, I could not find anything that would solve the problem. Everybody is just talking about changing the color.

Anyway, if this is possible, how can I change the text size of Command Prompt.

Thank you!


回答1:


You have to initialize CONSOLE_FONT_INFOEX structure with sizeof(CONSOLE_FONT_INFOEX) before getting current font info.
Also you have to use only available sizes:

  • 4 x 6
  • 16 x 8
  • 6 x 9
  • 8 x 9
  • 5 x 12
  • 7 x 12
  • 8 x 12
  • 16 x 12
  • 12 x 16
  • 10 x 18
BOOL SetConsoleFontSize(COORD dwFontSize){
    HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_FONT_INFOEX info{sizeof(CONSOLE_FONT_INFOEX)};
    if (!GetCurrentConsoleFontEx(output, false, &info))
        return false;
    info.dwFontSize = dwFontSize;
    return SetCurrentConsoleFontEx(output, false, &info);
}



回答2:


You can use the CONSOLE_FONT_INFOEX struct to specify the parameters of the text/formatting of your command prompt writes.

For more information, click here.

Extract from above link.

here's a full example. I can't say if it works, because apparently functions Get/SetCurrentConsoleFontEx are only available in windows vista and later.

#include <iostream>
#include <windows.h>

int main(){
    HANDLE outcon = GetStdHandle(STD_OUTPUT_HANDLE);//you don't have to call this function every time

    CONSOLE_FONT_INFOEX font;//CONSOLE_FONT_INFOEX is defined in some windows header
    GetCurrentConsoleFontEx(outcon, false, &font);//PCONSOLE_FONT_INFOEX is the same as CONSOLE_FONT_INFOEX*
    font.dwFontSize.X = 7;
    font.dwFontSize.Y = 12;
    SetCurrentConsoleFontEx(outcon, false, &font);

    SetConsoleTextAttribute(outcon, 0x0C);
    std::cout << "I'm red";
    std::cin.get();
    return 0;
}


来源:https://stackoverflow.com/questions/18114395/changing-command-prompt-text-size-c

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