特别郁闷,今天打算实现一下单击手机屏幕,更换字符串的功能。其实很简单,只需要覆盖原来字符串打印的位置就行,但是直接打过去原来的位置,系统并不会用原来的背景色刷掉那位置空间再把新的字符串打印过去。没办法,只能通过人工使用背景色刷掉旧的字符串。但是问题又出现,刷背景后,会留下一个黑色的边框线。无奈,这应该是画笔所作,把画笔选为PS_NULL样式,选入dc中,所有问题解决。
附上源码:
1
void
BtnDownTextDraw(HDC hdc)
2
{
3
//
创建字体
4
HFONT hFont, hOldFont;
5
LOGFONT logfont;
6
memset(
&
logfont,
0
,
sizeof
(logfont));
7
logfont.lfWeight
=
FW_BOLD;
8
logfont.lfWidth
=
7
;
9
logfont.lfQuality
=
CLEARTYPE_QUALITY;
10
hFont
=
CreateFontIndirect(
&
logfont);
11
12
//
创建与目标设备dc兼容的内存dc
13
HDC hMemoryDC
=
CreateCompatibleDC(hdc);
14
//
创建设备dc的兼容画布
15
HBITMAP hMemoryBitmap
=
CreateCompatibleBitmap(hdc,FONT_WIDTH,FONT_HIGHT);
16
//
把画布选入内存dc中
17
HBITMAP hOldMemoryBitymap
=
(HBITMAP)SelectObject(hMemoryDC,hMemoryBitmap);
18
19
//
画笔
20
HPEN hOldPen, hPen
=
CreatePen(PS_NULL,
0
,RGB(
21
,
15
,
20
));
21
hOldPen
=
(HPEN)SelectObject(hMemoryDC,hPen);
22
23
HBRUSH hOldBrush, hBrush
=
CreateSolidBrush(RGB(
21
,
15
,
20
));
24
//
填充背景色
25
hOldBrush
=
(HBRUSH)SelectObject(hMemoryDC,hBrush);
26
Rectangle(hMemoryDC,
0
,
0
,FONT_WIDTH,FONT_HIGHT);
27
28
//
打印字符串的位置
29
RECT rtFont
=
{
0
,
0
,FONT_WIDTH,FONT_HIGHT};
30
//
打印文字
31
hOldFont
=
(HFONT)SelectObject(hMemoryDC,hFont);
32
SetBkMode(hMemoryDC,TRANSPARENT);
33
SetTextColor(hMemoryDC,RGB(
255
,
255
,
255
));
34
if
(bZCalibration)
35
{
36
DrawText(hMemoryDC,_T(
"
*****垂直放置****
"
),
-
1
,
&
rtFont,DT_LEFT);
37
}
38
else
39
{
40
DrawText(hMemoryDC,_T(
"
*****水平放置****
"
),
-
1
,
&
rtFont,DT_LEFT);
41
}
42
43
//
内存dc拷贝到设备dc中
44
BitBlt(hdc,FONT_X,FONT_Y,FONT_WIDTH,FONT_HIGHT,hMemoryDC,
0
,
0
,SRCCOPY);
45
46
//
释放字体资源
47
SelectObject(hdc,hOldFont);
48
DeleteObject(hFont);
49
50
SelectObject(hdc,hOldPen);
51
DeleteObject(hPen);
52
53
//
释放画刷资源
54
SelectObject(hdc,hOldBrush);
55
DeleteObject(hBrush);
56
57
//
释放资源
58
SelectObject(hMemoryDC,hOldMemoryBitymap);
59
DeleteObject(hMemoryBitmap);
60
DeleteObject(hOldMemoryBitymap);
61
hMemoryBitmap
=
NULL;
62
hOldMemoryBitymap
=
NULL;
63
DeleteDC(hMemoryDC);
64}