Microsoft KB Archive/79604

= How to Convert Units to Pixels for DrawWidth in VB =

Article ID: 79604

Article Last Modified on 12/12/2003

-

APPLIES TO


 * 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

-



This article was previously published under Q79604



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 &quot;target units.&quot;

  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   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   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                         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 query words: 2.00 3.00

Keywords: KB79604

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.