-
February 3rd, 2005, 03:14 PM
#1
Capture Screen vs. Printing Forms
Alright. I've resolved most of the issues in my earlier posts about printing forms.
However, I've found that the code that I am using to print the form functions more as capture and print screen operation. I'm trying to print the entirety of the form (it's a lengthy form with a scroll bar)....not just the part that appears on the screen.
Does anyone know how I can change the following code to do that?
Private Sub pd_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles pd.PrintPage
e.Graphics.DrawImage(formImage, 0, 0)
pd.DefaultPageSettings.Landscape = True
End Sub
Private Sub GetFormImage()
Dim g As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
formImage = New Bitmap(s.Width, s.Height, g)
Dim mg As Graphics = Graphics.FromImage(formImage)
Dim dc1 As IntPtr = g.GetHdc
Dim dc2 As IntPtr = mg.GetHdc
' added code to compute and capture the form
BitBlt(dc2, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height, dc1, 0, 0, 13369376)
g.ReleaseHdc(dc1)
mg.ReleaseHdc(dc2)
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' create an instance of the PrintDocument component
pd = New Printing.PrintDocument
Me.StartPosition = FormStartPosition.CenterScreen
pd.DefaultPageSettings.Landscape = True
End Sub
Private Sub Print_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles Print.Click
GetFormImage()
pd.Print()
End Sub
-
February 3rd, 2005, 04:18 PM
#2
Re: Capture Screen vs. Printing Forms
Sorry, don't think you can do it. Things that aren't visible on the screen don't exsist on the graphics device used to show the form. This includes portions of the screen outside the monitor's bounds as well as stuff above or below the form causing the need for scrollbars.
-
February 3rd, 2005, 05:56 PM
#3
Re: Capture Screen vs. Printing Forms
But I've noticed that, if I have the form scrolled all the way to the bottom and I select Print from the menu, using the code I have in the former post, it only prints the very top of the form, even though it is not visible on the monitor. This seems to suggest that you can use the Graphics device to print something that is not visible on the monitor. Admittingly, I'm relatively new to VB.NET, but I can't make sense of this.
-
February 3rd, 2005, 06:53 PM
#4
Re: Capture Screen vs. Printing Forms
Disregard that last post. I was incorrect. It does not print the top of the form even when I call the print method while the bottom of the form is showing on the monitor.
However, is there any way at all to print out the entirety of the form. The form asks the user for input through various textboxes, comboboxes, and radiobuttons. It is the equivalent of two printed pages. I want to have in print out with all the users input when the user has completed the form.
Any help would be greatly appreciated.
-
February 3rd, 2005, 09:57 PM
#5
Re: Capture Screen vs. Printing Forms
Just use a series of inherited forms instead, that the user has to click "next" through. Then at the end you can print all the forms in one print job.
-
February 4th, 2005, 09:30 AM
#6
Re: Capture Screen vs. Printing Forms
Or you can give up on trying to print an actual image of the screen and write a report to do the job thru code.
-
February 4th, 2005, 09:34 AM
#7
Re: Capture Screen vs. Printing Forms
Can you point me to a good resource on how to do this. I'm new to VB.NET and all the reference materials I've used are thin when it comes for explanations on printing or creating those reports.
I appreciate the help.
-
February 4th, 2005, 10:24 AM
#8
Re: Capture Screen vs. Printing Forms
You just have to write your own sub procedure that formats and prints all the information. Make a button at the very end of the form that says "print" or "submit" or whatever. In the code for that button, have it call a function that formats all the data that was inputed. Then have it call a sub that takes the formated data and prints it. For example, say I have a simple form with just two textboxes, which are labeled "input name" and "input age". And then a button that says submit. Here's what the code should look like:
Code:
Private Sub btnSubmit_Click (sender,e) Handles btnSubmit.Click
Dim strFormatedData As String = FormatData()
Call PrintData(strFormatedData)
End Sub
Private Function FormatData() As String
Dim strName As String = "Your name is: " & txtName.Text
Dim strAge As String = "Your age is: " & txtAge.Text
Dim strFormatData = strName & ControlChars.Newline & strAge
Return strFormatData
End Function
Private Sub PrintData(IncomingData)
'Here is where you call your print dialog to print the formated string
'the code should be simple enough
End Sub
Last edited by Ch0pinZee; February 4th, 2005 at 10:25 AM.
Reason: forgot closing code tag
-
February 4th, 2005, 10:38 AM
#9
Re: Capture Screen vs. Printing Forms
Great. That clears up a lot issues. I've looked through probably about 8 different books and none of them just spelled it out like that.
One more thing though: How do I deal with formatting? For example, how do I create indents, spacing, next line commands? Also, how do I change font for individuals sections....if I want one line bolded but the next line normal.
Thanks for all the help here. It's greatly appreciated.
-
February 4th, 2005, 11:14 AM
#10
Re: Capture Screen vs. Printing Forms
Use ConrolChars.NewLine for a carriage return. Just concatenate it with a string.
As far as creating bold text and changing fonts, you can't really do that with the code I gave you because it just formats all the data as one big string with line returns. You can, however, add html tags to this string, and have it save to an html file. For example, replace the FormatData() funtion with this one:
Code:
Private function FormatData() As String
Dim strName As String = "<font face="arial"><b>" & "Your name is: " & "</font></b>" & txtName.Text
End Function
Then in your sub, use a save dialog instead of a print dialog, and have it save the file as type .html.
-
February 4th, 2005, 11:16 AM
#11
Re: Capture Screen vs. Printing Forms
It's going to take a little work, time and effort. But if you look at the Graphics class, it's Drawxxxxx() methods all take point, font, brush, etc. arguments to position and format the text.
-
February 4th, 2005, 12:21 PM
#12
Re: Capture Screen vs. Printing Forms
If I use the html format, do I still have this statement at the bottom of the FormatData function:
Dim strFormatData = strName & ControlChars.Newline & strAge
Return strFormatData
-
February 4th, 2005, 01:32 PM
#13
Re: Capture Screen vs. Printing Forms
Yes. My code above was incomplete, because I thought you'd get the idea. The complete code would be:
Code:
Private Function FormatData() As String
Dim strName As String
Dim strAge As String
strName = "<font face=""arial""><b>" & "Your name is: " & "</font></b> & txtName.Text & "<br>"
strAge = "<font face=""arial""><b>" & "Your age is: " & "</font></b> & txtAge.Text
strFormatData = strName & ControlChars.NewLine & strAge
Return strFormatData
End Function
The double quotation marks are necessary in the part that says ""arial"", because that is the syntax for using a quotation mark as part of a string. Also I added a <br> tag because that is the line-return tag in html.
-
February 4th, 2005, 02:44 PM
#14
Re: Capture Screen vs. Printing Forms
Just two more questions and then I should be done:
First, on my form some of the labels have numbers that get plugged in there from calculations. The labels are formated to only show two decimal places on the screen. Is there a way to keep that two decimal display format on this?
Also, for saving it as an html, do I have to put any of the other standard html code that I would have to use in notepad, such as beginning it with <HTML> and ending with </HTML>
Thanks a lot. This has been extraordinarily helpful.
-
February 4th, 2005, 04:06 PM
#15
Re: Capture Screen vs. Printing Forms
1. Yes, it will stay that way automatically, because when you formated the number it automatically converted it to a string.
2. Yes. At the beginning of the final string put in:
Code:
<html><head><title>Summary</title></head><body>
But change the part that says "Summary" to whatever you wish, and at the end of the string, put in:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|