如何计算不同字体在DataWindow中的显示宽度

catf1sh 2007-09-22 10:45:40
我想写一个函数,传入字符串、字体、字体大小、字体类型,传出像素值。
该怎么写,需要调用什么API函数?谢谢!
...全文
125 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wu_07 2007-09-25
  • 打赏
  • 举报
回复
完整的控件可以在这里下载:

http://www.tiantiansoft.com/bbs/fileshow.asp?boardid=3&id=77
wu_07 2007-09-25
  • 打赏
  • 举报
回复
type logfont from structure
long lfHeight
long lfWidth
long lfEscapement
long lfOrientation
long lfWeight
character lfItalic
character lfUnderline
character lfStrikeOut
character lfCharSet
character lfOutPrecision
character lfClipPrecision
character lfQuality
character lfPitchAndFamily
string lfFaceName
end type

type textsize from structure
long l_cx
long l_cy
end type

FUNCTION ulong GetDC(ulong hwnd) LIBRARY "user32.dll"
FUNCTION ulong ReleaseDC(ulong hwnd,ulong hdc) LIBRARY "user32.dll"
FUNCTION ulong SelectObject(ulong hdc,ulong hObject) LIBRARY "gdi32.dll"
FUNCTION ulong DeleteObject(ulong hObject) LIBRARY "gdi32.dll"
FUNCTION ulong CreateFontIndirect(ref LOGFONT lpLogFont) LIBRARY "gdi32.dll" ALIAS FOR "CreateFontIndirectA"
FUNCTION ulong GetTextExtentExPoint(ulong hdc,ref string lpszStr,ulong cchString,ulong nMaxExtent,ref ulong lpnFit,ref ulong alpDx,ref textSIZE lpSize) LIBRARY "gdi32.dll" ALIAS FOR "GetTextExtentExPointA"
FUNCTION ulong GetTextExtentPoint32(ulong hdc,ref string lpsz,ulong cbString,ref textSIZE lpSize) LIBRARY "gdi32.dll" ALIAS FOR "GetTextExtentPoint32A"
Function long MulDiv (long nNumber, long nNumerator, long nDenominator) Library "KERNEL32.DLL"
FUNCTION ulong GetDeviceCaps(ulong hdc,ulong nIndex) LIBRARY "gdi32.dll"
wu_07 2007-09-25
  • 打赏
  • 举报
回复
//根据列名和文本,得到文本的显示宽度
//得到字体相关信息
Int li_charset
li_charset = Integer(idw_requestor.Describe(as_colName+".Font.CharSet"))
Int li_Escapement
li_Escapement = Integer(idw_requestor.Describe(as_colName+".Font.Escapement"))
String ls_Face
ls_Face = idw_requestor.Describe(as_colName+".Font.Face")
Int li_Family
li_Family = Integer(idw_requestor.Describe(as_colName+".Font.Family"))
Int li_height
li_height = Integer(idw_requestor.Describe(as_colName+".Font.Height"))
Int li_Italic
li_Italic = Integer(idw_requestor.Describe(as_colName+".Font.Italic"))
Int li_Pitch
li_Pitch = Integer(idw_requestor.Describe(as_colName+".Font.Pitch"))
Int li_Strikethrough
li_Strikethrough = Integer(idw_requestor.Describe(as_colName+".Font.Strikethrough"))
Int li_Underline
li_Underline = Integer(idw_requestor.Describe(as_colName+".Font.Underline"))
Int li_Weight
li_Weight = Integer(idw_requestor.Describe(as_colName+".Font.Weight"))
Int li_Width
li_Width = Integer(idw_requestor.Describe(as_colName+".Font.Width"))


Long ll_newFont,ll_oldFont,ll_hdc
LOGFONT lst_Font

lst_Font.lfWeight = li_Weight
lst_Font.lfWidth = li_Width
If li_Italic = 1 Then
lst_Font.lfItalic = Char(255)
Else
lst_Font.lfItalic = Char(0)
End If
If li_Underline = 1 Then
lst_Font.lfUnderline = Char(1)
Else
lst_Font.lfUnderline = Char(0)
End If
If li_Strikethrough = 1 Then
lst_Font.lfStrikeOut = Char(1)
Else
lst_Font.lfStrikeOut = Char(0)
End If
//DEFAULT_CHARSET
lst_Font.lfCharSet = Char(li_charset)
lst_Font.lfOutPrecision = Char(0)
lst_Font.lfClipPrecision = Char(0)
lst_Font.lfQuality = Char(0)
lst_Font.lfPitchAndFamily = Char(0)
lst_Font.lfFaceName = ls_Face

ll_hdc = getdc(Handle(idw_requestor))
//以点为大小单位的字体转变成设备所需要的恰当的逻辑大小
//LOGPIXELSY=90
//muldiv : abs(li_Height)*getdevicecaps(ll_hdc,90)/72
lst_Font.lfHeight = - muldiv(Abs(li_height),getdevicecaps(ll_hdc,90),72)
//用指定的属性创建逻辑字体
ll_newFont = CreateFontIndirect(lst_Font)
//选入
ll_oldFont = SelectObject(ll_hdc,ll_newFont)

TextSize lstr_Size
//判断字串的大小
GetTextExtentpoint32(ll_hdc, as_text, Len(as_text), lstr_Size )
//恢复
SelectObject(ll_hdc,ll_oldFont)
//释放资源
DeleteObject(ll_newFont)
ReleaseDC(Handle(idw_requestor),ll_hdc)

//返回宽度(unit单位)
Return PixelsToUnits(lstr_Size.l_cx,XPixelsToUnits!)

catf1sh 2007-09-23
  • 打赏
  • 举报
回复
迷人知道吗?

680

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder API 调用
社区管理员
  • API 调用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧