?
Solved

Printing

Posted on 2003-03-24
10
Medium Priority
?
451 Views
Last Modified: 2010-04-07
How can i print the contents of a form, the text boxes and their values onto paper?
Cheers
Matt
0
Comment
Question by:MattRawling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 9

Expert Comment

by:GivenRandy
ID: 8194597
To capture and print a screen, form or window, use the sample and source at:

http://support.microsoft.com/support/kb/articles/Q161/2/99.ASP 

To save to a file, use the SavePicture function:

SavePicture Picture1, "filename"

Alternatively:

---
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Const SRCCOPY = &HCC0020

Dim Desk As Long
Dim THdc As Long
   
Desk = GetDesktopWindow
THdc = GetDC(Desk)
Me.AutoRedraw = True
Me.ScaleMode = vbPixels
BitBlt Me.hDC, 0, 0, Screen.Width, Screen.Height, THdc, 0, 0, SRCCOPY
SavePicture Me.Image, "somefile.bmp"
---
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8194631
The visible part ?
Form1.PrintForm

But if you need to print also the non visible content (non visible as outside the screen or as in multiline textboxes), you will have to write more complex code.
Take a look here:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20351282.html

or in Msdn:

HOWTO: Print a Form That Is Too Large for the Screen or Page
Q230502
--------------------------------
The information in this article applies to:
Microsoft Visual Basic Standard Edition, 32-bit, for Windows, version 4.0
Microsoft Visual Basic Professional Edition, 32-bit, for Windows, version 4.0
Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows, version 4.0
Microsoft Visual Basic Learning Edition for Windows, versions 5.0, 6.0
Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0
Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0
---------------------------------
SUMMARY
The PrintForm method prints only the portion of the form
that is displayed. This article demonstrates how to print
a form that is either larger than the screen or is only
partially displayed.

MORE INFORMATION
If you need to print a form that is larger than the
screen, or is only partially displayed, then the controls
on the form must be placed in a PictureBox. This prints
the bitmap that is contained in the PictureBox, instead of
a screen capture of the form itself (which is what
PrintForm does). Because the PictureBox's bitmap is stored
in a device context in memory, you have access to the
entire bitmap regardless of what is displayed.

NOTE: For the purposes of this article, the height of the
form is set to 11 inches. If another paper size is
desired, such as 8.5- X 14- inches, set the sTall variable
to an appropriate value, for example, 14. Also note that
these assignments are made in twips because this is the
default scalemode for forms. If you change the scalemode,
you need to change these values to match as well.

Print a Form That Is Larger Than the Screen
Start a new Standard EXE project in Visual Basic. Form1 is
created by default.
Add two PictureBoxes to Form1.
Avoid drawing the second PictureBox inside the first,
because doing so makes the second PictureBox a member of
the first. Instead, place the origin point of the second
PictureBox to the left of the origin point of the first
PictureBox.
Right-click Picture2 and choose Send to Back.
Add two labels to Picture1, leaving Picture2 empty.
Add the following code to the General Declarations section
of Form1:

Private Const twipFactor = 1440
Private Const WM_PAINT = &HF
Private Const WM_PRINT = &H317
Private Const PRF_CLIENT = &H4&    ' Draw the window's client area.
Private Const PRF_CHILDREN = &H10& ' Draw all visible child windows.
Private Const PRF_OWNED = &H20&    ' Draw all owned windows.

Private Declare Function SendMessage Lib "user32" Alias _
   "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long
   
Private Sub Form_Load()
   Dim sWide As Single, sTall As Single
   Dim rv As Long

   Me.ScaleMode = vbTwips   ' default
   sWide = 8.5
   stall = 11   ' or 14, etc.
   Me.Width = twipFactor * sWide
   Me.Height = twipFactor * stall
   With Picture1
      .Top = 0
      .Left = 0
      .Width = twipFactor * sWide
      .Height = twipFactor * stall
   End With
   With Picture2
      .Top = 0
      .Left = 0
      .Width = twipFactor * sWide
      .Height = twipFactor * stall
   End With
   With Label1
      .Caption = "Top"
      .Left = Me.Width / 2
      .Top = 0
   End With
   With Label2
      .Caption = "Bottom"
      .Top = (twipFactor * stall) - .Height * 2
      .Left = Me.Width / 2
   End With
   Me.Visible = True
   DoEvents

   Picture1.SetFocus
   Picture2.AutoRedraw = True
   rv = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hDC, 0)
   rv = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hDC, _
   PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)
   Picture2.Picture = Picture2.Image
   Picture2.AutoRedraw = False

   Printer.Print ""
   Printer.PaintPicture Picture2.Picture, 0, 0
   Printer.EndDoc
End Sub

Run the project.
The Top and Bottom labels should appear in their
respective positions regardless of whether the form is
completely displayed.

Print a Form on More Than One Page
If the form you are printing is too large to fit on a
page, you can use the same approach as in the preceding
example with a minor addition. The PaintPicture method has
a number of optional parameters. The clipping region
parameters can be used to grab a portion of the bitmap
image and print it.

To accomplish this, locate the following code in the
previous example:

Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0
Printer.EndDoc
Replace those three lines of code with the following:

Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0, , , _
   0, 0, , Picture2.Height / 2
Printer.NewPage
Printer.PaintPicture Picture2.Picture, 0, 0, , , _
   0, Picture2.Height / 2, , Picture2.Height / 2
Printer.EndDoc

For ease of illustration, this example merely divides the
form in half. You can adjust the clipping region
appropriately for your needs.

NOTE: Visual Basic 6.0 Online Help states that PrintForm
prints the entire form, whether the entire form is visible
or not. This information is incorrect.

REFERENCES
For additional information about printing forms, click the article numbers below to view the articles in the Microsoft Knowledge Base:

Q178076 HOWTO: Use a PictureBox to Control Orientation Printing a Form
Q161299 HOWTO: Capture and Print the Screen, a Form, or Any Window
Q194580 HOWTO: Print a Composite Image From a RichTextBox
Q146022 HOWTO: Set Up the RichTextBox Control for WYSIWYG Printing
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 8194723
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Fischermaen
ID: 8194740
Just take the PrintForm-Methode from your form.

e.g.: MyForm.PrintForm
0
 

Expert Comment

by:avirama2
ID: 8194749
   Printer.Font.Name = TextBox.Font.Name
    Printer.Font.Bold = TextBox.Font.Bold
    Printer.Font.Charset = TextBox.Font.Charset
    Printer.Font.Italic = TextBox.Font.Italic
    Printer.Font.Size = TextBox.Font.Size
    Printer.Font.Strikethrough = TextBox.Font.Strikethrough
    Printer.Font.Underline = TextBox.Font.Underline
    Printer.Font.Weight = TextBox.Font.Weight
 Printer.Print TextBox.text
0
 
LVL 11

Expert Comment

by:supunr
ID: 8194759
how about simply using the command....

PrintForm ' print the form as it is directly to the printer.

Good Luck!
0
 

Expert Comment

by:CleanupPing
ID: 8531741
Hi MattRawling,
This old question (QID 20560858) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9441191
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->PAQ - no points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9441192
make a claim
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9495956
This question is PAQed and no points refunded (of 25)

YensidMod
Expert Exchange Moderator
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question