Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printer Dimension, Measurements (vb6)

    Using the Printer function, how can I size the co-ordinates so the picture prints in the centre of an
    A4 or 8.5 x 11.5 piece of paper ?

    Is there an easy method to find the dimensions ?

    Printer.PaintPicture (Me.Controls("img" & i)), ???, ???, ???, ?????????????
    Printer.EndDoc

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printer Dimension, Measurements (vb6)

    Dave, I would print the image directly from the file, using ShellExecute. The image will be centered automatically, as far as I know.

    Add this declaration to a general module:

    Public Const SW_SHOWNORMAL = 1

    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Call it like this in the On Click event of a command button or something like that to print an image:

    Dim lngResult As Long
    lngResult = ShellExecute(Me.hWnd, "Print", Me.Controls("img" & i), vbNullString, vbNullString, SW_SHOWNORMAL)
    If lngResult < 32 Then
    MsgBox "There was a problem while printing the image.", vbExclamation
    End If

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    <pre>SW_SHOWNORMAL</pre>


    Variable not defined ?

    Hans
    When you say General Module, I created a new one and called it modCentreImage.

    Was I correct ?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printer Dimension, Measurements (vb6)

    Did you put the line

    Public Const SW_SHOWNORMAL = 1

    from my reply above into the module? A "general" module is the type you create by selecting Project | Insert Module...

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    Hans

    I missed that line !!
    It's now added but the Error Trap msg at the end: "MsgBox "There was a problem while printing the image.......
    Fires every time I try to print.

    BTW
    Debug.Print lngResult = 2

    <pre> Case 7
    Dim lngResult As Long
    lngResult = ShellExecute(Me.hwnd, "Print", Me.Controls("img" & i), _
    vbNullString, vbNullString, SW_SHOWNORMAL)
    Debug.Print lngResult
    If lngResult < 32 Then
    MsgBox "There was a problem while printing the image.", vbExclamation
    End If</pre>


  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printer Dimension, Measurements (vb6)

    Oh, wait - Me.Controls("img" & i) is not the file name, of course. I forget, is arrFileNames a module-level variable (declared at the top of the module) or local to the FillImages function? If it is not local, you could probably use

    "L:mmpdfimage" & Left$(arrFileNames(i + intIndex - 1, 1), 8) & ".jpg"

    instead of Me.Controls("img" & i)

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    Ahhhhhhhhhhhhh

    I see where you're coming from now, Filename instead of image.
    Initial tests show promising, but the XP fax viewer kicks in now trying to load 1200...........
    images.

    I'll get around that one, I'll play with the code a little later.

    Thanks again.

    p.s
    I wonder sometimes who's really writing this.
    You seem to know more about this project than I <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

  8. #8
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printer Dimension, Measurements (vb6)

    Here's a Subroutine I use to print Images either in the center of the page or stretched to the size of the page. I hope it's useful.

    Regards,
    Kevin Bell

    Private Sub CenterPrintImage(pb As PictureBox, Optional pOrientation As Long = 1, Optional pStretchImage As Boolean = False, Optional bEndDoc As Boolean = True)
    '
    ' pb is a PictureBox control containing the Image you want to print.
    ' I use a hidden picturebox with the AutoSize property set to True.
    ' If pStretchImage is True then the image is stretched to fit the page.
    ' Orientation and EndDoc are self-explanatory.
    '
    ' The routine assumes you've set the printer to the one you want and the paper size
    ' is set. Note that no allowances are made for unprintable areas on the page.
    ' I'm not sure about PostScript printers either. I haven't got one to test it on.
    '

    Dim lImageWidth As Long, lImageHeight As Long
    Dim lPrinterWidth As Long, lPrinterHeight As Long
    Dim sX As Single, sY As Single

    '
    ' Set the orientation. 1 = Portrait, 2 = Landscape. Default is Portrait
    '
    ' Make sure you do this before calculating anything based on the printer object
    ' as it changes the Height and Width properties.
    '
    On Error GoTo PrinterError ' If we can't set the orientation then
    Select Case pOrientation ' I'm pretty sure we can't print the image.
    Case 1
    Printer.Orientation = 1
    Case 2
    Printer.Orientation = 2
    Case Else
    Printer.Orientation = 1
    End Select

    lPrinterWidth = Printer.ScaleWidth
    lPrinterHeight = Printer.ScaleHeight
    lImageWidth = pb.Width
    lImageHeight = pb.Height

    '
    ' If the picture is larger than the page or we want to stretch it then
    ' scale it to the page
    '

    If pStretchImage Or lImageWidth > lPrinterWidth Then
    lImageWidth = lPrinterWidth
    End If

    If pStretchImage Or lImageHeight > lPrinterHeight Then
    lImageHeight = lPrinterHeight
    End If

    '
    ' Calculate the coordinates for the print
    '

    sX = (lPrinterWidth - lImageWidth) / 2
    sY = (lPrinterHeight - lImageHeight) / 2

    Printer.PaintPicture pb.Picture, sX, sY, lImageWidth, lImageHeight

    If bEndDoc Then
    Printer.EndDoc
    End If

    On Error GoTo 0

    Exit Sub

    PrinterError:
    Dim x As Long
    x = MsgBox("There was an error (" & Trim(Str(Err.Number)) & " - " & Err.Description & ") printing the image to " & Printer.DeviceName & vbCrLf & "Do you wish to continue?", vbQuestion Or vbYesNo, "Printer Error")
    If x = vbNo Then
    End
    End If

    End Sub

  9. #9
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    Kev
    Thanks for the code.

    Just a couple of questions:

    Do I deal with this as a module within the form, or create a new module. ?
    How do I call this function and pass my image name as criteria ?
    Momentarily I am using a case statement for right click upon the image (1 of many)
    Regards

    Dave

  10. #10
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printer Dimension, Measurements (vb6)

    Dave

    <Do I deal with this as a module within the form, or create a new module?>

    If the Sub is only going to be called from within the form you only have to add the code to the end of the form code. if you are going to call it from multiple places in the project then the best thing to do is to add a module and put the code there. Remember if you do this to change the 'Private Sub' to 'Public Sub'.

    <How do I call this function and pass my image name as criteria ?>

    The Sub doesn't cater for passing a file name as a parameter. Add a hidden PictureBox control to your form. Set the AutoSize propeerty to true. Let's say you cann it PictureBoxToPrint.
    Load your image file, let's call the variable containing the picture file name FileNameToLoad, into the hidden PictureBox with PictureBoxToPrint.Picture = LoadPicture(FileNameToLoad)

    The call would then be (assuming that we want to center the picture and not stretch it and we are going to print it Landscape and close the printer document after the print),
    *Notice I've changed the name of the Sub*

    Call PrintImage(PictureBoxToPrint, 2, False, True) - Parameters are PictureBox, Orientation, Stretch, EndDoc

    I've included a ZIP file of a small project that allows you to load a picture and print it. I think it should show you how to call the Sub.

    I hope this helps. Any more questions please don't hesitate to post them.

    Regards,

    Kevin Bell

  11. #11
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    Kev
    Thanks for the additional information.
    Your example is exactly what I had in mind, and all the information is within the example.
    I'll be ok from here.

  12. #12
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printer Dimension, Measurements (vb6)

    Dave.

    Glad to hear it.

    The attached ZIP file is a small project that allows you to print an image from a file name.

    More strings to the bow.

    Regards,

    Kevin Bell

  13. #13
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    And again Kev thanks.

    Works really well in my application.
    Hope you don't mind me using the form as it is ?

  14. #14
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printer Dimension, Measurements (vb6)

    No problem, use and modify the form or code to your heart's content.

    Regards,
    Kevin Bell

  15. #15
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printer Dimension, Measurements (vb6)

    Kev

    Just a further idea, Can the common dialog.ShowPrinter routine be added in the Print module ?

Page 1 of 2 12 LastLast

Posting Permissions

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