C # Printing and C # Print preview implementation of the three major parts: Page setup, print Preview, print specific use of the class and attribute events are what content? So this article will introduce you to the implementation of C # printing and C # print preview.
We talked about the implementation of C # printing and C # Print preview, mainly including: Page setup, print preview, printing three parts. The principle of printing is to generate MDI files that are automatically processed when the system touches the MDI. So, no matter what template, what way; in PrintPage event processing, generate a picture to print the content is OK! The primary support classes are included in the namespace System.Drawing.Printing and implemented in assembly System.Drawing.dll. The implementations of C # printing and C # Print preview are mainly done through the PrintDocument class, plus several auxiliary classes: PrintDialog (Print dialog), PrintPreviewDialog (Print Preview dialog box), PageSetupDialog.
The PrintDocument class is the central class for printing and print previews, and the actual printing operation is done through this class. The process of printing using this class:
1) Create class instance PrintDocument Printdt = new PrintDocument ();
2) Set the properties of the class instance
3) Add event handlers for this class of print events
4) Call the print function of this class for printing.
Printed: Printdt.print ();
The printing of documents in Windows applications is a very important feature that has been a very complex task in the past, and the Microsoft. Net Framework's printing capabilities are provided in a component way, providing programmers with great convenience, But the use of these components is still very complex, it is necessary to explain.
A print operation typically includes the following four features:
1 printing settings Set some parameters of the printer, such as changing the printer driver;
2 Page Setup Settings page size paper type etc.
3 Print Preview is similar to print preview in Word
4 Printing
The core of the printing function is the PrintDocument class, which belongs to the System.Drawing.Printing namespace, which encapsulates the current Print Setup page setup and all the printing-related events and methods
This class includes the following properties, events, and methods
1. PrinterSettings Properties
The setting information for the printer, which is not required by the programmer, because it is obtained by the Print dialog.
2. Printcountroller Properties
Controlling the printing process
3. DefaultPageSettings Properties
Store Page setup information, paper size direction, etc., and do not need programmer settings, because it is obtained by the Page Setup dialog box.
4. DocumentName Properties
Specifies the document name, which appears in the Printer status window
1. BeginPrint Events
Issue before printing
2. PrintPage Events
Each printed page is emitted, and the event accepts a PrintPageEventArgs parameter that encapsulates the printing-related information
The PrintPageEventArgs parameter has many important properties
1 Cancel Printing
2 drawing objects for graphics pages
3 HasMorePages If there are any pages to print
Print method: The method does not have parameters called it will start printing according to the current settings.
If you implement the Print function, first construct the PrintDocument object to add the Print event
1:printdocument PrintDocument;
2:private void InitializeComponent ()
3: {
4: ...
5: //The PrintDocument object here can be implemented by dragging and dropping the PrintDocument control onto the form, noting that the PrintPage event of the control is to be set.
6: printdocument=new PrintDocument ();
7: Printdocument.printpage + = new Printpageeventhandler (this.printdocument_printpage);
8: ...
9:}
Implementing the Print events feature
Printing is similar to drawing a method of calling the graphics class to paint differently is one on the monitor on the print paper and printing to do some complicated calculations
such as line breaks, pagination, and so on.
1:private void Printdocument_printpage (Object Sender,printpageeventargs e)
2: {
3: Graphics g = e.graphics;//Get drawing Object
4: Float linesperpage = 0;//Line number of the page
5: Float yposition = 0; Draw the vertical position of a string
6: int count = 0;//Row counter
7: Float leftMargin = e.marginbounds.left;//left margin
8: Float topMargin = e.marginbounds.top;//Top margin
9: string line = null;
: Font printfont = this.textBox.Font;//Current print font
One: solidbrush mybrush = new SolidBrush (color.black);//brushes
: linesperpage = E.marginbounds.height/printfont.getheight (g);//number of rows per page that can be printed
: //Line-by-loop printing one page
+: While (Count < linesPerPage && ((line=linereader.readline ()) = null))
: {
: yposition = TopMargin + (count * printfont.getheight (g));
: g.drawstring (line, Printfont, MyBrush, LeftMargin, YPosition, New StringFormat ());
: count++;
: }
: //NOTE: Before using this code, define the Linereader object in the form's class:
: // StringReader linereader = null;
: //If this page is printed and line is not empty, then there are still unfinished pages, which will trigger the next print event. In the next print, Linereader will
: //Automatically reads content that was not printed last, because Linereader is a member of the class outside of this printing method, it can record the current read location
: if (line! = null)
: e.hasmorepages = true;
: Else
: {
: e.hasmorepages = false;
: //Reinitialize the Linereader object, or print out a blank page using the Print button in print preview
: linereader = new StringReader (textbox.text);//textbox is the content of the text box you want to print
: }
32:}
print Settings , construct the Print dialog box to assign the document property set in the dialog box to PrintDocument this will automatically save the user's settings to PrintDocument
In the PrinterSettings property
1:protected void Filemenuitem_printset_click (Object Sender,eventargs e)
2: {
3: PrintDialog PrintDialog = new PrintDialog ();
4: printdialog.document = PrintDocument;
5: printdialog.showdialog ();
6:}
Page Setup and print preview are the same as print settings, which are constructed by the dialog box to save the user's settings in the dialog box to the properties of the corresponding class
1:protected void Filemenuitem_pageset_click (Object Sender,eventargs e)
2: {
3: PageSetupDialog PageSetupDialog = new PageSetupDialog ();
4: pagesetupdialog.document = PrintDocument;
5: pagesetupdialog.showdialog ();
6:}
Print Preview
1:protected void Filemenuitem_printview_click (Object Sender,eventargs e)
2: {
3: PrintPreviewDialog PrintPreviewDialog = new PrintPreviewDialog ();
4: printpreviewdialog.document = PrintDocument;
5: linereader = new StringReader (textbox.text);
6: Try
7: {
8: printpreviewdialog.showdialog ();
9: }
Ten: catch (Exception excep)
One: {
: MessageBox.Show (excep. Message, "Print error", MessageBoxButtons.OK, Messageboxicon.error);
: }
14:}
printing can call PrintDocument's print () method directly because the user may have to change the print settings before printing.
The Print Settings dialog box is displayed again here
1:protected void Filemenuitem_print_click (Object Sender,eventargs e)
2: {
3: PrintDialog PrintDialog = new PrintDialog ();
4: printdialog.document = PrintDocument;
5: linereader = new StringReader (textbox.text);
6: if (printdialog.showdialog () = = DialogResult.OK)
7: {
8: Try
9: {
Ten: printdocument.print ();
One: }
: catch (Exception excep)
: {
: MessageBox.Show (excep. Message, "Print error", MessageBoxButtons.OK, Messageboxicon.error);
: printDocument.PrintController.OnEndPrint (Printdocument,new PrintEventArgs ());
: }
: }
: }
The process of summarizing printing is
1 Constructing the PrintDocument object when the application form is initialized, adding the PrintDocument PrintPage method
2 Implementing the PrintPage method
3 print function is called by the PrintDocument Print method in the user's Click event
In the middle of this, you might want to use PrintDialog PrintPreviewDialog PageSetupDialog to set up and view the print effects These methods are usually triggered by a menu click.
This article from: Zhongke Software Park (www.4oa.com) Detailed source reference: http://www.4oa.com/Article/html/6/34/501/2005/18114.html
The author made some modifications and comments.
C # print and print preview