当前位置:主页   - 电脑 - 程序设计 - VB
Windows API一日一练(24)DrawText函数
来源:网络   作者:蔡军生   更新时间:2011-08-04
收藏此页】    【字号    】    【打印】    【关闭

  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 DrawText DrawTextW
#else
#define DrawText DrawTextA
#endif // !UNICODE

  hdc是当前设备的句柄。

  lpchText是输出文本的缓冲区首地址。

  cchText是输出文本的字符个数。

  lprc是输出的显示区域。

  format是用什么格式输出。

  调用这个函数的例子如下:

#001 //
#002 //界面显示输出.
#003 //
#004 //蔡军生 2007/08/27 QQ:9073204 深圳
#005 //
#006 void CCaiWinMsg::OnDraw(HDC hDC)
#007 {
#008  //
#009  std::wstring strShow(_T("C++窗口类的实现,2007-08-27"));
#010  TextOut(hDC,10,10,strShow.c_str(),(int)strShow.length());    
#011 
#012  //设置输出字符串的颜色.
#013  COLORREF crOld = SetTextColor(hDC,RGB(255,0,0));
#014 
#015  RECT rcText;
#016 
#017  //显示不全.
#018  rcText.left = 10;
#019  rcText.top = 30;
#020  rcText.right = 100;
#021  rcText.bottom = 50;
#022 
#023  DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#024        DT_LEFT|DT_SINGLELINE|DT_END_ELLIPSIS);
#025 
#026  //完全显示,左对齐.
#027   rcText.left = 10;
#028  rcText.top = 50;
#029  rcText.right = 300;
#030  rcText.bottom = 80;
#031 
#032  DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#033         DT_LEFT|DT_SINGLELINE|DT_END_ELLIPSIS);
#034 
#035  
#036  SetTextColor(hDC,RGB(0,0,255));
#037  //完全显示,右对齐.
#038  rcText.left = 10;
#039  rcText.top = 80;
#040  rcText.right = 300;
#041  rcText.bottom = 110;
#042 
#043  strShow = _T("A&bcd");
#044  DrawText(hDC,strShow.c_str(),(int)strShow.length(),&rcText,
#045         DT_RIGHT|DT_SINGLELINE|DT_END_ELLIPSIS);
#046 
#047 
#048  //
#049  SetTextColor(hDC,crOld);
#050 }

编缉推荐阅读以下文章

  • Windows API一日一练(44)wsprintf函数
  • Windows API一日一练(43)WaitForSingleObject函数
  • Windows API一日一练(42)CreateThread函数
  • Windows API一日一练(41)FindWindowEx函数
  • Windows API一日一练(40)CreateRectRgn和CombineRgn函数
  • Windows API一日一练(39)AnimateWindow函数
  • Windows API一日一练(38)SetWindowPos函数
  • Windows API一日一练(37)MoveWindow函数
  • Windows API一日一练(36)SetWindowText函数
  • Windows API一日一练(35)OutputDebugString函数
其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明