`
yuanjinxiu
  • 浏览: 659993 次
文章分类
社区版块
存档分类
最新评论

Windows API一日一练(24)DrawText函数

 
阅读更多
DrawText函数与前面介绍的TextOut函数都是文本输出函数,但它们是有区别的。DrawText函数是格式化输出函数,而TextOut函数不具备这样的功能。因而DrawText函数比TextOut函数功能强大,可以让文本输出时左对齐,或者右对齐,或者中间对齐,还可以让文本适应输出矩形内,如果超出时可以截断,或者显示为省略号的方式。DrawText函数在表格方式显示时肯定要使用到的函数。
函数DrawText声明如下:
WINUSERAPI
int
WINAPI
DrawTextA(
__in HDC hdc,
__inout_ecount(cchText) LPCSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT format);
WINUSERAPI
int
WINAPI
DrawTextW(
__in HDC hdc,
__inout_ecount(cchText) LPCWSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT format);
#ifdef UNICODE
#define DrawTextDrawTextW
#else
#define DrawTextDrawTextA
#endif // !UNICODE
hdc是当前设备的句柄。
lpchText是输出文本的缓冲区首地址。
cchText是输出文本的字符个数。
lprc是输出的显示区域。
format是用什么格式输出。
调用这个函数的例子如下:
#001//
#002//界面显示输出.
#003//
#004//蔡军生2007/08/27 QQ:9073204 深圳
#005//
#006void CCaiWinMsg::OnDraw(HDC hDC)
#007{
#008//
#009std::wstring strShow(_T("C++窗口类的实现,2007-08-27"));
#010TextOut(hDC,10,10,strShow.c_str(),(int)strShow.length());
#011
#012//设置输出字符串的颜色.
#013COLORREF crOld = SetTextColor(hDC,RGB(255,0,0));
#014
#015RECT rcText;
#016
#017//显示不全.
#018rcText.left = 10;
#019rcText.top = 30;
#020rcText.right = 100;
#021rcText.bottom = 50;
#022
#023DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#024 DT_LEFT|DT_SINGLELINE|DT_END_ELLIPSIS);
#025
#026//完全显示,左对齐.
#027 rcText.left = 10;
#028rcText.top = 50;
#029rcText.right = 300;
#030rcText.bottom = 80;
#031
#032DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#033 DT_LEFT|DT_SINGLELINE|DT_END_ELLIPSIS);
#034
#035
#036SetTextColor(hDC,RGB(0,0,255));
#037//完全显示,右对齐.
#038rcText.left = 10;
#039rcText.top = 80;
#040rcText.right = 300;
#041rcText.bottom = 110;
#042
#043strShow = _T("A&bcd");
#044DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#045 DT_RIGHT|DT_SINGLELINE|DT_END_ELLIPSIS);
#046
#047
#048//
#049SetTextColor(hDC,crOld);
#050}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics