Solved

Determining actual font width and height

Posted on 2000-04-15
5
489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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