Visual Basic 6 Problem

Miscellaneous Forums/General Discussion/Visual Basic 6 Problem

The GetDeviceCaps API will return all information required to calculate the printable area of the current printer object. You will need to call the API for each parameter you are interested in:

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

Public Type PRINTERAREA
  DpiX As Long      ' Horizontal Resolution
  DpiY As Long      ' Vertical Resolution
  Height As Long    ' Printer.Height equivalent
  Width As Long     ' Printer.Width equivalent
  Left As Long      ' Left Gutter Margin
  Top As Long       ' Top Gutter Margin
  HorRes As Long    ' Printable Width (MM)
  VerRes As Long    ' Printable Height (MM)
  HorSize As Long   ' Paper Width (MM)
  VerSize As Long   ' Paper Height (MM)
End Type

Const PHYSICALWIDTH = 110
Const PHYSICALHEIGHT = 111
Const PHYSICALOFFSETX = 112
Const PHYSICALOFFSETY = 113
Const LOGPIXELSX = 88
Const LOGPIXELSY = 90
Const HORZRES = 8
Const HORZSIZE = 4
Const VERTRES = 10
Const VERTSIZE = 6

Const IFACT = 1440 'Inch to Twip Factor

Public Prt_Area As PRINTERAREA

' load current settings into structure
Call GetPrintArea

Private Sub GetPrintArea()
  With Prt_Area
    .DpiX = GetDeviceCaps(Printer.hdc, LOGPIXELSX)
    .DpiY = GetDeviceCaps(Printer.hdc, LOGPIXELSY)
    .Left = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX) / .DpiX * IFACT
    .Top = GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY) / .DpiY * IFACT
    .Width = GetDeviceCaps(Printer.hdc, PHYSICALWIDTH) / .DpiX * IFACT
    .Height = GetDeviceCaps(Printer.hdc, PHYSICALHEIGHT) / .DpiY * IFACT
    .HorRes = GetDeviceCaps(Printer.hdc, HORZRES) / .DpiX * IFACT
    .VerRes = GetDeviceCaps(Printer.hdc, VERTRES) / .DpiY * IFACT
    .HorSize = GetDeviceCaps(Printer.hdc, HORZSIZE)
    .VerSize = GetDeviceCaps(Printer.hdc, VERTSIZE)
  End With
End Sub

You must call this every time the user changes the current printer (the simplest way to do this is to call it every time you start a new print job). You can then offset all your drawing/text calls with the appropriate left/top margins as you send them to the printer, thus ensuring your printout looks the same on every device.