Dotcpp  >  编程教程  >  C语言使用EasyX绘制文本  >  如何设置字体风格?

如何设置字体风格?

点击打开在线编译器,边学边练

大家在想自由输出不同样子的字体的时候一定思考过,比如能不能输出更大的字?其他字体的字? 字的样式等等,这些可以吗?

在easyX里是完全可以的,需要用到settextstyle函数,settextstyle函数被函数重载为多种函数形式,在easyX头文件中全部定义如下:

void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace);
void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut);
void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut, BYTE fbCharSet, BYTE fbOutPrecision, BYTE fbClipPrecision, BYTE fbQuality, BYTE fbPitchAndFamily);
void settextstyle(const LOGFONT *font);	// Set current text style
void gettextstyle(LOGFONT *font);		// Get current text style

文教程将重点介绍最为常用的第一种方式,其余更多参数大家可以参考easyX官方使用手册

void settextstyle(int nHeight, int nWidth, LPCTSTR lpszFace);

此处settextstyle有三个参数,意义分别为:

int nHeight:表示字体的高度,即控制了字的大小

int nWidth :表示字符串的宽度,一般我们可以输入0,表示自适应,以防止字体扭曲变形

LPCTSTR lpszFace:以何种字体显示


如我们想以高为50的字体输出字符串“www.dotcpp.com”,以Consolas字体输出,则代码为:

//www.dotcpp.com
#include <graphics.h>        // 引用图形库头文件
#include <conio.h>
int main()
{

    initgraph(640, 480);   // 创建绘图窗口,大小为 640x480 像素
    setcolor(LIGHTBLUE);
    settextstyle(50,0,"Consolas");
    outtextxy(100,100,"www.dotcpp.com");
    getch();            // 按任意键继续
    closegraph();           // 关闭绘图窗口
    return 0;
}

大家注意,我们还是使用亮蓝色,并且都要在outtextxy执行之前设定好这些,就可以起作用,效果为:

控制字体样式效果


这里字体样式非常多,大家可以使用常见的,黑体、楷体、宋体、行楷、微软雅黑、仿宋等等都可以,大家可以看自己电脑C:\Windows\Fonts里的所有ttf文件的名字都可以使用


本文固定URL:https://www.dotcpp.com/course/1199

C语言图形编程
第一章 easyX图形编程入门
第二章 C语言用easy画图
第三章 C语言使用EasyX绘制文本
第四章 C语言使用EasyX图形处理
第五章 EasyX项目实战
Dotcpp在线编译      (登录可减少运行等待时间)