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是用什么格式输出。
调用这个函数的例子如下:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bmp: TBitmap;
OutRect: TRect;
begin
with DBGrid1 do
begin
Canvas.FillRect(Rect);
OutRect := Rect;
InflateRect(OutRect, -2, -2);
if Column.Field is TGraphicField then
begin
bmp := TBitmap.Create;
try
bmp.Assign(Column.Field);
Canvas.StretchDraw(OutRect, Bmp);
finally
bmp.Free;
end;
end
else if Column.Field is TMemoField then
begin
DrawText(Canvas.Handle, Pchar(Column.Field.AsString),
Length(Column.Field.AsString), OutRect, dt_WordBreak or dt_NoPrefix);
end
else
DrawText(Canvas.Handle, Pchar(Column.Field.DisplayText),
Length(Column.Field.DisplayText),
OutRect, dt_WordBreak or dt_NoPrefix);
end;
end;
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 }
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/caimouse/archive/2007/08/27/1761276.aspx
DrawText(IntegerValue,R,DT_RIGHT or DT_SINGLELINE or FormatView_V)
const
{ DrawText() Format Flags }
{$EXTERNALSYM DT_TOP}
DT_TOP = 0;
{$EXTERNALSYM DT_LEFT}
DT_LEFT = 0;
{$EXTERNALSYM DT_CENTER}
DT_CENTER = 1;
{$EXTERNALSYM DT_RIGHT}
DT_RIGHT = 2;
{$EXTERNALSYM DT_VCENTER}
DT_VCENTER = 4;
{$EXTERNALSYM DT_BOTTOM}
DT_BOTTOM = 8;
{$EXTERNALSYM DT_WORDBREAK}
DT_WORDBREAK = $10;
{$EXTERNALSYM DT_SINGLELINE}
DT_SINGLELINE = $20;
{$EXTERNALSYM DT_EXPANDTABS}
DT_EXPANDTABS = $40;
{$EXTERNALSYM DT_TABSTOP}
DT_TABSTOP = $80;
{$EXTERNALSYM DT_NOCLIP}
DT_NOCLIP = $100;
{$EXTERNALSYM DT_EXTERNALLEADING}
DT_EXTERNALLEADING = $200;
{$EXTERNALSYM DT_CALCRECT}
DT_CALCRECT = $400;
{$EXTERNALSYM DT_NOPREFIX}
DT_NOPREFIX = $800;
{$EXTERNALSYM DT_INTERNAL}
DT_INTERNAL = $1000;
{$EXTERNALSYM DT_HIDEPREFIX}
DT_HIDEPREFIX = $00100000;
{$EXTERNALSYM DT_PREFIXONLY}
DT_PREFIXONLY = $00200000;
{$EXTERNALSYM DT_EDITCONTROL}
DT_EDITCONTROL = $2000;
{$EXTERNALSYM DT_PATH_ELLIPSIS}
DT_PATH_ELLIPSIS = $4000;
{$EXTERNALSYM DT_END_ELLIPSIS}
DT_END_ELLIPSIS = $8000;
{$EXTERNALSYM DT_MODIFYSTRING}
DT_MODIFYSTRING = $10000;
{$EXTERNALSYM DT_RTLREADING}
DT_RTLREADING = $20000;
{$EXTERNALSYM DT_WORD_ELLIPSIS}
DT_WORD_ELLIPSIS = $40000;