DrawTextEx
DrawTextEx(
DC: HDC; {设备句柄}
lpchText: PChar; {文本}
cchText: Integer; {要绘制的字符个数; -1 表示全部}
var p4: TRect; {矩形结构}
dwDTFormat: UINT; {选项}
DTParams: PDrawTextParams {附加参数, 指向一个 DRAWTEXTPARAMS 结构}
): Integer; {返回文本高度}
//dwDTFormat 参数的可选值:
DT_TOP = 0; {顶对齐, 须 or DT_SINGLELINE}
DT_LEFT = 0; {左对齐}
DT_CENTER = 1; {水平居中}
DT_RIGHT = 2; {右对齐}
DT_VCENTER = 4; {垂直居中, 须 or DT_SINGLELINE}
DT_BOTTOM = 8; {底对齐, 须 or DT_SINGLELINE}
DT_WORDBREAK = $10; {给超出边界的单词换行; 如果 SetTextAlign 函数设置了 TA_UPDATECP, 这里的设置无效}
DT_SINGLELINE = $20; {单行文本}
DT_EXPANDTABS = $40; {扩展制表符, 默认制表跨 8 个字符}
DT_TABSTOP = $80; {TAB 后添加的字符数}
DT_NOCLIP = $100; {使文本不限制在指定的矩形内}
DT_EXTERNALLEADING = $200; {返回的高度包括字体的外部行距}
DT_CALCRECT = $400; {自动检测容纳文本的矩形的高度和宽度, 并返回高度, 但文本不被绘制}
DT_NOPREFIX = $800; {不处理前缀字符(&); 默认时, & 后的字符会被添加下划线}
DT_INTERNAL = $1000; {用系统字体来计算文本高度}
DT_HIDEPREFIX = $00100000; {隐藏下划线}
DT_PREFIXONLY = $00200000; {将 & 解释为前缀}
DT_EDITCONTROL = $2000; {如果最后一行只是部分可见, 将不被绘制}
DT_PATH_ELLIPSIS = $4000; {文本过长时, 中间使用 ... 会尽量保留最后一个 \ 后面的字符}
DT_END_ELLIPSIS = $8000; {文本过长时, 尾部使用 ...}
DT_MODIFYSTRING = $10000; {省略显示, 只与 DT_PATH_ELLIPSIS、DT_END_ELLIPSIS 联合使用}
DT_RTLREADING = $20000; {从右到左绘制文本, 只用于相应语种}
DT_WORD_ELLIPSIS = $40000; {截短不符合矩形的正文, 并增加椭圆}
//DRAWTEXTPARAMS 结构是 tagDRAWTEXTPARAMS 的重定义:
tagDRAWTEXTPARAMS = packed record
cbSize: UINT; {结构大小}
iTabLength: Integer; {TAB 占位大小, 以字符平均宽度为单位}
iLeftMargin: Integer; {左边距, 以字符平均宽度为单位}
iRightMargin: Integer; {右边距, 以字符平均宽度为单位}
uiLengthDrawn: UINT; {返回 DrawTextEx 函数绘制的字符个数, 包括空格}
end;
--------------------------------------------------------------------------------
//例1:
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
MyRect: TRect;
str: string;
begin
MyRect.Left := X;
MyRect.Top := Y;
MyRect.Right := X + 200;
MyRect.Bottom := Y + 200;
str := Application.ExeName;
DrawTextEx(
Canvas.Handle,
PChar(str),
-1,
MyRect,
DT_MODIFYSTRING or DT_END_ELLIPSIS,
nil
);
end;
--------------------------------------------------------------------------------
//效果图:
--------------------------------------------------------------------------------
//例2:
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
MyRect: TRect;
str: string;
begin
MyRect.Left := X;
MyRect.Top := Y;
MyRect.Right := X + 200;
MyRect.Bottom := Y + 200;
str := Application.ExeName;
DrawTextEx(
Canvas.Handle,
PChar(str),
-1,
MyRect,
DT_MODIFYSTRING or DT_PATH_ELLIPSIS,
nil
);
end;