Windows API一日一練(24)DrawText函數

來源:互聯網
上載者:User
 

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 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.