C # WindowsForm Printing

Source: Internet
Author: User

The printing of documents in Windows applications is a very important feature that has been a very complex work in the past, and the Microsoft. NET Framework is playing

Printing functions are provided as components, providing great convenience to programmers, but the use of these components is complex and necessary to explain.

Print operations typically include the following four functions

1 print settings Set some parameters of the printer, such as changing the printer driver, etc.

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 the

Some printing-related events and methods

This class includes the following property events and methods

1. PrinterSettings Properties

Store settings information for the printer this property does not require programmer setup because it is obtained by the Print dialog box

2. Printcountroller Properties

Controlling the printing process

3. DefaultPageSettings Properties

Store page settings information paper size direction and so also 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

The Print method does not have a parameter call to the method that will start printing according to the current settings

If you implement the Print function, first construct the PrintDocument object to add the Print event

PrintDocument PrintDocument;

private void InitializeComponent ()

{

...

Printdocument=new PrintDocument ();

Printdocument.printpage + = new Printpageeventhandler (this.printdocument_printpage);

...

}

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 and so on.

private void Printdocument_printpage (Object Sender,printpageeventargs e)

{

StringReader linereader = new StringReader (textbox.text);

Graphics g = e.graphics; Get drawing objects

float linesperpage = 0; Line number of the page

float yposition = 0; Draw the vertical position of a string

int count = 0; Line counter

float leftMargin = e.marginbounds.left; Left margin

float topMargin = e.marginbounds.top; Top margin

string line = null; Line string

Font Printfont = This.textBox.Font; The current print font

SolidBrush MyBrush = new SolidBrush (color.black);//brushes

linesPerPage = E.marginbounds.height/printfont.getheight (g);//number of rows per page that can be printed

Print a page by line loop

while (Count < linesPerPage && ((line=linereader.readline ()) = null))

{

YPosition = TopMargin + (count * printfont.getheight (g));

g.DrawString (line, Printfont, MyBrush, LeftMargin, YPosition, New StringFormat ());

count++;

}

If this page is printed and line is not empty, then there are unfinished pages. This will trigger the next print event in the next print Linereader will

Automatically read content that was not last printed because Linereader is a member of a class outside of this printing method it can record the current read location

if (line! = null)

E.hasmorepages = true;

Else

E.hasmorepages = false;

}

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

protected void Filemenuitem_printset_click (Object Sender,eventargs e)

{

PrintDialog PrintDialog = new PrintDialog ();

Printdialog.document = PrintDocument;

Printdialog.showdialog ();

}

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

protected void Filemenuitem_pageset_click (Object Sender,eventargs e)

{

PageSetupDialog PageSetupDialog = new PageSetupDialog ();

Pagesetupdialog.document = PrintDocument;

Pagesetupdialog.showdialog ();

}

Print Preview

protected void Filemenuitem_printview_click (Object Sender,eventargs e)

{

PrintPreviewDialog PrintPreviewDialog = new PrintPreviewDialog ();

Printpreviewdialog.document = PrintDocument;

Try

{

Printpreviewdialog.showdialog ();

}

catch (Exception excep)

{

MessageBox.Show (EXCEP. Message, "Print error", MessageBoxButtons.OK, Messageboxicon.error);

}

}

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

protected void Filemenuitem_print_click (Object Sender,eventargs e)

{

PrintDialog PrintDialog = new PrintDialog ();

Printdialog.document = PrintDocument;

Linereader = new StringReader (textbox.text);

if (printdialog.showdialog () = = DialogResult.OK)

{

Try

{

Printdocument.print ();

}

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 PrintDocument object is constructed when the application form is initialized PrintPage method for adding PrintDocument

2 Implement PrintPage Method 4 print function is called in the user's Click event by calling the PrintDocument Print method

In this middle you may want to use the PrintDialog PrintPreviewDialog PageSetupDialog to set and view the printing effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.