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

xiaoxiao2021-03-06  82

In the .NET environment, implementation of printing page settings, printer settings, print preview dialog box

Open Source: .Net environment related to print page settings, printer settings, print preview dialog implementation

I personally think that developing MIS, first to solve the grid problem, and development tools provide us with controls such as DataGrid, MshflexGrid. Second, it is printing problem and prints the business documents with the data report. It is understood that even if a business function is very perfect management software, there is no support for printing, and it must not be a complete software.

For printing, we can always look at the effect of printing before the user prints. At this time, the general software provides print preview; if you need to adjust the effect of printing, the size of the paper, the margin setting, but also provides page settings The dialog allows the user to select and adjust; if the system has multiple printers, you can also select the output object when the user clicks the print button.

In short, for printing, we must at least provide print page settings, printer settings, print preview dialogs, so that users have more opportunities to participate in interaction. This is a printed module more important and more basic, let's take them together.

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 property in the iPrinterpageSetting interface;

The following is the contents of the iPrinterpageSetting interface file:

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 box and return PagesettingS /// /// system.drawing.printing.pagesettings showpagesetupdialog (); /// < Summary> /// Displays the printer settings dialog box and return 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 the print document /// public printdocument printdocument {get {return this._printdocument;} set {this._printdocument = value;}} /// / / / Be sure to instantiate such an attribute before calling the print / preview, make it associated with a method, the purpose is to make the specific print by instantiator /// / printerpagesetting1.printpage = new PrintPageDelegate (this .Printpageeventhandler); /// public printpageDelegate printpage {set {// initial entrustment variable, association method _printpage = value; if (_printdocument == null) {throw new exception ("associated print document can not be empty! ");} _Printdocument.printpage = new system.drawing.printing.printpageeeventhandler (this._printpage);} get {return_printpage;}}

///

/// Displays the 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 Pagesettings PageSettings Ps = New Pagesettings (); if (PrintDocument == Null) {throw new exception ("associated print document can not be empty!");}

try {// declare and instantiate PageSetupDialog PageSetupDialog psDlg = new PageSetupDialog (); // page document and the related default settings psDlg.Document = printDocument; psDlg.PageSettings = printDocument.DefaultPageSettings;

// 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) {// Statement Return value PrinterSettings PrinterSettings PS = New PrinterSettings (); if (PrintDocument == NULL) {throw new exception ("associated print document can not be empty!");} Try {// Statement and instance 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 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 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 with printed document can not be empty!");} try {// declare and instantiate PrintPreviewDialog PrintPreviewDialog ppDlg = new PrintPreviewDialog (); // specified to print a document PPDLG.Document = printocument;

// 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 members of the public System.Drawing.Printing.PrintDocument PrintDocument {get {// TODO: Add WebPrinterPageSetting.PrintDocument getter achieve return null;} set {// TODO: Add WebPrinterPageSetting.PrintDocument setter achieve}}

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 {_printerpagesetting = 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.Forms; 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 ();}}}

///

/// Get or sets print document /// public printdocument printDocument {get {return_printerpagesetting.printdocument;} set {_printerpagesetting.printDocument = value;}}

///

// / must instantiate such a class after calling the print / preview initial variable, which is associated with a method, the purpose is to make the specific print by instantiator /// / 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 preview, suitable for Window and ASP.NET, web mode please Add key printMode and value web in the configuration file, and then set the printMode property to PrintModeflag.Web after instantiation. /// /// 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 = printocument;

} #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

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

New Post(0)