Results 1 to 2 of 2

Thread: Print Device Context

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Posts
    17

    Print Device Context

    I need to create a print device context to a computers default computer.

    Here is the code I'm using right now, but it doesn't work. It seems to be breaking at the EnumPrinters function. Can somebody please help me?

    Dim hPrintDC As Long ' handle to the printer's device context
    Dim di As DOCINFO ' information about the document to print
    Dim buffer(0 To 3076 / 4) As Long ' 3076-byte buffer
    Dim pi2 As PRINTER_INFO_2 ' receives info about the default printer
    Dim printret As Long ' receives the number of printers returned from EnumPrinters
    Dim spaceneeded As Long ' receives space requires for EnumPrinters
    Dim retval As Long ' return value
    Dim fileName As String
    Dim cnt As Integer

    ' Get the device and driver names of the default printer. For a more detailed
    retval = EnumPrinters(PRINTER_ENUM_DEFAULT, "", 2, buffer(0), 3076, spaceneeded, printret)
    If retval = 0 Then
    Debug.Print "No default printer is configured."
    End ' abort the program
    End If
    ' Copy the device and driver names to the structure. All the
    ' other information retrieved is not needed and is omitted here.
    pi2.pPrinterName = Space(lstrlen(buffer(1)))
    retval = lstrcpy(pi2.pPrinterName, buffer(1))
    pi2.pDriverName = Space(lstrlen(buffer(4)))
    retval = lstrcpy(pi2.pDriverName, buffer(4))

    ' Create a device context to the printer, using its default initialization.
    hPrintDC = CreateDC("", pi2.pPrinterName, 0, ByVal CLng(0))

  2. #2
    jim mcnamara
    Guest
    This may not be what you want but it is easier:
    Code:
      Dim prtr as Object
      Dim dc() as long
      Dim hDC as long
      hDC = Printer.hDC  ' this gives the hDC of the currently active printer
    
       For each Prtr in Printers
             Redim Preserve dc(Ubound(dc) + 1)
             dc(Ubound(dc)) = Prtr.hDC  
       next
       ' you now have the device context for all printers
    PS: Pictureboxes and printers have hDC property

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width