这次介绍win GDI的文字部分,开发环境vs2013,语言C/C++,使用的是win32项目。
win32GDI字体
win32GDI字体的输出跟画笔很类似
首先我们要定义字体句柄,然后可以通过两种模式来获得字体对象
1.获得系统预设字体对象 : HFONT hfont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
2.自己创建字体对象 HFONT hfont = CreateFont();
我们主要介绍第二种
CreatFont()函数参数如下
HFONT CreateFont( int nHeight, // height of font int nWidth, // average character width int nEscapement, // angle of escapement int nOrientation, // base-line orientation angle int fnWeight, // font weight DWORD fdwItalic, // italic attribute option DWORD fdwUnderline, // underline attribute option DWORD fdwStrikeOut, // strikeout attribute option DWORD fdwCharSet, // character set identifier DWORD fdwOutputPrecision, // output precision DWORD fdwClipPrecision, // clipping precision DWORD fdwQuality, // output quality DWORD fdwPitchAndFamily, // pitch and family LPCTSTR lpszFace // typeface name);
我们填上具体的参数来详细说明每个参数的意思
HFONT hfont = CreateFont(140,//字体的高度
// 0,//如果为0,会根据高度自动匹配,如果有值,将用值做宽度
// 00,//文字倾斜角度,10代表倾斜1度
// 0,//字体基线的倾斜角度
// 500,//字体的粗细,400以下为细体字,700以上为粗体字
// false,//是否是斜体
// true,//是否有下划线
// false,//是否有水平线
// GB2312_CHARSET,//字符集
// 0,//字体输出精度
// 0,//裁剪字体的方式
// 0,//字体质量
// 0,//字体系列和字体宽度的设定
// _T("楷体")//字样名 字样名的选择和字符集有关,要先有字符集,才能选择字符集有的字样名
// );示例代码:
case WM_LBUTTONDOWN: { hdc = GetDC(hWnd); HFONT hFont = CreateFont(40, 0, 0, 0, 400, false, false, false, ANSI_CHARSET, 0, 0, 0, 0, _T("楷体")); SelectObject(hdc, hFont); SetBkColor(hdc, 0x000000ff); SetBkMode(hdc, TRANSPARENT); TextOut(hdc, 100, 100, _T("你好吗!"), 4); DeleteObject(hFont); ReleaseDC(hWnd, hdc); } break;
运行结果
可以通过使用
SetTextColor(hdc, 0x0000ff); 设置字体颜色
SetBkMode(hdc, TRANSPARENT);//设置背景模式SetBkColor() //设置字体背景颜色