3

I have a picture box and I print contents in it. I want to know the exact textwidth of the text in millimeters. But I get wrong value. here is my code

me.scalemode = vbmillimeters
picturebox.scalemode = vbmillimeters

picturebox.fontname = "Arial"
picturebox.fontsize = 12
debug.print textwidth("AB.C.D.E. FGHIJKLMN")

When i measure in the printout in paper it is 48 mm but it shows 32.97mm

please help me where am wrong. Thanks in advance

Srinivasan
  • 293
  • 1
  • 6
  • 16

1 Answers1

4

If you need the width of the text printed to the picture box, use:

PictureBox.textwidth("AB.C.D.E. FGHIJKLMN")

What you are actually doing: textwidth("AB.C.D.E. FGHIJKLMN") is mesuring the same text printed to the Form (Me).

Doing like this would be less error-prone:

Dim TextWidth as Single
With PictureBox
  .ScaleMode = vbMillimeters
  .FontName = "Arial"
  .FontSize = 12
  TextWidth = .TextWidth("AB.C.D.E. FGHIJKLMN")
End With

because if you are then switching to paper, you can also easily switch context:

With SelectedPrinter....
deblocker
  • 7,629
  • 2
  • 24
  • 59
  • 1
    Of course it will fail at some High DPI settings. VB6 uses something in OLE to perform those scalings, something that is off a bit at some DPI settings such as 200% (192 DPI). 7.5 twips/pixel gets truncated to 7 internally, throwing everything off. Don't expect Microsoft to ever fix it though. You won't see this unless your program is marked DPI-Aware anyway. – Bob77 Apr 15 '17 at 08:19