Printing of. NET Framework in MIS Development

Source: Internet
Author: User
The printing function of Microsoft. NET Framework is provided as a component. Program Personnel provide a lot of convenience, but the use of these components is still very complex, it is necessary to explain.

Print operations generally include the following four functions

1. Print and set some printer parameters, such as changing the printer driver.

2. Set page size and paper type on the page

3. print preview is similar to printing preview in word.

4 print

The core of printing is the printdocument class, which belongs to the system. Drawing. Printing namespace class. This class encapsulates the current printing settings page settings and

Some printing-related events and Methods

This class includes the following attribute events and Methods

1. printersettings Properties

This attribute does not need to be set by the programmer because it is obtained by the Print dialog box.

2. printcountroller attributes

Control the printing process

3. defaultpagesettings attributes

You do not need to set the size and direction of the printed paper because it is obtained by the page Setting dialog box.

4. documentname attributes

The specified document name appears in the printer status window.

1. beginprint event

Issue before printing

2. printpage event

Each printed page is sent, and the event receives a printpageeventargs parameter. This parameter encapsulates printed information.

The printpageeventargs parameter has many important attributes.

1 cancel printing

2 graphics page Drawing Object

3. Does hasmorepages have pages to print?

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

If the print function is implemented, the printdocument object is constructed first to add a print event.

 

Printdocument;
Private void initializecomponent ()
{
...
Printdocument = new printdocument ();
Printdocument. printpage + = new printpageeventhandler (this. printdocument_printpage );
...
}
Event Printing
Printing and plotting are similar to calling graphics methods for drawing. Different from drawing on a display, printing on a printing paper requires some complex calculations.
Such as line feed pages.
Private void printdocument_printpage (Object sender, printpageeventargs E)
{
Graphics G = E. graphics; // obtain the Drawing Object
Float linesperpage = 0; // The row number of the page
Float yposition = 0; // draw the vertical position of the string
Int COUNT = 0; // The row 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; // current print font
Solidbrush mybrush = new solidbrush (color. Black); // brush
Linesperpage = E. marginbounds. Height/printfont. getheight (g); // Number of printable lines per page
// Print one page in a row-by-row 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 the page is printed and the line is not empty, the page is still incomplete. This will trigger the next printing event. In the next printing, linereader will

Automatically reads the last printed content because linereader is a member of the class outside the printing method and can record the current read location.

 

If (line! = NULL)
E. hasmorepages = true;
Else
E. hasmorepages = false;
}

 

Print settings. In the print structure dialog box, the document attribute set in the dialog box is assigned to printdocument. This will automatically save your settings to printdocument.

In the printersettings attribute

 

Protected void filemenuitem_printset_click (Object sender, eventargs E)
{
Printdialog = new printdialog ();
Printdialog. Document = printdocument;
Printdialog. showdialog ();
}

 

The principle of page setting and print preview is the same as that of print setting. The create dialog box saves the settings in the dialog box to the attributes of the corresponding class.

 

Protected void filemenuitem_pageset_click (Object sender, eventargs E)
{
Pagesetupdialog = new pagesetupdialog ();
Pagesetupdialog. Document = printdocument;
Pagesetupdialog. showdialog ();
}

 

Print preview

 

Protected void filemenuitem_printview_click (Object sender, eventargs E)
{
Printpreviewdialog = new printpreviewdialog ();
Printpreviewdialog. Document = printdocument;
Linereader = new stringreader (textbox. Text );
Try
{
Printpreviewdialog. showdialog ();
}
Catch (exception excep)
{
MessageBox. Show (excep. message, "print error", messageboxbuttons. OK, messageboxicon. Error );
}
}

 

Print, you can directly call the print () method of printdocument because you may need to change the print settings before printing.

The print Settings dialog box is displayed again.

 

Protected void filemenuitem_print_click (Object sender, eventargs E)
{
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 summary printing is

1. Construct the printpage Method for adding printdocument to the printdocument object during application form Initialization

2 implement printpage Method 4 call the print method of printdocument in the user's Click Event to implement the printing function

Printdialog printpreviewdialog pagesetupdialog may be used to set and view the print effect.

These methods are usually triggered by a menu click.

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.