Date: 02-16-2022
Return to Index
created by gbSnippets
'In figuring out how many lines can fit in a space, you need to know
'the line-to-line dimension of a font. Here are two ways to calculate
'the information.
'Primary Code:
'This function prints two lines and check the difference between the
'starting and ending position.
Function LineToLine As Single
'assumes Graphic Attach has been used, works on currently assigned font
Local x,y,m,n As Single
Graphic Get Pos To x,y
Graphic Print "ABC"
Graphic Get Pos To m,n
Function = n-y
End Function
'This function uses the GetTextMetrics API to get info about the metric.
'The line-to-line dimensions is the sum of .tmExternalLeading and .tmHeight.
Function LineToLineGTM As Single
'assumes Graphic Attach has been used, works on currently assigned font
Local tmi As TextMetric, hDC As DWord
Graphic Get DC To hDC
GetTextMetrics hDC, tmi
Function = tmi.tmExternalLeading + tmi.tmHeight
End Function
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
%IDC_Graphic = 503
Function PBMain() As Long
Local Style As Long
Dialog New Pixels, 0, "Font Height Test",300,300,300,310, %WS_OverlappedWindow To hDlg
Control Add Graphic, hDlg, %IDC_Graphic, "", 20, 40, 250, 260, %WS_Border
Graphic Attach hDlg, %IDC_Graphic
Graphic Font "Bauhaus 93", 10, 0
Graphic Clear
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_InitDialog
'get line height by testing POS before/after Graphic Print
MsgBox "Using Graphic Pos" + Str$(LineToLine)
'get line height using GetTextMetric
MsgBox "Using GetTextMetric" + Str$(LineToLineGTM)
'get line height with Graphic Chr
Local m,n As Single
Graphic Chr Size To m,n
MsgBox "Using Graphic Chr" + Str$(n)
End Select
End Function
Function LineToLine As Single
'assumes Graphic Attach has been used, works on currently assigned font
Local x,y,m,n As Single
Graphic Get Pos To x,y
Graphic Print "ABC"
Graphic Get Pos To m,n
Function = n-y
End Function
Function LineToLineGTM As Single
'assumes Graphic Attach has been used, works on currently assigned font
Local tmi As TextMetric, hDC As DWord
Graphic Get DC To hDC
GetTextMetrics hDC, tmi
Function = tmi.tmExternalLeading + tmi.tmHeight
End Function
'gbs_00555
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm