Solved

Determining actual font width and height

Posted on 2000-04-15
5
440 Views
Last Modified: 2013-12-03
I want to print some text on my form. I need to be able to measure the height and width in twips of the text. Eg, if I have the string "Hello", I need to be able to measur the height from the top of the text to the bottom, and the width from left to right. This is so I can center it in a fixed area, and also so I can determine wether the point size needs decreasing to fit. I know how to do this in Java using the FontMetrics class, but how can I do this in VB?
0
Comment
Question by:Arachn1d
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2719693
Private Type POINTAPI
    X As Long
    Y As Long
End Type
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ExtTextOut Lib "gdi32" Alias "ExtTextOutA" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal wOptions As Long, ByVal lpRect As Any, ByVal lpString As String, ByVal nCount As Long, lpDx As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Pt As POINTAPI, mWnd As Long, WR As RECT, nDC As Long
    Dim TextSize As POINTAPI, CX As Long, CY As Long
    'Get the current cursor position
    GetCursorPos Pt
    'Get the window under the cursor
    mWnd = WindowFromPoint(Pt.X, Pt.Y)
    'Get the window's position
    GetWindowRect mWnd, WR
    'Get the window'zs device context
    nDC = GetWindowDC(mWnd)
    'Get the height and width of our text
    GetTextExtentPoint32 nDC, "Hello !", Len("Hello !"), TextSize
    For CX = 1 To WR.Right - WR.Left Step TextSize.X
        For CY = 1 To WR.Bottom - WR.Top Step TextSize.Y
            'Draw the text on the window
            ExtTextOut nDC, CX, CY, 0, ByVal 0&, "Hello !", Len("Hello !"), ByVal 0&
        Next
    Next
End Sub
Private Sub Form_Paint()
    Me.CurrentX = 0
    Me.CurrentY = 0
    Me.Print "Click on this form," + vbCrLf + "Hold the mouse button," + vbCrLf + "drag the mouse over another window," + vbCrLf + "release the mouse button" + vbCrLf + "and see what happens!"
End Sub

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2719703
bad example but the idea is to use the gettextextentpoint32 api
the gettextextentpoint32 function computes the width and height of the specified string of text

Public Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
0
 
LVL 3

Accepted Solution

by:
Foyal earned 50 total points
ID: 2719724
These VB Functions will do what you need without any trouble at all. Once you have the width and height, you can use those values to figure everything else out.

Sub TestText()

    Dim S As String

    S = "Test String"

    Debug.Print "Width = " & TextWidth(S)
    Debug.print "Height = " & TextHeight(S)

End Sub

0
 

Author Comment

by:Arachn1d
ID: 2719903
Thanks! This is just what I was looking for. Sorry, AzraSound, but this fits the bill exactly.
0
 
LVL 3

Expert Comment

by:Foyal
ID: 2721569
Glad I could help...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now