Results 1 to 8 of 8

Thread: [RESOLVED] Printing a Picturebox with Controls - improper sizing

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Resolved [RESOLVED] Printing a Picturebox with Controls - improper sizing

    I have several Controls (textboxes, labels, and an Image Control) placed in a Picturebox. Using the code below, I print the contents (well, actually, the entire PB), but it comes out larger than my printed page (IOW, it needs to be scaled to fit the paper (am using regular 8.5 x 11) when printed). Any suggestions?

    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias _
       "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
       ByVal wParam As Long, ByVal lParam As Long) As Long
    
    
    Private Const WM_PAINT = &HF
    Private Const WM_PRINT = &H317
    Private Const PRF_CLIENT = &H4&   
    Private Const PRF_CHILDREN = &H10&
    Private Const PRF_OWNED = &H20&  
    
    Private Sub Command2_Click()
        PrintPictureBox Picture1, 1000, 1000
    End Sub
    
    
    
    
    Public Sub SavePictureBox(Box As PictureBox, _
                            Optional X As Single = 0, _
                            Optional Y As Single = 0)
    Dim rv As Long
    Dim ar As Boolean
        On Error GoTo Exit_Sub
        With Box
            ar = .AutoRedraw
            .AutoRedraw = True
            rv = SendMessage(.hwnd, WM_PAINT, .hDC, 0)
            rv = SendMessage(.hwnd, WM_PRINT, .hDC, _
            PRF_CHILDREN Or PRF_CLIENT Or PRF_OWNED)
            .Picture = .Image
            Printer.PaintPicture .Picture, X, Y
            Printer.EndDoc
            .AutoRedraw = ar
        End With
    Exit_Sub:
        If Err.Number Then MsgBox Err.Description, vbOKOnly, "Printer Error!"
    End Sub
    Sam I am (as well as Confused at times).

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Printing a Picturebox with Controls - improper sizing

    So scale it. Read the docs for PaintPicture. You are letting too many arguments default.

  3. #3

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Printing a Picturebox with Controls - improper sizing

    Got it...thanks.
    Sam I am (as well as Confused at times).

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Printing a Picturebox with Controls - improper sizing

    Simple demo printing a loaded image:

    Code:
    Option Explicit
    
    Private Sub Main()
        Dim Picture As StdPicture
        Dim Aspect As Double
    
        ChoosePrinter.Show vbModal
        If ChoosePrinter.Canceled Then
            Unload ChoosePrinter
            MsgBox "Canceled!"
        Else
            Set Printer = ChoosePrinter.SelectedPrinter
            Unload ChoosePrinter
            Set Picture = LoadPicture("OrgChart.emf")
            
            'Print Picture, scaled to printable width of Printer maintaininng aspect ratio:
            With Picture
                Aspect = CDbl(.Height) / CDbl(.Width)
            End With
            With Printer
                On Error Resume Next 'For printers that raise a dialog, e.g. PDF print drivers.
                .PaintPicture Picture, _
                              0, _
                              0, _
                              .ScaleWidth, _
                              .ScaleY(.ScaleX(.ScaleWidth, .ScaleMode, vbHimetric) * Aspect, _
                                      vbHimetric, _
                                      .ScaleMode)
                If Err Then
                    MsgBox "Canceled!"
                    Exit Sub
                End If
                On Error GoTo 0
                .NewPage
                .EndDoc
            End With
            MsgBox "Done!"
        End If
    End Sub
    Attached Files Attached Files

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Printing a Picturebox with Controls - improper sizing

    You can modify this if you have control over Printer.ScaleWidth, which normally defaults to twips but you can be expliocit:

    Code:
            With Printer
                .ScaleMode = vbTwips
                On Error Resume Next 'For printers that raise a dialog, e.g. PDF print drivers.
                .PaintPicture Picture, _
                              0, _
                              0, _
                              .ScaleWidth, _
                              .ScaleWidth * Aspect
    Any absolute units will do (inches, etc.), just be sure you don't fall into the trap of vbPixels.

  6. #6

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: [RESOLVED] Printing a Picturebox with Controls - improper sizing

    thx dile....but I had already 'fixed' my issue with simple arguments (as you suggested).

    In view of learning, what does your project above add in as far as capability/concept, vice simply using proper arguments (which I had failed to do in the beginning)?
    Sam I am (as well as Confused at times).

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,156

    Re: [RESOLVED] Printing a Picturebox with Controls - improper sizing

    Quote Originally Posted by SamOscarBrown View Post
    In view of learning, what does your project above add in as far as capability/concept, vice simply using proper arguments (which I had failed to do in the beginning)?
    He cannot possible tell this without your first showing which proper arguments the code is currently using.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: [RESOLVED] Printing a Picturebox with Controls - improper sizing

    @w....(and dile?)...well, I simply used

    Code:
    Printer.PaintPicture .Picture, X, Y, 10000, 13000
    where X and Y are 1000 and 500 respectively (changed from 1000 and 1000 in call above).
    Sam I am (as well as Confused at times).

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