Product : Engine, Version : 4.0.1.1018, ArticleID : 41018051

HowTo : Get printer information (Color, Margins etc)

Article41018051
TypeHowTo
ProductEngine
Version4.0.1.1018
Date Added4/3/2003
Submitted byBrian Stafford
Keywords

Subject

Get printer information (Color, Margins etc)

Summary

Hi,
I need to determine if the printer that I'm printing to supports color (so I can print in black and white if it does not). Is there some method in your control that will tell me this? If not, could you possibly point me somewhere to find out how to do this? In either case, a little snippet of vb code would be great!

Thanks,
Brian

Solution

See the VB 6.0 code below:

Option Explicit

Private Declare Function GetDeviceCaps Lib "gdi32" _
        (ByVal hdc As Long, ByVal nIndex As Long) As Long

' Constants for nIndex argument of GetDeviceCaps
Private Const NUMCOLORS = 24         '  Number of colors the device supports
Private Const BITSPIXEL = 12         '  Number of bits per pixel
Private Const PLANES = 14            '  Number of planes
Private Const SIZEPALETTE = 104      '  Number of entries in physical palette

Private Const HORZRES = 8
Private Const VERTRES = 10
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Private Const PHYSICALWIDTH = 110
Private Const PHYSICALHEIGHT = 111
Private Const PHYSICALOFFSETX = 112
Private Const PHYSICALOFFSETY = 113

Private Sub Command1_Click()
 Dim dpiX As Long, dpiY As Long
 Dim MarginLeft As Long, MarginRight As Long
 Dim MarginTop As Long, MarginBottom As Long
 Dim PrintAreaHorz As Long, PrintAreaVert As Long
 Dim PhysHeight As Long, PhysWidth As Long
 Dim Info As String
 Dim NColors As Long
 Dim NBitsPerPixel As Long
 Dim NPlanes       As Long
 Dim NSizePal      As Long
 Dim iNumColors       As Long

    Info = "Printer Name : " & Printer.DeviceName
    Info = Info & vbCrLf & "iNumColors = 2 ^ (PLANES * BITSPIXEL)"
    Info = Info & vbCrLf & "------------------------------------------"
    
    NColors = GetDeviceCaps(Printer.hdc, NUMCOLORS)
    Info = Info & vbCrLf & "NUMCOLORS: " & NColors & " "
    
    NPlanes = GetDeviceCaps(Printer.hdc, PLANES)
    Info = Info & vbCrLf & "PLANES: " & NPlanes & " "
    
    NSizePal = GetDeviceCaps(Printer.hdc, SIZEPALETTE)
    Info = Info & vbCrLf & "SIZEPALETTE: " & NSizePal & " "
    
    NBitsPerPixel = GetDeviceCaps(Printer.hdc, BITSPIXEL)
    Info = Info & vbCrLf & "BITSPIXEL: " & NBitsPerPixel & " "
    
    iNumColors = 2 ^ (NPlanes * NBitsPerPixel)
    Info = Info & vbCrLf & "Colors : " & iNumColors & " "
        
    dpiX = GetDeviceCaps(Printer.hdc, LOGPIXELSX)
    Info = Info & vbCrLf & "Pixels X: " & dpiX & " dpi"
    
    dpiY = GetDeviceCaps(Printer.hdc, LOGPIXELSY)
    Info = Info & vbCrLf & "Pixels Y: " & dpiY & " dpi"
    
    MarginLeft = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX)
    Info = Info & vbCrLf & "Unprintable space on left: " & _
    MarginLeft & " pixels = " & MarginLeft / dpiX & " inches"
    
    MarginTop = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY)
    Info = Info & vbCrLf & "Unprintable space on top: " & _
    MarginTop & " pixels = " & MarginTop / dpiY & " inches"
    
    PrintAreaHorz = GetDeviceCaps(Printer.hdc, HORZRES)
    Info = Info & vbCrLf & "Printable space (Horizontal): " & _
    PrintAreaHorz & " pixels = " & PrintAreaHorz / dpiX & " inches"
    
    PrintAreaVert = GetDeviceCaps(Printer.hdc, VERTRES)
    Info = Info & vbCrLf & "Printable space (Vertical): " & _
    PrintAreaVert & " pixels = " & PrintAreaVert / dpiY & " inches"
    
    PhysWidth = GetDeviceCaps(Printer.hdc, PHYSICALWIDTH)
    Info = Info & vbCrLf & "Total space (Horizontal): " & _
    PhysWidth & " pixels = " & PhysWidth / dpiX & " inches"
    
    MarginRight = PhysWidth - PrintAreaHorz - MarginLeft
    Info = Info & vbCrLf & "Unprintable space on right: " & _
    MarginRight & " pixels = " & MarginRight / dpiX & " inches"
    
    PhysHeight = GetDeviceCaps(Printer.hdc, PHYSICALHEIGHT)
    Info = Info & vbCrLf & "Total space (Vertical): " & _
    PhysHeight & " pixels = " & PhysHeight / dpiY & " inches"
    
    MarginBottom = PhysHeight - PrintAreaVert - MarginTop
    Info = Info & vbCrLf & "Unprintable space on bottom: " & _
    MarginBottom & " pixels = " & MarginBottom / dpiY & " inches"
    
    MsgBox Info, , "GetDeviceCaps Returned the Following:"
End Sub