In the NET environment, implementation of printing page settings, printer settings, and print preview dialog box (BLOG article f

xiaoxiao2021-03-06  71

Go to the VS2003 IDE, click New Projects from the File menu, the project type is C #, select the class library in the template, the name is estimated and called Goldprinter, is determined. In the open development environment, delete the default generated class1.cs in the Solution Explorer.

Now, we have to consider it. For printing, it may be under Windows, or it may be under Web. If the two methods are united, let the caller don't have to worry about it. Therefore, we first design an interface to provide a method of sharing. When the project is right-click, add a new class, name: iprinterpagesetting, because it is an interface, change the Class to Interface, and remove the constructor. It should be similar to the following: use system; namespace goldprinter {///

/// iprinterpagesetting summary description. /// public class iprinterpagesetting {}}

At least three declarations, showpagesetupdialog (), showPrintSetupDialog (), showPrintSetupDialog (), showPrintPreviewDialog (), That is to open the print page settings, printer settings, and print preview dialogs. VS2003 provides us with a PrintDocument, named space is system.drawing.printing. PrintDocument is a class provided for printing. It is an overview that defines a reusable object that sends the output to the printer.

PrintDocument encapsulates a lot of complex details, it has a vital event called PrintPage, and the profiling of PrintPage is: when you need to be played for the current page. Therefore, we will pass the printed event, hand over the object of the printing specific output to PrintPage, other we don't have to worry.

So how do we achieve the specific output process? Because we are encapsulation, we can't know how end users do it. At this time, delegate has played a big role in us. Everyone knows that when a function is implemented, some input data is often needed, but the function itself does not know which specific value of this data, only knows the data type of this value, and introduces the parameters, the caller call The specific value of the corresponding type is passed when the correspondence data. Everyone should realize my intention, similar to the function, we provide a method of related printers for printing, but the specific implementation is only by the caller, the difference is the function passed the parameters, and the delegate we have to do Method. At this time, as long as we establish a corresponding relationship for both, this is a commission. We need to define a delegate, the parameter type is consistent with the PrintPage, ie (Object Obj, System.drawing.printing.PrintPageEventArgs EV).

It can be seen that we need to do something below: 1. Add a reference at the reference to the project, select System.Drawing.dll in the .NET tab of the pop-up dialog, because the namespace of PrintDocument is System.drawing. Printing, implementation in system.drawing.dll; 2. Increase the declaration of the PrintDocument attribute in the iPrinterpageSetting interface; :

Using system.drawing; using system.drawing.printing;

Delegate definition namespace GoldPrinter {///

/// PrintDocument.PrintPage the /// public delegate void PrintPageDelegate (Object obj, System.Drawing.Printing.PrintPageEventArgs ev);

///

/// iprInterpagesetting interface, display the print paper settings, printer settings, and print preview dialogs. /// The program provides: Zhou Fang Yong; Email: flygoldfish@sina.com. /// public interface iprinterpagesetting {/// /// Get or sets print document /// system.drawing.printing.printDocument printocument {get; set;}

///

/// Associated a method, the purpose is to make the specific print by instantiator /// / printerpagesetting1.printpage = new printPageDelegate (this.printpageeventhandler); /// PrintPageDelegate PRINTPAGE {Get; set;}

///

/// Displays the page settings dialog and return PagesettingS /// /// system.drawing.printing.pagesettings showpagesetupdialog ();

///

/// Displays the printer settings dialog box and returns printersettings /// /// system.drawing.printing.printersettings showprintSetupDialog ();

///

/// Display print preview dialog /// void showprintpreviewDialog ();

} // end interface} // end namespace

Below, we implement the programming method under Windows and the framework under Web programming. Since it is programming under Windows, we may use the dialog as an error prompt, so we will join System.Windows.Forms.dll reference. Build a class file, name is WinPrinterpagesetting. After entering the file, add: iprinterpagesetting after class declaration: iPrinterpageSetting, implement interface functionality. The list of procedures is as follows, there are a lot of comments in the program, and there is no more. Using system.drawing; using system.drawing.printing; using system.windows.form;

Namespace Goldprinter {///

/// WinForm prints Under print paper settings, printer settings, print preview dialog box. /// public class winprinterpagesetting: iprinterpagesetting {// PRINTPAGE delegate declares as a member variable private printPageDelegate _printpage; // Print Document Private PrintDocument_printDocument;

#Region constructor PUBLIC Winprinterpagesetting (): this (null) {}

///

/// Use PrintDocument to initialize a new instance of the class, automatically create a printDocument instance /// /// Public WinPrinterPageSetting (PrintDocument PrintDocument) {if (PrintDocument! = NULL) {_PrintDocument = printDocument;} else {_printDocument = new printDocument ();}} #ENDREGION

#Region iPrInterpagesetting member

///

/// Get or sets print document /// public printdocument printdocument {get {return this._printdocument;} set {this._printDocument = value;}}

///

// / must instantiate such an attribute after calling the print / preview, enabling it to associate a method, the purpose is to make the specific print by instantiator ///, such as Printerpagesetting1. PrintPage = new PrintPageDelegate (this.PrintPageEventHandler); /// public PrintPageDelegate PrintPage {set {// initial delegate variable, associated methods _printPage = value; if (_printDocument == null) {throw new Exception ( "association The print document cannot be empty! ");} _Printdocument.printpage = new system.drawing.printing.printpageEventhandler (this._printpage);} get {return_printpage;}} /// /// display page settings Dialog and return PageSettings /// Public Pagesettings ShowpageSetupDialog () {Return ShowpageSetupDialog (this._printDocument);}

///

/// printer settings dialog box and return PrinterSettings /// /// public PrinterSettings ShowPrintSetupDialog () {return ShowPrintSetupDialog (this._printDocument);}

///

/// Display print preview dialog /// public void showprintpreviewDialog () {showprintpreviewdialog (this._printdocument);}

#ndregion

// ************** Note: The following dialog box can use the *************** #Region page settings dialog protected Virtual Pagesetting SHOWPAGESETUPDIALOG (PrintDocument PrintDocument) ///

/// Page Settings dialog, you can use /// /// /// < Returns> /// /// Author: Zhou Die Yong /// Modified Date: 2004-08-07 /// protected virtual pagesettings showpagesetupdialog (PrintDocument PrintDocument) {// Notice Returns Value Pagesettings Pagesettings Ps = New Pagesettings ();

IF (PrintDocument == NULL) {throw new exception ("associated print document can not be empty!");} Try {// Statement and instantiate PagesetupDialog PagesetupDialog PSDLG = New PageSetupDialog (); // Related documentation and document page Default Set psdlg.document = printDocument; psdlg.pagesettings = printocument.defaultpagesetting;

// Display dialog DialogResult result = psdlg.showdialog (); if (result == DialogResult.ok) {ps = psdlg.pagesettings; printdocument.defaultpagesettings = psdlg.pagesettings;}

} Catch (system.drawing.printing.invalidprinterexception e) {MessageBox.show ("No printer installed, please enter the system control network version to add printers!", "Print", MessageBoxButtons.ok, MessageBoxicon.Warning);} catch (Exception EX) {MessageBox.show (ex.Message, Print ", MessageBoxButtons.ok, MessageBoxicon.warning;

Return PS;} #ENDREGION

#Region Print Settings Dialog Protected Virtual PrinterSettings ShowPrintSetUpDialog (PrintDocument PrintDocument) ///

/// Print Settings dialog, you can use /// /// < / param> /// /// /// Author: Zhou Dieong /// Modified Date: 2004-08-07 /// Protected Virtual PrinterSettings ShowPrintSetupDialog (PrintDocument PrintDocument) {// Declaration of returned printersettings printersettings ps = new printerSettings (); if (PrintDocument == NULL) {throw new exception ("associated print document can not be empty!");}

Try {// Stated and instantiate PrintDialog PrintDialog PDLG = New PrintDialog (); // You can select page pdlg.allowsomepages = true;

/ / Specify the print document PDLG.Document = printDocument;

// Display dialog DialogResult result = pdlg.showdialog (); if (result == DialogResult.ok) {// Save print settings PS = pdlg.printersettings; // Print printDocument.print ();}} catch (system. Drawing.printing.invalidprinterexception E) {MessageBox.show ("No printer installed, please enter the system control panel to add printers!", "Print", MessageBoxButtons.ok, MessageBoxicon.warning);} catch (exception ex) {MessageBox. Show (ex.Message, "Print", MessageBoxButtons.ok, MessageBoxicon.warning;

Return PS;} #ENDREGION

#Region Print Preview dialog PROTECTED VIRTUAL VOID SHOWPRINTPREVIEWDIALOG (PrintDocument PrintDocument) ///

/// Print preview dialog, you can use /// //// < / param> /// /// /// Author: Zhou Square /// Modified Date: 2004-08-07 /// protected virtual void showprintpreviewDialog (PrintDocument PrintDocument) {if (PrintDocument == NULL) {throw new exception ("associated print document can not be empty!");

Try {// Stated and instantiate PrintPreviewDialog PrintPreviewDialog PPDLG = New PrintPreviewDialog (); // Specify print document PPDLG.Document = printDocument;

// Display dialog DialogResult result = ppdlg.showdialog (); if (result == DialogResult.ok) {// ...}

} Catch (system.drawing.printing.invalidprinterexception e) {MessageBox.show ("No printer installed, please enter the system control network version to add printers!", "Print", MessageBoxButtons.ok, MessageBoxicon.Warning);} catch (Exception EX) {MessageBox.show (ex.Message, "Print", MessageBoxButtons.ok, MessageBoxicon.warning;}} #ENDREGION

} // End class} // end namespace

Everyone can see #Region specification, it is used with #ndregion, you can fold the middle text, it is very convenient, click when you need to view the front expansion symbol. The last three functions are more important ways while providing their overloaded functions. ShowpageSetupDialog () Returns the current settings for users to confirm with ShowPrintSetupDialog (). Built again, the name is WebPrinterpagesetting. After entering the file, add: iprinterpagesetting after class declaration: iPrinterpageSetting, implement interface functionality. The list of procedures is as follows:

Using system;

Namespace Goldprinter {///

/// WebForm print settings, printer settings, print preview dialog box. (*** No specific implementation ***) /// public class webprinterpagesetting: iprinterpagesetting {public webprinterpagesetting () {// // Todo: Add constructor logic here //}

#Region iPrInterpagesetting member

public System.Drawing.Printing.PrintDocument PrintDocument {get {// TODO: Add WebPrinterPageSetting.PrintDocument getter achieved return null;} set {// TODO: Add WebPrinterPageSetting.PrintDocument setter implemented}}

Public PrintPageDelegate PrintPage {Get {// Todo: Add WebPrinterpagesetting.printpage GetTer Implement Return Null;} Set {// Todo: Add WebPrinterpagesetting.PrintPage Setter Implementation}}

Public system.drawing.printing.pagesettings showpagesetupdialog () {// Todo: Add webprinterpagesetting.showpagesetupdialog implementation Return NULL;}

Public system.drawing.printing.printersettings showPrintSetupDialog () {// Todo: Add webprinterpagesetting.showprintSetupDialog implementation Return null;}

Public void showprintpreviewDialog () {// Todo: Add webprinterpagesetting.showprintpreviewDialog implementation}

#ndregion

} // End Class

} // end namespace

I have made this step, everyone may say that it is already, it is true. Because WinprinterPagesettingTing / WebPrinterpageSetting and the method or attribute you call it when calling. Everyone thinks about why I want to provide iprinterpagesetting? If I call the print dialog now, I don't care if it is Windows or on the web. At this time, iPrinterpageSetting has played a role.

For example: iprinterpagesetting _printerpagesetting; there is a printed interface object, you can call it directly, but it is Windows mode or web mode, we only need to let the program goelf to go according to the system as the real WinPrinterpagesetting or WebPrinterpagesetting. . Such as: // Create a new instance of the interface object, make it an object IF (Windows mode) {_printerpagesetting = new WinPrinterpagesetting () {_printerpagesetting = new webprinterpagesetting = new webprinterpagesetting = new webprinterpagesetting = new webprinterpagesetting } We can see this effect in the next program list. Let's build a PrinterpageSetting class and combine WinPrinterpagesetting with WebPrinterpageSetting. Increasing automatically determining Windows mode or web mode in the constructor of the class, but also provides the PrintMode property to make the user modify. The list of procedures is as follows:

Using system.drawing; using system.drawing.printing; using system.windows.form;

Namespace goldprinter {

///

/// printing mode /// public enum printmodeflag {/// /// windows window printing mode /// Win, /// /// Web window printing mode /// web}

///

/// Package page settings, printer settings, print preview, suitable for Window and ASP.NET. /// The program provides: Zhou Fang Yong; Email: flygoldfish@sina.com. /// public class printerpagesetting {// Stateing a package page setting, printer setting, print preview interface private iPrInterpagesetting_printerpagesetting;

// Printing Private Private PrinTModeFlag_printmodeflag;

Public PrintModeflag PRINTMODE {get {return;} set {this._printmodeflag = value; // Using abstract mode, create a new instance of interface objects, make it an object IF that realizes its interface (THIS._PRINTMODEFLAG == PrintModeFlag.Win) {_printerPageSetting = new WinPrinterPageSetting ();} else if (this._printModeFlag == PrintModeFlag.Web) {_printerPageSetting = new WebPrinterPageSetting ();}}}

///

/// Gets or sets the print document /// public PrintDocument PrintDocument {get {return _printerPageSetting.PrintDocument;} set {_printerPageSetting.PrintDocument = value;}} /// / / / Be sure to instantiate such a method in calling the print / preview initial variable to associate a method, the purpose is to operate the specific print by instantiator ///, such as printerpagesetting1.printpage = new PrintPageDelegate (this. PRINTPAGEEVENTHANDLER); /// public printPageDelegate PrintPage {set {_printerpagesetting.printpage = value;} get {return_printerpagesetting.printpage;}}

#REGION Construction Function ///

/// Package Settings, Printer Settings, Print Preview, Suitable for Window and ASP.NET, Web Method, please add key printmode and value web in the configuration file, or instantiate The printMode property is set to PrintModeflag.Web. /// public printerpagesetting (): this (null) {}

///

/// package page settings, printer settings, print previews, suitable for Window and ASP.NET, web mode, please add key printMode and value web in the configuration file, or instantiate the printMode property For PrintModeflag.Web. /// /// public printerpagesetting (printdocumentprintdocument) {string strprintmode = "

// Profile mid button printmode strprintmode = system.configuration.configurationSettings.appsettings ["printmode"];

IF (strprintmode == null) {// defaults to WIN mode strprintmode = "win";

// Configure the value of the key printMode WINTMODE WINTMODE WIN / Web if (StrprintMode.toupper () == "WIN") {this.printMode = PrintModeflag.win;} else {this.printMode = PrintModeflag.Web;}

/// // The write method of adding a key value in the configuration file < / appSettings> * / _printerpagesetting.printdocument = printdocument

} #Endregion

///

/// Displays the page settings dialog and return PagesettingS /// public PageSettings showpagesetupdialog () {return_printerpagesetting.showpagesetupdialog ()

///

/// printer settings dialog box and return PrinterSettings /// /// public PrinterSettings ShowPrintSetupDialog () {return _printerPageSetting.ShowPrintSetupDialog ();}

///

/// Display print preview dialog /// public void showprintpreviewDialog () {_printerpagesetting.showprintpreviewDialog ();}

} // End class} // end namespace

In this class constructor, the configuration file is mentioned. When establishing an ASP.NET application, you will generate a file called WebMis.config. There are some settings for the environment and other information in the file, which can be easily understood to provide an external setting interface to improve applications. flexibility. When establishing a Windows application, there is no such file by default, but you can use the new item on the project, select the application configuration, the default configuration file name is app.config. In the constructor, we default to the print under WIN. If it is a web mode, you can add key printMode and value web in the configuration file, or you can instantiate the printMode property is PrintModeFlag.Web. Here is the write method of adding a key value in the configuration file

At this point, there are all the implementations of printing page settings, printer settings, and print preview dialogs in the NET environment. We can compile the program into a dynamic library. In the next article, let's take a look at how to apply this dynamic library, and take a complete document, the report print program is further explained.

For discussion and source download and complete applications about print printers, see http://community.9cbs.net/expert/topic/3278/3278050.xml?temp =.5012934 Source: http://blog.9cbs.net/ Flygoldfish / Archive / 2004/08/17 / 77208.ASPX

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

New Post(0)