Custom Print Preview window for VB5


Create a VB5 form which functions as a custom Print Preview window.


(1) No external 3rd-party software may be involved; use only VB5 internals.

(2) Print Preview window (PPWin) will be called from a custom VB5 app.

(3) Win32 API calls are welcome, if well documented.

(4) User must be able to print PPWin contents with full WYSIWYG results.

(5) PPWin must be able to display bitmap (.BMP) files.

   o .BMP file path to be passed to PPWin programmatically.

   o Size of .BMP file in PPWin is flexible--will be determined programmatically.

(6) PPWin must be able to accept and display text strings programmatically

   These 1-3 line titles will label and describe the .BMP image, and will be determined internally by the developer.

(6) While PPWin is displayed, user must be able to:

   o Change portrait/landscape options. PPWin contents change accordingly.

         If image is more suited to landscape, then image will expand when "Landscape" is selected. Reverse is true for portrait layouts.

   o Submit the data to the printer of his/her choice.


If this can be accomplished with a Form and a PictureBox control, that's fine--but it MUST be WYSIWYG, it MUST reflect what will be produced on the printer. Also, the
ability to scale items in this Print Preview window in ad hoc fashion is crucial.

The answer doesn't have to be a polished procedure. But it does need to be past the conceptual level and down to real code examples.

This is a big question, I know, and I'm giving it all the point I've got. I'll add more as I earn them!


Who is Participating?
watyConnect With a Mentor Commented:
First of all, you have to deal with a picture to preview

All the following text was taken from my Print Preview OCX.

o User must be able to easily switch between Portrait/Landscape
Set the picture size to Printer.Width, Printer.Height. If the user change from Portrait to Landscape, change the picture size according to new Printer.Width and Printer.Height.

o PPWin must accept .BMP images submitted programmatically
Here is my code to preview a bitmap
frmPreview.PictList = LoadPicture(previewLine.sText)

' *** Paint the bitmap
frmPreview.pictPreview(0).PaintPicture frmPreview.PictList, previewLine.nCurrentX, previewLine.nCurrentY
Set frmPreview.PictList = Nothing

Here is the code to print the bitmap :

' *** Load the bitmap ***
frmPreview.PictList = LoadPicture(previewLine.sText)

' *** Print the bitmap
PrintBitmap frmPreview.PictList, previewLine.nCurrentX, previewLine.nCurrentY
Set frmPreview.PictList = Nothing

Public Sub PrintBitmap(pictBox As PictureBox, nX As Integer, nY As Integer)
   ' *** Print a bitmap
   Printer.PaintPicture pictBox, nX, nY
End Sub

o Images placed on the PPWin must be sizeable, and able to
be "maximized" to take advantage of available space, depending
on Portrait/Landscape settings;

Private Sub Stretch(Src As Control, Dest As Control, StretchMode As Long)
   ' *** Stretch pictures ***
   Dim nHoldSrcMode     As Long
   Dim nHoldDestMode    As Long
   Dim XSrc             As Long
   Dim YSrc             As Long
   Dim XDest            As Long
   Dim YDest            As Long
   Dim nSrcWidth        As Long
   Dim nSrcHeight       As Long
   Dim nDestWidth       As Long
   Dim nDestHeight      As Long
   Dim hSrcDC           As Long
   Dim hDestDC          As Long
   Dim nRet             As Long
   Const SRCCOPY = &HCC0020
   On Error Resume Next
   nHoldSrcMode = Src.ScaleMode
   nHoldDestMode = Dest.ScaleMode
   Src.ScaleMode = 3
   Dest.ScaleMode = 3
   XSrc = 0
   YSrc = 0
   nSrcWidth = Src.ScaleWidth
   nSrcHeight = Src.ScaleHeight
   XDest = 0
   YDest = 0
   nDestWidth = Dest.ScaleWidth
   nDestHeight = Dest.ScaleHeight
   hSrcDC = Src.hdc
   hDestDC = Dest.hdc
   nRet = SetStretchBltMode(hDestDC, StretchMode)
   nRet = StretchBlt(hDestDC, XDest, YDest, nDestWidth, nDestHeight, hSrcDC, XSrc, YSrc, nSrcWidth, nSrcHeight, SRCCOPY)
   Src.ScaleMode = nHoldSrcMode
   Dest.ScaleMode = nHoldDestMode
End Sub

o Developer must be able to programmatically place text into the PPWin;
With frmPreview.pictPreview(0)
  If previewLine.nTextRotation = 0 Then
     ' *** set the font ***
     .FontName = Trim(previewLine.sFontName)
     .FontSize = previewLine.dFontSize
     .FontBold = previewLine.bFontBold
     .FontUnderline = previewLine.bFontUnderline
     .FontItalic = previewLine.bFontItalic
     .FontStrikethru = previewLine.bFontStrikeThru
     If (previewLine.nCount <> nRowFind) Then
        .ForeColor = previewLine.nForeColor
        .ForeColor = &HC0&
     End If
     Call SetBkColor(frmPreview.pictPreview(0).hdc, previewLine.nBackColor)
     ' *** set the position ***
     .CurrentX = previewLine.nCurrentX
     .CurrentY = previewLine.nCurrentY
     ' *** print the text ***
     frmPreview.pictPreview(0).Print previewLine.sText
End With

go to my web site :

You will find a complete Print Preview OCX. As it is a shareware (only 20$), it does all you want.

I know it is a 3dr party, but if you don't want to register, send me a mail with specific things you would like to know.

brykerAuthor Commented:

This procedure may be folded into an app distributed to various countries in the Pacific Rim. We've learned long ago not to use any 3rd-party software that's not absolutely essential.

The legalities almost inevitably come back upon us somehow. Plus, we need source code which can be tailored per client.

Again, please, folks, no 3rd-party software. Just good old VB5 code.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

brykerAuthor Commented:
Adjusted points to 190
Why don't you take a look to this OCX, as I told you, I can help you for some parts.
brykerAuthor Commented:

If you would like to submit CODE with which this can be accomplished, I don't care what OCX from which it is sourced or modeled.

I have no doubts that this OCX works perfectly and does everything I seek--it remains, however, a 3rd party product.

If you want to submit some code, be my guest.  Otherwise, let's give others a chance to submit some actual code examples.

Thanks for your cooperation and helpfulness.
brykerAuthor Commented:
Adjusted points to 200
What I would like to say, is that I could help you by submiting some code, but I need to know exactly what you want, and to have idea, is just taking a look to the Print Preview OCX (not use it, nor buy it)
brykerAuthor Commented:
So the question is, what do I want in a Print Preview window?

To rehash my section SPECIFICS in my initial question:

   o User must be able to easily switch between Portrait/Landscape;

   o PPWin must accept .BMP images submitted programmatically;

   o Images placed on the PPWin must be sizeable, and able to
be "maximized" to take advantage of available space, depending
on Portrait/Landscape settings;

   o Developer must be able to programmatically place text into the PPWin;

   o Print results from PPWin must be WYSIWYG;

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.