C # text file print example 3

xiaoxiao2021-03-06  56

/ / -------------------------------------------------------------------------------------------- ------------------------------ // // / copyright (c) Microsoft Corporation. all rights reserved. ///// This source code is only supplemented by Microsoft development tools and / or online documentation ///. For more information on the Microsoft code example, please read these additional information. ////// // -------------------------------------- ---------------------------------------- Namespace Microsoft.Samples.winForms.cs.printingexample3 {Using system.componentmodel; using system.drawing; using system.drawing.printing; using system.io;

Public class printform: system.windows.Forms.form {///

// The required designer variable. /// private system.componentmodel.container Components; protected innal system.windows.forms.button printbutton;

Public PrintForm () {// // Windows Form Designer Support for // InitializationComponent ();

// Start the event handler of the print button PrintButButton.click = new system.eventhandler (printbutton_click);

///

/// Clean all resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

// Event void printButton_Click (Object Sender, Eventargs E) {Try {in the user presses the print button

StreamReader streamToPrint = new StreamReader ( "PrintMe.Txt"); try {TextFilePrintDocument pd = new TextFilePrintDocument (streamToPrint); // assumed default printer PrintDialog dlg = new PrintDialog (); dlg.Document = pd; DialogResult result = dlg.ShowDialog ();

IF (result == DialogResult.ok) {pd.print ();}

} Finally {streamtoprint.close ();

} Catch (exception ex) {messagebox.show ("error occurred while printing file -" ex. amount);}}

///

/// Designer supports the necessary methods, do not use the /// code editor to modify the contents of this method. /// private void initializecomponent () {this.components = new system.componentmodel.container (); this.printButton = new system.windows.Forms.Button ();

THIS.TEXT = "Print Example 3"; this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (504, 381); printbutton.imageAlign = system.drawing. ContentAlignment.MiddleLeft; printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; printButton.Size = new System.Drawing.Size (136, 40); printButton.TabIndex = 0; printButton.Location = new System.Drawing.Point (32, 112); PrintButton.text = "Print file"; this.controls.add (this.printbutton);}

///

/// The main entry point of the application. /// [stathread] public static void main (string [] args) {Application.run (new printform ());}}

// // // textFilePrintDocument outputs stream to printer // // Note: To avoid problems when the file is turned off, // If exception occurs, this class uses only one stream // and will Leave the caller to open this file // // // // // public class textfileprintDocument: printDocument {private font printfont = null; private streamreader streamtoprint = null;

Public TextFilePrintDocument: base () {this.streamtoprint = streamtoprint;}

// Override OnBeginPrint to set the font protected overrid onbeginprint (printeventargs ev) {base.onbeginprint (ev); printfont = new font ("arial", 10);}

// Override OnPrintPage to provide documentation to provide print logic protected override void onprintpage (PrintPageEventArgs EV) {

Base.onPrintPage (EV);

FLOAT LPP = 0; float ypos = 0; int count = 0; float leftmargin = ev.Marginbounds.Left; float TopMargin = ev. marginbounds.top; string line = null;

// Row number of rows per page // Use marginbounds on the event to reach this LPP = ev.MarginBounds.Height / PrintFont.getHeight (ev.graphics);

// Now, repeat this operation on the file to output per row // Note: Assume that a single line is narrow // first check the number of rows in order to see the line of lines that do not printed ((line = streamtoprint. Readline ())! = NULL)) {YPOS = TopMargin (Count * PrintFont.getHeight (ev.graphics));

Ev.graphics.drawstring (Line, PrintFont, Brushes.black, leftmargin, ypos, new stringformat (); count ;}

// If there is a multi line, another page IF (line! = Null) ev.hasmorepages = true; Else ev.hasmorepages = false;}

}

}

转载请注明原文地址:https://www.9cbs.com/read-116717.html

New Post(0)