C # Printing

Source: Internet
Author: User

Zamenhof smell dog bark, snow night return to people. Wild Day Low tree, Chiang month neighbor. Fragrant fog cloud make matters worse wet, shining jade arm cold. Don't worry about the road without bosom friend, the world who does not know June. Often will be cold-shouldered
Look at the crab and see how long you've been on the rampage. To implement the most basic print text in vs2005, you basically need the following steps

1 new Project (form)

2 Adding a PrintDocument control on a form add a Button to a TextBox control

Add code to the PrintPage event in the PrintDocument control room

E.graphics.drawstring (textbox1.text,new Font ("Arial", FontStyle.Bold), Brushes.black, 150, 125);

4 Add the Click event for the button control to add the following code in the event

1 New // Declaration Definition Print dialog box 3 printdialog1.document = printDocument1; 5 DialogResult result = printdialog1.showdialog (); 7 if (Result = = DialogResult.OK) 8  9printdocument1.print (); Ten }   

5 Adding a EndPrint event to a PrintDocument control such as a hint after printing technology

6 the "page margins and paper orientation settings to use for PageSetupDialog class" instructions in MSDN personally think the settings are not very convenient because the previous
Printdilog instance PrintDilog1 is created, so you can change the way you print by changing some of the default settings for the dialog box, for example: The following example sets
The default print orientation is landscape

PrintDialog1.Document.DefaultPageSettings.Landscape = true;

The printing capabilities of the Microsoft. NET Framework are provided as components, providing great convenience to programmers, but the use of these components is still complex
, it is 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. This class encapsulates the current Print Setup page settings to
And all the 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 that appears in the Printer Status window 1. BeginPrint event is emitted before printing
2. PrintPage event Each printed page is emitted, the event accepts a PrintPageEventArgs parameter this parameter encapsulates 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

1 PrintDocument PrintDocument; Private void InitializeComponent () 2 {3... 4 printdocument=new  PrintDocument (); 5 New Printpageeventhandler (this. printdocument_printpage); 6 ... 7 }   

Implementing the Print Events feature printing and drawing is similar to calling the graphics class method to paint differently is one on the monitor on the print paper and printing to go in
Line some complex calculations such as line breaks and so on.

1 Private voidPrintdocument_printpage (ObjectSender,printpageeventargs e)2 {3Graphics g = e.graphics;//Get drawing Objects4     floatlinesPerPage =0;//line number of the page5     floatYPosition =0;//draw the vertical position of a string6     intCount =0;//Line Counter7     floatLeftMargin = E.marginbounds.left;//left margin8     floatTopMargin = E.marginbounds.top;//Top Margin9     stringline =NULL;//Line StringTenFont Printfont = This. Textbox.font;//the current print font OneSolidBrush MyBrush =NewSolidBrush (Color.Black);//Brushes AlinesPerPage = 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)) -     { theYPosition = TopMargin + (count *Printfont.getheight (g)); -g.DrawString (line, Printfont, MyBrush, LeftMargin, YPosition,NewStringFormat ()); -count++; -}

If the 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 the last
No content is printed because Linereader is a member of a class outside of this printing method it can record the current read location

1 if NULL )2true; 3 Else 4 false ; 5 }

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{3New  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_printset_click (object  sender,eventargs e)2{3New  PrintDialog (); 4 printdialog.document = PrintDocument; 5 Printdialog.showdialog (); 6 }

Print Preview

1 protected voidFilemenuitem_printview_click (ObjectSender,eventargs e)2 {3PrintPreviewDialog PrintPreviewDialog =NewPrintPreviewDialog (); Printpreviewdialog.document =PrintDocument;4Linereader =NewStringReader (textbox.text);5 Try6 {Printpreviewdialog.showdialog ();}7 Catch(Exception excep)8 { 9MessageBox.Show (EXCEP. Message,"Print Error", MessageBoxButtons.OK, messageboxicon.error);Ten } One}

Printing can call PrintDocument's print () method directly because the user may have to change the print settings before printing, so here again the print settings are displayed
Words box

protected voidFilemenuitem_print_click (ObjectSender,eventargs e) {PrintDialog PrintDialog=NewPrintDialog ();p rintdialog.document=Printdocument;linereader=NewStringReader (textbox.text);if(Printdialog.showdialog () = =DialogResult.OK) {Try{printdocument.print ();}Catch(Exception excep) {MessageBox.Show (excep. Message,"Print Error", Essageboxbuttons.ok, Messageboxicon.error); PrintDocument.PrintController.OnEndPrint (PrintDocument,NewPrintEventArgs ()); }}}

The process of summarizing printing is

1 PrintDocument object is constructed when the application form is initialized PrintPage method for adding PrintDocument
2 Implementing the PrintPage method
4 The Print method of PrintDocument is called in the user's Click event to implement the printing function it may be used in the middle of the PrintDialog
PrintPreviewDialog PageSetupDialog setting and viewing print effects These methods are usually triggered by a menu click.
Street light rain run as crisp, grass color remote look near but no. Drunken lying on the battlefield June Mo Smile, ancient battle several people back! Snow outside the door of Qian Shan Green, flowers and river side of the fine. A letter is not a beautiful word.
Letter

C # Printing

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.