How to Convert Units to Pixels for DrawWidth in VBLast reviewed: June 21, 1995Article 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
SUMMARYThe 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 INFORMATIONThe following steps describe how to calculate DrawWidth from units other than pixels, referred to as "target units."
'*** In the global module: *** ' ScaleMode (form, picture box, Printer)Global Const TWIPS = 1 Global Const POINTS = 2 ' 20 twipsGlobal Const PIXELS = 3 Global Const CHARACTERS = 4 ' x: 120 twips, y: 240 twips Global Const INCHES = 5 ' 1440 twipsGlobal 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 SubWhen 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |