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 {///
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 {///
///
///
///
///
} // 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 {///
#Region constructor PUBLIC Winprinterpagesetting (): this (null) {}
///
#Region iPrInterpagesetting member
///
///
///
///
#ndregion
// ************** Note: The following dialog box can use the *************** #Region page settings dialog protected Virtual Pagesetting SHOWPAGESETUPDIALOG (PrintDocument PrintDocument) ///
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) ///
/ / 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) ///
// 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 {///
#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 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 ();}}}
///
///
#REGION Construction Function ///
// 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 XML Version = "1.0" encoding = "utf-8"?>
_printerpagesetting.printdocument = printocument;
} #Endregion
///
///
} // 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 XML Version = "1.0" encoding = "UTF-8"?>
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
Disclaimer: This article is copyright to Zhou Fang Yong. If you need to reprint, please keep the full content and source.