The printdocument control sets the printed document. In the printdocument control, the printpage event and print method of the control are common. The printpage event occurs when the output of the current page needs to be printed. Call the print method to start the document printing process. The following example shows how to use the printdocument control.
For example, create a Windows application and add a button control, a printdocument control, and a printpreviewdialog control to the form. In the printpage event of the printdocument control, draw the printed content, set the printpreviewdialog attribute under the Click Event of the button to preview the printed document, and call the print method of the printdocument control to start the printing process of the document, the Code is as follows.
Private void printdocumentemediprintpage (Object sender, system. Drawing. Printing. printpageeventargs E)
{
// Draw and print the document through GDI +
E. Graphics. drawstring ("Butterfly", new font ("", 15), brushes. Black, 350, 80 );
E. Graphics. drawline (new pen (color. Black, (float) 3.00), 100,185,720,185 );
E. Graphics. drawstring. ", New font (" ", 12), brushes. Black, 110,195 );
E. Graphics. drawstring. ", New font (" ", 12), brushes. Black, 110,220 );
E. Graphics. drawstring ("it is intended to get the crazy figure drunk. It is boring to play with wine. ", New font (" ", 12), brushes. Black, 110,245 );
E. Graphics. drawstring ("I don't regret it. It's embarrassing for Iraq. ", New font (" ", 12), brushes. Black, 110,270 );
E. Graphics. drawline (new pen (color. Black, (float) 3.00), 100,300,720,300 );
}
Click the button to view the current event
Private void button#click (Object sender, eventargs E)
{
If (MessageBox. Show ("whether to preview printed documents", "print preview", messageboxbuttons. yesno) = dialogresult. Yes)
{
// Enable the anti-tooth feature of the Operating System
This. printpreviewdialog1.useantialias = true;
// Set the document to be previewed
This. printpreviewdialog1.document = This. printdocument1;
// Open the preview window
Printpreviewdialog1.showdialog ();
}
Else
{
// Call the print method to print the document directly
// This. printdocument1.print ();
}
}
Run the program and click "print" to bring up the "print preview" form ,.