B41830C
Enables the printing function (using C #, PrintDialog, PrintDocument's) in C # Author: Shao make 2004-8-24 17:04:01 blog China (Blogchina.com) in C # using PrintDialog can easily achieve the print function of the program . The steps are as follows:
Create an instance of PrintDialog. As follows: System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog (); create an instance of a PrintDocument as follows: System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument (); set the printer to start printing event. Processing function. The function is as follows: void doctoprint_printpage (Object sender, system.drawing.printing.printpageEventargs E) Add the event handler to the PrintDocument's PrintPage event. docToPrint.PrintPage = new PrintPageEventHandler (docToPrint_PrintPage); PrintDocument set of related attributes, such as: PrintDialog1.AllowSomePages = true; PrintDialog1.ShowHelp = true; Document property of the PrintDialog to the above configuration example of good PrintDocument: PrintDialog1.Document = docToPrint Call PrintDialog's showdialog function Display print dialog: DialogResult result = printdialog1.showdialog (); according to user selection, start printing: if (result == DialogResult.ok) {doctoprint.print ();} example: use Create an instance of the PrintService class, then call the Void StartPrint (String StreamType "function to start printing. Where STREAMTOPRINT is the content (byte stream) to be printed, streamType is the type of stream (txt represents normal text, Image represents images);
Using system; using system.drawing.printing; using system.windows.forms; using system.io; namespace edimagesystem {///
PrintDialog1.AllowSomePages = true;. // Show the help button PrintDialog1.ShowHelp = true;. // Set the Document property to the PrintDocument for // which the PrintPage Event has been handled To display the // dialog, either this property or the PrinterSettings property // must be set PrintDialog1.Document = docToPrint; // set the configuration properties of the Document PrintDialog good PrintDocument above examples DialogResult result = PrintDialog1.ShowDialog (); // call the PrintDialog function displays the print dialog ShowDialog // If the result is OK then print the document if (result == DialogResult.OK). {docToPrint.Print (); // start printing}} // The PrintDialog will print the document // by handling the document's PrintPage event . private void docToPrint_PrintPage (object sender, System.Drawing.Printing.PrintPageEventArgs e) // set the printer starts printing the event handler {// Insert code to render the page here. // This code will be called when the control is drawn . // The Following Code Will Render A Simple // Message on The Printed Document Swit CASE "TXT": String text = null; system.drawing.font printfont = new system.drawing.font ("arial", 35, system.drawing.fontstyle.regular; // Draw THE content System.IO.StreamReader streamReader = new StreamReader (this.streamToPrint);. text = streamReader.ReadToEnd (); e.Graphics.DrawString (text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e .MARGINBOUNDS.Y); BREAK; CASE "Image": system.drawing.image image = system.drawing.image.fromstream (this.streamtoprint); int x = E.MarginBounds.x; int y = E.Marginbounds.y Int width = image.width; int Height = image.height;