Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Printer information without using Common Dialog Box


Message #1 by "Shannon Sustar" <shannons@d...> on Tue, 4 Mar 2003 20:17:26
Place this on a form with a listbox called List1 and a command button called
Command1...

Option Explicit

      Private Const NULLPTR = 0&
      ' Constants for DEVMODE
      Private Const CCHDEVICENAME = 32
      Private Const CCHFORMNAME = 32
      ' Constants for DocumentProperties
      Private Const DM_MODIFY = 8
      Private Const DM_COPY = 2
      Private Const DM_IN_BUFFER = DM_MODIFY
      Private Const DM_OUT_BUFFER = DM_COPY
      ' Constants for dmOrientation
      Private Const DMORIENT_PORTRAIT = 1
      Private Const DMORIENT_LANDSCAPE = 2
      ' Constants for dmPrintQuality
      Private Const DMRES_DRAFT = (-1)
      Private Const DMRES_HIGH = (-4)
      Private Const DMRES_LOW = (-2)
      Private Const DMRES_MEDIUM = (-3)
      ' Constants for dmTTOption
      Private Const DMTT_BITMAP = 1
      Private Const DMTT_DOWNLOAD = 2
      Private Const DMTT_DOWNLOAD_OUTLINE = 4
      Private Const DMTT_SUBDEV = 3
      ' Constants for dmColor
      Private Const DMCOLOR_COLOR = 2
      Private Const DMCOLOR_MONOCHROME = 1
      ' Constants for dmCollate
      Private Const DMCOLLATE_FALSE = 0
      Private Const DMCOLLATE_TRUE = 1
      Private Const DM_COLLATE As Long = &H8000
      ' Constants for dmDuplex
      Private Const DM_DUPLEX = &H1000&
      Private Const DMDUP_HORIZONTAL = 3
      Private Const DMDUP_SIMPLEX = 1
      Private Const DMDUP_VERTICAL = 2

      Private Type DEVMODE
          dmDeviceName(1 To CCHDEVICENAME) As Byte
          dmSpecVersion As Integer
          dmDriverVersion As Integer
          dmSize As Integer
          dmDriverExtra As Integer
          dmFields As Long
          dmOrientation As Integer
          dmPaperSize As Integer
          dmPaperLength As Integer
          dmPaperWidth As Integer
          dmScale As Integer
          dmCopies As Integer
          dmDefaultSource As Integer
          dmPrintQuality As Integer
          dmColor As Integer
          dmDuplex As Integer
          dmYResolution As Integer
          dmTTOption As Integer
          dmCollate As Integer
          dmFormName(1 To CCHFORMNAME) As Byte
          dmUnusedPadding As Integer
          dmBitsPerPel As Integer
          dmPelsWidth As Long
          dmPelsHeight As Long
          dmDisplayFlags As Long
          dmDisplayFrequency As Long

      End Type

      Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
      "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
      ByVal pDefault As Long) As Long

      Private Declare Function DocumentProperties Lib "winspool.drv" _
      Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
      ByVal hPrinter As Long, ByVal pDeviceName As String, _
      pDevModeOutput As Any, ByVal pDevModeInput As Long, _
      ByVal fMode As Long) As Long

      Private Declare Function ClosePrinter Lib "winspool.drv" _
      (ByVal hPrinter As Long) As Long

      Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
      (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

      Function StripNulls(OriginalStr As String) As String
         If (InStr(OriginalStr, Chr(0)) > 0) Then
            OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
         End If
         StripNulls = Trim(OriginalStr)
      End Function

      Function ByteToString(ByteArray() As Byte) As String
        Dim TempStr As String
        Dim I As Integer

        For I = 1 To CCHDEVICENAME
            TempStr = TempStr & Chr(ByteArray(I))
        Next I
        ByteToString = StripNulls(TempStr)
      End Function

      Function GetPrinterSettings(szPrinterName As String, hdc As Long) _
      As Boolean
      Dim hPrinter As Long
      Dim nSize As Long
      Dim pDevMode As DEVMODE
      Dim aDevMode() As Byte
      Dim TempStr As String

        If OpenPrinter(szPrinterName, hPrinter, NULLPTR) <> 0 Then
           nSize = DocumentProperties(NULLPTR, hPrinter, szPrinterName, _
           NULLPTR, NULLPTR, 0)
          If nSize < 1 Then
            GetPrinterSettings = False
            Exit Function
          End If
          ReDim aDevMode(1 To nSize)
          nSize = DocumentProperties(NULLPTR, hPrinter, szPrinterName, _
          aDevMode(1), NULLPTR, DM_OUT_BUFFER)
          If nSize < 0 Then
            GetPrinterSettings = False
            Exit Function
          End If
         Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))


         List1.Clear   ' empty the ListBox
         List1.AddItem "Printer Name: " & _
         ByteToString(pDevMode.dmDeviceName)

         If pDevMode.dmOrientation = DMORIENT_PORTRAIT Then
            TempStr = "PORTRAIT"
         ElseIf pDevMode.dmOrientation = DMORIENT_LANDSCAPE Then
            TempStr = "LANDSCAPE"
         Else
            TempStr = "UNDEFINED"
         End If
         List1.AddItem "Orientation: " & TempStr

         Select Case pDevMode.dmPrintQuality
            Case DMRES_DRAFT
                TempStr = "DRAFT"
            Case DMRES_HIGH
                TempStr = "HIGH"
            Case DMRES_LOW
                TempStr = "LOW"
            Case DMRES_MEDIUM
                TempStr = "MEDIUM"
            Case Else   ' positive value
                TempStr = CStr(pDevMode.dmPrintQuality) & " dpi"
         End Select
         List1.AddItem "Print Quality: " & TempStr

         Select Case pDevMode.dmTTOption
            Case DMTT_BITMAP    ' default for dot-matrix printers
                TempStr = "TrueType fonts as graphics"
            Case DMTT_DOWNLOAD  ' default for HP printers that use PCL
                TempStr = "Downloads TrueType fonts as soft fonts"
            Case DMTT_SUBDEV    ' default for PostScript printers
                TempStr = "Substitute device fonts for TrueType fonts"
            Case Else
                TempStr = "UNDEFINED"
         End Select
         List1.AddItem "TrueType Option: " & TempStr

         ' Windows NT drivers often return COLOR from Monochrome printers
         If pDevMode.dmColor = DMCOLOR_MONOCHROME Then
            TempStr = "MONOCHROME"
         ElseIf pDevMode.dmColor = DMCOLOR_COLOR Then
            TempStr = "COLOR"
         Else
            TempStr = "UNDEFINED"
         End If
         List1.AddItem "Color or Monochrome: " & TempStr

         If pDevMode.dmScale = 0 Then
            TempStr = "NONE"
         Else
            TempStr = CStr(pDevMode.dmScale)
         End If
         List1.AddItem "Scale Factor: " & TempStr

        If pDevMode.dmFields And DM_COLLATE Then
            If pDevMode.dmCollate = DMCOLLATE_FALSE Then
               TempStr = "Collating is supported, but turned off"
            ElseIf pDevMode.dmCollate = DMCOLLATE_TRUE Then
               TempStr = "Collating is supported and turned on"
            End If
         Else
            TempStr = "Collating is unsupported"
         End If
         List1.AddItem TempStr
         If pDevMode.dmFields And DM_DUPLEX Then
            If pDevMode.dmDuplex = DMDUP_SIMPLEX Then
               TempStr = "Duplex is supported, but turned off (1)"
            ElseIf pDevMode.dmDuplex = DMDUP_VERTICAL Then
               TempStr = "Duplex is set to VERTICAL (2)"
            ElseIf pDevMode.dmDuplex = DMDUP_HORIZONTAL Then
               TempStr = "Duplex is set to HORIZONTAL (3)"
            Else
               TempStr = "Duplex is set to undefined value of " & _
                  pDevMode.dmDuplex
            End If
         Else
            TempStr = "Duplex is unsupported"
         End If
         List1.AddItem TempStr

         List1.AddItem "Y Resolution: " & pDevMode.dmYResolution & " dpi"
         List1.AddItem "Copies: " & CStr(pDevMode.dmCopies)
         ' Add any other items of interest ...

         Call ClosePrinter(hPrinter)
         GetPrinterSettings = True
      Else
         GetPrinterSettings = False
      End If
      End Function

      Private Sub Command1_Click()
      If GetPrinterSettings(Printer.DeviceName, Printer.hdc) = False Then
         List1.AddItem "No Settings Retrieved!"
         MsgBox "Unable to retrieve Printer settings.", , "Failure"
      End If
      End Sub


Regards,
Peter-John Lightfoot

-----Original Message-----
From: Shannon Sustar [mailto:shannons@d...]
Sent: 04 March 2003 20:17
To: professional vb
Subject: [pro_vb] Printer information without using Common Dialog Box


How can I get the printer information, such as status, type what port,
etc. without using the Windows Common Dialog Box?

Thanks,
Shannon

______________________________________________
"The information contained in this communication is confidential and
may be legally privileged.  It is intended solely for the use of the
individual or entity to whom it is addressed and others authorised to
receive it.  If you are not the intended recipient you are hereby
notified that any disclosure, copying, distribution or taking action
in reliance of the contents of this information is strictly prohibited
and may be unlawful.  Absa is liable neither for the proper, complete
transmission of the information contained in this communication, nor 
for any delay in its receipt, nor for the assurance that it is 
virus-free."

  Return to Index