1.文字的绘制
TextOut - 将文字绘制在指定坐标位置
int DrawText( HDC hDC, //DC句柄
LPCTSTR lpString, //字符串
int nCount, //字符个数
LPRECT lpRect, //绘制文字的矩形区域
UINT uFormat); //绘制方式
BOOL ExtTextOut( HDC hdc, //DC句柄
int X, //输出X位置
int Y, //输出Y位置
UINT fuOptions, //输出选项
CONST RECT *lprc, //输出的矩形
LPCTSTR lpString, //字符串
UINT cbCount, //字符个数
CONST INT *lpDx); //字符间距的数组
2.文字颜色和背景
文字颜色
COLORREF SetTextColor( HDC hdc, COLORREF crColor);
文字背景色
COLORREF SetBkColor( HDC hdc, COLORREF crColor);
文字背景模式
int SetBkMode(
HDC hdc, int iBkMode);
3.字体
windows常用的字体格式为TureType字体
字体名 - 表示字体类型
HFONT - 字体句柄
(1)创建字体
HFONT CreateFont( int nHeight, //字体高度
int nWidth, //字体宽度
int nEscapement, //字符串倾斜角度
int nOrientation, //字符串旋转角度
int fnWeight, //字体的粗细
DWORD fdwItalic, //斜体
DWORD fdwUnderline //字符下划线
DWORD fdwStrikeOut, //删除线
DWORD fdwCharSet, //字符集
DWORD fdwOutputPrecision, //输出精度
DWORD fdwClipPrecision, //剪切精度
DWORD fdwQuality, //输出质量
DWORD fdwPitchAndFamily, //匹配字体
LPCTSTR lpszFace); //字体名称
(2)应用字体到DC
SelectObject
(3)绘制文字
DrawText/TextOut
ExtTextOut
(4)取出字体
SelectObject
(5)删除字体
DeleteObject
相关示例代码:

#include "stdafx.h" HINSTANCE g_hInstance = 0; //接收当前程序实例句柄 void OnPaint(HWND hWnd) { char szText[] = "Hello&Text"; PAINTSTRUCT ps = { 0 }; HDC hdc = BeginPaint(hWnd, &ps); SetTextColor(hdc, RGB(255, 0, 0)); SetBkColor(hdc, RGB(0, 255, 0));//只适用于OPAQUE模式 SetBkMode(hdc, TRANSPARENT); HFONT hFont = CreateFont(30, 0, 45, 0, 900, 1, 1, 1, GB2312_CHARSET, 0, 0, 0, 0, "黑体"); HGDIOBJ nOldFont = SelectObject(hdc, hFont); TextOut(hdc, 100, 100, szText, strlen(szText)); RECT rc = { 0 }; rc.left = 100; rc.top = 150; rc.right = 200; rc.bottom = 200; Rectangle(hdc, 100, 150, 200, 200); /* DT_VCENTER DT_BOTTOM - 只使用DT_SINGLELINE DT_WORDBREAK - (多行)字符串只能靠顶端绘制 */ DrawText(hdc, szText, strlen(szText), &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOCLIP | DT_NOPREFIX); char szExtText[] = "A中国人民"; int nDis[] = {10, 0, 20, 0, 30, 0, 40, 0}; ExtTextOut(hdc, 100, 300, 0, NULL, szExtText, strlen(szExtText), nDis); SelectObject(hdc, nOldFont); DeleteObject(hFont); EndPaint(hWnd, &ps); } //窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: OnPaint(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, msg, wParam, lParam); } //注册窗口类 BOOL Register(LPSTR lpClassName, WNDPROC wndProc) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof(wce); wce.cbClsExtra = 200; wce.cbClsExtra = 200; wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInstance; wce.lpfnWndProc = wndProc; wce.lpszClassName = lpClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW | CS_VREDRAW; ATOM nAtom = RegisterClassEx(&wce); if (0 == nAtom) { return FALSE; } return TRUE; } //创建主窗口 HWND CreateMainWindow(LPSTR lpClassName, LPSTR lpWndName) { HWND hWnd = CreateWindowEx(0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL); return hWnd; } //显示窗口 void Display(HWND hWnd) { ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); } //消息循环 void Message() { MSG nMsg = { 0 }; while (GetMessage(&nMsg, NULL, 0, 0)) { TranslateMessage(&nMsg); DispatchMessage(&nMsg); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; Register("Main", WndProc); HWND hWnd = CreateMainWindow("Main", "window"); Display(hWnd); Message(); return 0; }
运行结果: