You are currently offline, waiting for your internet to reconnect

How to Convert Units to Pixels for DrawWidth in VB

This article was previously published under Q79604
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
The DrawWidth property controls line thickness for the graphics methodsCircle, 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 tocorrespond with measurements in units other than pixels.
MORE INFORMATION
The following steps describe how to calculate DrawWidth from unitsother 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 DrawWidthproperty in inches, for a form and the printer:
'*** In the global module: ***' ScaleMode (form, picture box, Printer)Global Const TWIPS = 1Global Const POINTS = 2      ' 20 twipsGlobal Const PIXELS = 3Global Const CHARACTERS = 4  ' x: 120 twips, y: 240 twipsGlobal Const INCHES = 5      ' 1440 twipsGlobal Const MILLIMETERS = 6 ' 5669 twipsGlobal 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.EndDocEnd Sub				
When run, the above sample program will cause two lines in the form ofan X to be printed to the form and printer simultaneously. The widthof the thicker diagonal line should be 0.25 inches wide on the printedpage. The other diagonal line represents a line five pixels wide.
2.00 3.00
Properties

Article ID: 79604 - Last Review: 12/12/2003 20:04:17 - Revision: 2.0

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition
  • KB79604
Feedback