如何用c改变控制台字体以及字体大小?

测试平台:VS2017

关键代码
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;//粗细
wcscpy_s(cfi.FaceName, L"Raster"); //设置字体,此处设为点阵字体
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

关于CONSOLE_FONT_INFOEX 结构体的定义
typedef struct _CONSOLE_FONT_INFOEX {
ULONG cbSize;
DWORD nFont;
COORD dwFontSize;
UINT FontFamily;
UINT FontWeight;
WCHAR FaceName[LF_FACESIZE];
} CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;

微软给出的成员变量解释
Members
cbSize
The size of this structure, in bytes. This member must be set to sizeof(CONSOLE_FONT_INFOEX) before calling GetCurrentConsoleFontEx or it will fail.

nFont
The index of the font in the system’s console font table.

dwFontSize
A COORD structure that contains the width and height of each character in the font, in logical units. The X member contains the width, while the Y member contains the height.

FontFamily
The font pitch and family. For information about the possible values for this member, see the description of the tmPitchAndFamily member of the TEXTMETRIC structure.

FontWeight
The font weight. The weight can range from 100 to 1000, in multiples of 100. For example, the normal weight is 400, while 700 is bold.

FaceName
The name of the typeface (such as Courier or Arial).

测试代码

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    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\n");

    printf("Setting to Lucida Console: press  ");
	getchar();
	wcscpy_s(cfi.FaceName, L"Lucida Console");
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

	printf("Setting to Consolas: press  ");
	getchar();
	wcscpy_s(cfi.FaceName, L"Consolas");
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

	printf("Setting to Raster: press  ");
	getchar();
	cfi.dwFontSize.X = 3;			//大小设置
	cfi.dwFontSize.Y = 5;
	cfi.FontWeight = FW_THIN;
	wcscpy_s(cfi.FaceName, L"Raster");//点阵字体
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

	printf("Setting to Raster: press  ");
	getchar();
	cfi.dwFontSize.X = 8;
	cfi.dwFontSize.Y = 8;
	cfi.FontWeight = FW_THIN;
	wcscpy_s(cfi.FaceName, L"Raster");
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

	printf("Press  to exit");
	getchar();
	return 0;
}

你可能感兴趣的:(c/c++,c++)