Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312

    Unanswered: Measure Text Height

    I am looking for some help measuring text. I would like to be able to determine how tall the text is in a given text box. It can be 1 to 6 lines tall and the font size and weight can be changed. Does anyone know of an API call that can do this? Or any other way?

    I have a solution from another programmer but it is not very slick. I have to record the font name, weight and height in a table and look it up and then count the number of carriage return, linefeeds. Too many tables, code, etc.

  2. #2
    Join Date
    Apr 2004
    Location
    outside the rim
    Posts
    1,011
    did u try setting the field to "can grow" and grabbing the height on_current? perhaps make a hidden report with just that field and open it on that record to get it to grow (or shrink) as desired?

    don't know if it will work, just thinking out loud ...

    tc

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I can't remember whether there has ever been a good solution but I've seen the odd post that are variations on this over the years and I know the answer is somewhere between bloody hard and not actually possible.

    Unless it is a Must Have or someone comes up with an answer I would walk away from this one if at all possible.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    tcace, the problem with Can Grow is that the rest of the report shifts as the text box grows.

    Pootle, I have been using the existing solution for several years and I am going to be taking advantage of some new features in 2002 and I thought while I was changing all my reports I would try to improve all aspects of my reports.

    Another possibility (also probably an impossibility) is a bottom justified textbox. If I could justify the text in the text box to the bottom of the textbox that would also help. However, I have not seen anything in Access for that.

  5. #5
    Join Date
    Apr 2004
    Location
    outside the rim
    Posts
    1,011
    DCKunkle, I was thinking that the code you are using that requires knowing the height of the text would open a seperate, single field report during execution to gather the size information. You would then use that to format the actual report on the fly.

    Access is a bit tricky with the things you can format "on the fly" with a report, but there is a lot of lattitude if you have the patience to run through iterations.

    tc

  6. #6
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    i looked into the API side from the original question. all i found is a useless-yes (assuming all text is on a single line you can API-find height & width) and a practical-no (when restricting width (thus forcing line-breaks)).
    doesn't mean there is no API solution - only that i didn't find one.

    with the greatest possible respect: why would you want to do this?
    expressed differently: assuming you could magic the answer - what then?

    izy
    currently using SS 2008R2

  7. #7
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    Here is the long explanation...

    We use Access to print direct mail pieces. In the US, if you are sending mail standard class you need to have a postal barcode with the address. When the barcode is printed and the mail is folded and inserted into an envelope, the barcode has to show through the window in the envelope according to certain rules. Therefore the barcode cannot move. We have the barcode below the address (in a separate textbox, different font), so if the address is a two line address then we have to move the address textbox up enough so that the last line of the address is just above the barcode.

    So the address textbox is long enough to handle x lines of address and it is moved up or down to accomodate the variable number of address lines.

    We have a report for each mailing we do. Right now we have thousands of reports each with the code to move the address around. The reports use different fonts, weights and sizes and right now I have to add every combination of font, weight, and size that we use into a table that I can do a lookup on. The current code determines how tall the box will be by counting crlfs and multiplying that by the heght of each line.

    I am not the only one that enters new report text, but I am the only one that can write code. So I would like to have the address block move transparent to the other 'programmers'.

    So that is the long explanation. Any comments/suggestions are welcome.

  8. #8
    Join Date
    Apr 2004
    Location
    outside the rim
    Posts
    1,011
    Use a header section with a hard coded height for the address. You may get a space between the address and the bar code, but the height of the area the address is printed is forced.

    Actually, can't you accomplish the same by forcing the height of the address box and setting can grow/shrink to false?

    tc

  9. #9
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    That's is something I didn't mention... The address does not have to be at the top of the report. In some cases it is in the middle of the report with text above and below.

    Currently I have the address textbox a fixed size with grow/shrink to false. Depending on the number of lines in the address (typically 4-6) there should only be about 1/8 inch gap between the barcode and the bottom line of the address. The reason is, if there is more of a gap then some of the address may not show through the window of the envelope.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,487
    Personally, I have never found any real definitive solution to retrieving the width or height of a String in Twips dependant upon the font size and the Control it may reside in. But I have come up with something that is extremely close and may do the job.

    It requires the use of a few Windows API functions, in particular the API GetFocus function so as to acquire the Handle (hWnd) of the MS-Access Control which contains the text String, the GetDC function so as to acquire the Device Context (DC) of the Control, and finally the GetTextExtentPoint32 function so as to determine the length or hieght of the String located within the Control in Points.

    All these API functions are put to work within the functions I call GetStringWidthInTwips and GetStringHeightInTwips and delivers the Twip String Width which can be used to either make wider or narrower the Control which contains the String value.

    To begin, Place the following code into a Database Code Module:

    Code:
    Option Explicit
    ' Type Declaration for the GetTextExtentPoint32 function.
    Private Type POINTAPI
       X As Long
       Y As Long
    End Type
    
    ' The API function to retreive the width and Height of 
    ' a String (in Points).
    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
    
    ' The API function used to acguire the Device Context
    ' (DC) of a Control.
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    
    ' The API function used to acquire the Handle (hWnd)
    ' of a MS-Access Control. MS-Access Controls don't
    ' have a Handle because they are drawn to screen as
    ' when required. Once it's drawn, we can then get a
    ' Handle.
    Private Declare Function apiGetFocus Lib "user32" _
                                     Alias "GetFocus" () As Long
    
    
    Public Function GetStringWidthInTwips(Ctrl As Control) As Long
       Dim TextSize As POINTAPI
       Dim nDC As Long
       Dim mWnd As Long
       Dim Strg As String
    
    ' Fill the Value of the Control into the Strg String variable.
       Strg = CStr(Ctrl.Text)
    'Get the Handle (hWnd) of the Control
       mWnd = GethWnd(Ctrl)
    'Get the Control's device context
       nDC = GetDC(mWnd)
    'Get the width of our Text String
       GetTextExtentPoint32 nDC, Strg, Len(Strg), TextSize
    ' Convert the the determined text length (in points) to Twips.
       GetStringWidthInTwips = (TextSize.X * 15)
     End Function
    
    Public Function GetStringHeightInTwips(Ctrl As Control) As Long
       Dim TextSize As POINTAPI
       Dim nDC As Long
       Dim mWnd As Long
       Dim Strg As String
       ' Fill the Value of the Control into the Strg String variable.
       Strg = CStr(Ctrl.Text)
       'Get the Handle (hWnd) of the Control
       mWnd = GethWnd(Ctrl)
       'Get the Control's Device Context
       nDC = GetDC(mWnd)
       'Get the Height of our Text String
       GetTextExtentPoint32 nDC, Strg, Len(Strg), TextSize
       ' Convert the the determined text Height (in points) to Twips.
       GetStringHeightInTwips = (TextSize.Y * 15)
    End Function
    
    Private Function GethWnd(ctl As Control) As Long
        ' Acquire the Handle (hWnd) of the passed
        ' MS -Access Control.
        On Error Resume Next
        ' Make sure the passed Control has Focus.
        ctl.SetFocus
        If Err Then  ' if focus can not be achieved then return 0
            GethWnd = 0
        Else         ' otherwise, get the Handle
            GethWnd = apiGetFocus
        End If
        On Error GoTo 0
    End Function
    


    Now...to use all this mess simple do this:

    Me.myControlName.Height = GetStringHeightInTwips(Me.myControlName)

    If your going to use this in a Report, then place this line of code into the OnFormat event of the Report Section which i contains the Control (usually the Details Section).

    .
    Environment:
    Self Taught In ALL Environments.....And It Shows!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •