1.使用到的函数总结
1. TextOut(): 定点显示字符串
1 | BOOL TextOut(HDC hdc, // 设备描述信息 |
eg.1
2LPCTSTR str = "Hello Word!!!";
TextOut(hdc, 100, 100, str, strlen(str));
2. DrawText(): 矩形框内显示字符串
1 | int DrawText(HDC hdc, // 设备描述信息 |
uFormat支持的类型:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/*
* DrawText() Format Flags
*/
#define DT_TOP 0x00000000
#define DT_LEFT 0x00000000
#define DT_CENTER 0x00000001
#define DT_RIGHT 0x00000002
#define DT_VCENTER 0x00000004
#define DT_BOTTOM 0x00000008
#define DT_WORDBREAK 0x00000010
#define DT_SINGLELINE 0x00000020
#define DT_EXPANDTABS 0x00000040
#define DT_TABSTOP 0x00000080
#define DT_NOCLIP 0x00000100
#define DT_EXTERNALLEADING 0x00000200
#define DT_CALCRECT 0x00000400
#define DT_NOPREFIX 0x00000800
#define DT_INTERNAL 0x00001000
#if(WINVER >= 0x0400)
#define DT_EDITCONTROL 0x00002000
#define DT_PATH_ELLIPSIS 0x00004000
#define DT_END_ELLIPSIS 0x00008000
#define DT_MODIFYSTRING 0x00010000
#define DT_RTLREADING 0x00020000
#define DT_WORD_ELLIPSIS 0x00040000
#if(WINVER >= 0x0500)
#define DT_NOFULLWIDTHCHARBREAK 0x00080000
#if(_WIN32_WINNT >= 0x0500)
#define DT_HIDEPREFIX 0x00100000
#define DT_PREFIXONLY 0x00200000
#endif /* _WIN32_WINNT >= 0x0500 */
#endif /* WINVER >= 0x0500 */
3. SetTextColor():设置文字颜色
1 | // 返回之前的颜色值 |
eg.1
COLORREF old_col = SetTextColor(hdc,RGB(0,255,0));
4. SetBkColor():设置背景颜色
1 | // 返回之前的背景颜色值 |
eg.1
COLORREF old_bcol = SetBkColor(hdc,RGB(0,0,0));
5. SetBkMode():设置背景的模式
1 | int SetMkMode(HDC hdc, |
背景mode | 值 |
---|---|
TRANSPARENT | 1 |
OPAQUE | 2 |
BKMODE_LAST | 2 |
2.使用流程
1.SetTextColor/SetBkColor/SetBkMode都会返回设置之前的值。目的是方便恢复到之前的设置。
理解为绘制管线的设置
2.流程
- 1.GetDC
- 2.设置绘制管线
- 3.绘制文字
- 4.恢复绘制管线设置
- 5.RleaseDC
3.demo code:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18COLORREF old_fcolor,old_bcolor;
int old_bkmode;
HDC hdc=GetDC(hWnd);
old_fcolor=SetTextColor(hdc,RGB(0,255,0));
old_bcolor=SetBkColor(hdc,RGB(0,0,0));
old_bkmode=SetBkMode(hdc,TRANSPARENT);
// 绘制文字
TextOut(hdc,20,30,"Hello!",strlen("Hello!"));
// 恢复设置
SetTextColor(hdc,old_fcolor);
SetBkColor(hdc,old_bcolor);
SetBkMode(hdc,old_bkmode);
Release(hWnd,hdc);