How to use the PageSettings object and the PagesetupDialog control to allow users to choose various print options

xiaoxiao2021-03-06  54

/ / -------------------------------------------------------------------------------------------- ------------------------------ // // / 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.printingexample4 {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 interface system.windows.Forms.Button PrintButton; protected infston pagesetupbutton;

Private Pagesettings StoredPageSettings = NULL;

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

// The event handler of the start button PagesetupButton.Click = new system.EventHandler (PagesetupButton_Click); PrintButton.click = new system.eventhandler (printbutton_click);}

// Event void PagesetupButton_Click (Object Sender, Eventargs E) when the user presses the page setting button

Try {PageSetupDialog Psdlg = New PagesetupDialog ();

IF (StoredPageSettings == Null) {storedpagesettings = new Pagesettings ();

Psdlg.pagesettings = storedpagesettings; psdlg.showdialog ();} catch (exception ex) {messagebox.show ("error -" ex. measureage);}

}

// 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); // assume the default printer

IF (storedpagesettings! = null) {pd.defaultpagesettings = storedpagesettings;

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);}}

///

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

///

/// 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.pageSetupButton = new System.Windows. Forms.Button (); this.text = "Print Example 4"; 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"; pageSetupButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; pageSetupButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; pageSetupButton.Size = New S YSTEM.DRAWING.SIZE (136, 40); PagesetupButton.Tabindex = 0; PagesetupButton.Location = New System.drawing.point (32, 160); PagesetupButton.Text = "THIS.CONTROLS.Add (this. PrintButton); this.controls.add (this.pagesetupbutton);} /// /// The primary 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;}}

}

Link to online sample documentation

Link to online example source code

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

New Post(0)