How to Convert Units to Pixels for DrawWidth in VB

Last reviewed: June 21, 1995
Article ID: Q79604
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

The DrawWidth property controls line thickness for the graphics methods Circle, Line, and PSet. You can only set DrawWidth in units of pixels. Pixel size and density vary among video and printer devices.

This article describes how to set DrawWidth to the number of pixels to correspond with measurements in units other than pixels.

MORE INFORMATION

The following steps describe how to calculate DrawWidth from units other than pixels, referred to as "target units."

  1. Determine the form (or printer) width in target units by setting the ScaleMode property to one of the values listed below and then retrieving the ScaleWidth property.

    ScaleMode Settings ------------------

    0 -- user-defined 1 -- twips 2 -- points 4 -- characters 5 -- inches 6 -- millimeters 7 -- centimeters

    For example:

          Form1.ScaleMode = 7  ' centimeters
          cm = Form1.ScaleWidth
    

  2. Determine the form (or printer) width in pixels by setting the ScaleMode property to 3 (PIXELS in CONSTANT.TXT) and then retrieving the ScaleWidth property.

    For example:

          Form1.ScaleMode = 3
          pixel = Form1.ScaleWidth
    

  3. Calculate the ratio of pixels per target unit by dividing the form (or printer) width in target units by the form (or printer) width in pixels.

    For example:

          pixel_per_cm = pixel / cm
    

  4. Set DrawWidth to the number of target units multiplied by the ratio of pixels per target unit.

    For example:

          Form1.DrawWidth = 5 * pixel_per_cm  ' 5cm thick lines
    

The following code example demonstrates how to calculate the DrawWidth property in inches, for a form and the printer:

'*** In the global module: ***

' ScaleMode (form, picture box, Printer)
Global Const TWIPS = 1
Global Const POINTS = 2      ' 20 twips
Global Const PIXELS = 3 Global Const CHARACTERS = 4 ' x: 120 twips, y: 240 twips
Global Const INCHES = 5      ' 1440 twips
Global Const MILLIMETERS = 6 ' 5669 twips Global Const CENTIMETERS = 7 ' 566.9 twips

' *** In the form: ***

Sub Form_Click ()
   Dim ptr_inch  As Integer ' printer width in inches
   Dim ptr_pixel As Long    ' printer width in pixels
   Dim ptr_dpi   As Single  ' printer dots (pixels) per inch
   Dim scn_inch  As Integer ' screen width in inches
   Dim scn_pixel As Long    ' screen width in pixels
   Dim scn_dpi   As Single  ' screen dots (pixels) per inch

   ' Determine printer pixels-per-inch ratio
   save% = Printer.ScaleMode
   Printer.ScaleMode = INCHES: ptr_inch = Printer.ScaleWidth
   Printer.ScaleMode = PIXELS: ptr_pixel= Printer.ScaleWidth
   Printer.ScaleMode = save%
   ptr_dpi = ptr_pixel / ptr_inch

   ' Determine form (screen) pixels-per-inch ratio
   save% = Form1.ScaleMode
   Form1.ScaleMode = INCHES: scn_inch = Form1.ScaleWidth
   Form1.ScaleMode = PIXELS: scn_pixel = Form1.ScaleWidth
   Form1.ScaleMode = save%
   scn_dpi = scn_pixel / scn_inch

   ' Set printer and form DrawWidth to 0.25 inches
   ' and draw a 0.25 inch thick line
   Printer.DrawWidth = .25 * ptr_dpi
   Form1.DrawWidth = .25 * scn_dpi
   Printer.Line (0, 0)-(Form1.ScaleWidth, Form1.ScaleHeight)
   Form1.Line (0, 0)-(Form1.ScaleWidth, Form1.ScaleHeight)

   ' Set printer.DrawWidth to match screen pixel size
   ' and draw a 5 screen-pixel thick line
   Form1.DrawWidth = 5
   Printer.DrawWidth = Form1.DrawWidth * ptr_dpi / scn_dpi
   Form1.Line (0, Form1.ScaleHeight)-(Form1.ScaleWidth, 0)
   Printer.Line (0, Form1.ScaleHeight)-(Form1.ScaleWidth, 0)

   Printer.EndDoc

End Sub

When run, the above sample program will cause two lines in the form of an X to be printed to the form and printer simultaneously. The width of the thicker diagonal line should be 0.25 inches wide on the printed page. The other diagonal line represents a line five pixels wide.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.