Detecting resolution for Printer object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cjreynolds
    New Member
    • Feb 2007
    • 6

    Detecting resolution for Printer object

    I'm using the Printer object (VB6) to print graphics and need to detect the printer's resolution (in DPI). Tried

    X = Printer.PrintQu aliy

    to no avail. Returns a zero.

    It's been several years since I worked with VB, I guess I'm easily stumped...

    Can it be calculated using TwipsPerPixel?

    Thanks,

    joe
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by cjreynolds
    I'm using the Printer object (VB6) to print graphics and need to detect the printer's resolution (in DPI). Tried
    X = Printer.PrintQu aliy
    to no avail. Returns a zero.
    It's been several years since I worked with VB, I guess I'm easily stumped...
    Can it be calculated using TwipsPerPixel?
    Yes, twips per pixel seems as though it should (indirectly) give you what you want. A twip is 1/1440th of an inch. Or (if you live somewhere that has moved on from the Dark Ages) about 0.017639 of a millimetre.

    Comment

    • cjreynolds
      New Member
      • Feb 2007
      • 6

      #3
      I think I've found a better way using GetDeviceCaps() :

      Code:
      Private Declare Function GetDeviceCaps Lib "gdi32" _
      (ByVal hdc As Long, ByVal nIndex As Long) As Long
            
      Private Const LOGPIXELSX = 88
      Private Const LOGPIXELSY = 90
      
      Private Sub Command1_Click()
      Dim dpiX As Long, dpiY As Long
          dpiX = GetDeviceCaps(Printer.hdc, LOGPIXELSX)
          dpiY = GetDeviceCaps(Printer.hdc, LOGPIXELSY)
      
          MsgBox (dpiX & " " & dpiY)
      End Sub
      I think this is working - if you can confirm/deny it's validity, please tell me.

      Now that I (hopefully) have the printer resolution, how do I use that to put something on paper at, say, X = 2 inches, Y = 4 inches? I've been using this code to scale/position the image:

      Code:
      Sub SetLargePrinterScale(obj As Object, VertPosition As Single)
      Dim Owid As Single
      Dim Ohgt As Single
      Dim Pwid As Single
      Dim Phgt As Single
      Dim Xmid As Single
      Dim Ymid As Single
      Dim S As Single
      Dim ScaleFactor As Single
      
          ScaleFactor = (50 - frmPrint.Slider3.Value) / 10
          
          Owid = obj.ScaleX(obj.ScaleWidth, obj.ScaleMode, vbPixels)
          Ohgt = obj.ScaleY(obj.ScaleHeight, obj.ScaleMode, vbPixels)
          
          Pwid = Printer.ScaleX(Printer.ScaleWidth, Printer.ScaleMode, vbPixels)
          Phgt = Printer.ScaleY(Printer.ScaleHeight, Printer.ScaleMode, vbPixels)
          
          If Ohgt / Owid > Phgt / Pwid Then
              S = Phgt / Ohgt
          Else
              S = Pwid / Owid
          End If
          
          Pwid = obj.ScaleX(Pwid * ScaleFactor, vbPixels, obj.ScaleMode) / S
          Phgt = obj.ScaleY(Phgt * ScaleFactor, vbPixels, obj.ScaleMode) / S
          
          Xmid = (obj.ScaleLeft + obj.ScaleWidth / 2) * ScaleFactor
          Ymid = (obj.ScaleTop + obj.ScaleHeight / 1.6) * ScaleFactor
          
          
          Printer.Scale (Xmid - Pwid / 2, Ymid - Phgt / 2)-(Xmid + Pwid / 2, Ymid + Phgt / 2)
      
      End Sub
      It prints the output in the middle of the page (X and Y). To move the printout away from the center, I've been adding an integer to the Xmid and Ymid equations:

      Xmid = (obj.ScaleLeft + obj.ScaleWidth / 2) * ScaleFactor + 100
      Ymid = (obj.ScaleTop + obj.ScaleHeight / 1.6) * ScaleFactor + 200

      but for portability I need to take the current printer resolution into account.

      Any suggestions?

      joe

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I haven't looked into this in detail yet (and it's not an area I'm really familiar with anyway). But I do know that you can produce the same results two ways...
        Code:
        dpiX = GetDeviceCaps(Printer.hdc, LOGPIXELSX)
        dpiY = GetDeviceCaps(Printer.hdc, LOGPIXELSY)
        Code:
        dpiX = 1440 / Printer.TwipsPerPixelX
        dpiY = 1440 / Printer.TwipsPerPixelY
        Not a lot of difference really, except that the TwipsPerPixelX/Y are already available without any declarations. I guess it's just a matter of which way you prefer to go.

        Comment

        • cjreynolds
          New Member
          • Feb 2007
          • 6

          #5
          That works - Thanks!

          Comment

          Working...