這次介紹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() //設置字體背景顏色
下次介紹win32GDI的位圖