文本 API

函数 意义
CreateFont(...) 返回建立的字体对象
CreateFontIndirect(LogFont) 返回建立的逻辑字体对象
TextOut(dc,left,top,string,cnt) 在指定位置打印文本,返回是否成功
SetTextColor(dc,color) 设置文本颜色,返回原来颜色值
DrawText(dc,string,cnt,Rect,Format) 在指定区域打印文本,成功返回文本高度
DrawTextEx(dc,string,cnt,Rect,Format,Params) 在指定区域打印文本
GetCharWidth(...) 取字符宽度
GetCharWidth32(...) 取字符宽度
GetCharWidthFloat(...) 取字符宽度
GetCharABCWidths(...) 取字符宽度
GetCharABCWidthsFloat(...) 取字符宽度
GetTextExtentPoint(...) 取字符串打印宽高度

CreateFont 字体

CreateFont(height, width, Escapement, Orientation, weight, italic, unerline, strikeout, charset, outputprecision, clipprecision, Quality, PitchAndFamily ,Face)
参数 意义
Height 字体逻辑高度
Width 字体宽高比,100 为方形
Escapement 整体倾斜度,单位 0.1 度
Orientation  
Weight 字体粗细,0-900,间隔 100,普通 400,加粗 700
Italic 是否斜体
Underline 是否下划线
StrikeOut 是否中划线
CharSet 字符集,中文为 GB2312_CHARSET
OutputPrecision 输出精度
ClipPrecision 裁剪精度
Quality  
PitchAndFamily  
Face  

DrawText 显示文字

支持热键功能,如“确定(&O)”,显示为“确定(O)”。

DrawText(dc,string,cnt,Rect,Format)
参数 意义
string 字符串
count 字符串长度,若长度设为 -1,应以零字符终止
Rect 绘制文字的矩形区域
Format 格式描述

DrawTextEx 字体

DrawTextEx(dc,string,count,Rect,Format,Params)
参数 意义
string 字符串
count 字符串长度,若长度设为 -1,应以零字符终止
Rect 绘制文字的矩形区域
Format 复合参数
Params 选项结构

Format 常量

常量 意义
DT_BOTTOM 文本对齐矩形底部,须与 DT_SINGLELINE 联合使用
DT_CALCRECT 根据文本调整矩形大小,并从参数返回

单行自动向右扩展
多行自动向下扩展

DT_CENTER 文本水平居中
DT_EDITCONTROL Duplicates the text-displaying characteristics of a multiline edit control. Specifically, the average character width is calculated in the same manner as for an edit control, and the function does not display a partially visible last line.
DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.

You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.

DT_EXPANDTABS 遇制表符时,对齐到制表位
DT_EXTERNALLEADING 计算文本行高度的时候,使用当前字体的外部间距属性
DT_INTERNAL Uses the system font to calculate text metrics.
DT_LEFT 文本左对齐
DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.
DT_NOCLIP 不剪切超出矩形范围的文本
DT_NOPREFIX 关闭热键功能,即字符 & 后的字符加下划线
DT_RIGHT 文本右对齐
DT_RTLREADING Layout in right to left reading order for bi-directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left to right.
DT_SINGLELINE 文本于单行绘制,不进行换行
DT_TABSTOP 指定制表站间距,存储于高位(8-15)
DT_TOP 文本顶部对齐,须与 DT_SINGLE 联合使用
DT_VCENTER 文本垂直居中,须与 DT_SINGLE 联合使用
DT_WORDBREAK Breaks words. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by the lpRect parameter. A carriage return-linefeed sequence also breaks the line.
DT_WORD_ELLIPSIS Truncates text that does not fit in the rectangle and adds ellipses.

Params 结构

Public Type DRAWTEXTPARAMS
    cbSize As Long
    iTabLength As Long
    iLeftMargin As Long
    iRightMargin As Long
    uiLengthDrawn As Long
End Type
成员 意义
Size 存储结构本身大小
TabLength 制表位尺寸
LeftMargin 左补丁
RightMargin 右补丁
LengthDrawn 返回绘制的字符串长度,含空格

LogFont 结构

Public Type LOGFONT
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As Byte
    lfUnderline As Byte
    lfStrikeOut As Byte
    lfCharSet As Byte
    lfOutPrecision As Byte
    lfClipPrecision As Byte
    lfQuality As Byte
    lfPitchAndFamily As Byte
    lfFaceName(1 To LF_FACESIZE) As Byte
End Type
属性 意义
所有属性 CreateFont 的参数

GetCharWidth 取字符宽度
GetCharWidth32 取字符宽度
GetCharWidthFloat 取字符宽度
GetCharABCWidths 取字符宽度
GetCharABCWidthsFloat 取字符宽度

GetCharWidth(dc, FirstChar, LastChar, bufLong)
GetCharWidth32(dc, FirstChar, LastChar, bufLong)
GetCharWidthFloat(dc, FirstChar, LastChar, BufDouble)
GetCharABCWidths(dc, FirstChar, LastChar, ABC)
GetCharABCWidthsFloat(dc, FirstChar, LastChar, ABCFLOAT)
参数 意义
dc 场景
FirstChar 开始 ASCII 值
LastChar 结束 ASCII 值
bufLong 整型数组,存储各字符宽度值
bufDouble 浮点数组,存储各字符宽度值
ABC ABC 结构数组地址
ABCFLOAT ABCFLOAT 结构数组地址

ABC 结构
ABCFLOAT 结构

Public Type ABC
    abcA As Long
    abcB As Long
    abcC As Long
End Type
Public Type ABCFLOAT
    abcfA As Double
    abcfB As Double
    abcfC As Double
End Type
参数 意义
abcA,abcfA 字符左空宽度
abcB,abcfB 字符实际宽度
abcC,abcfC 字符右空宽度

GetTextExtentPoint 取字符串打印宽高度

function GetTextExtentPoint(DC: HDC; Str: PChar; Count: Integer; var Size: TSize): BOOL
参数 意义
Size 结构地址

packed record
cx: Longint;
cy: Longint;
end;

返回值 0 失败