Open Source: Infinction of Print Page Settings, Printer Settings, Print Preview dialog box in .NET Environment (2)

xiaoxiao2021-03-06  66

Previously, in the .NET environment, the implementation of printing page settings, printer settings, and print preview dialogs, now I am now applying them.

We need to do something below:

1. Change the program output type written in the upper part to the class library and compile it into printerpagesetting.dll;

2, establish a C # or VB.NET project, and add a reference at the reference to the project, browse and select Printerpagesetting.dll in the .NET tab of the pop-up dialog.

3, add a TextBox text box control on the default window Form1, the default name TextBox1 (recommended naming and coding specification in the actual project, such as frmTestPrint, TXTPRINT)

4. Drag from the toolbox to the window Form1, create the MainMenu1 menu object, create the main menu Menufile

5. Click Menufile to establish a MenuFilePageSetup, MenuFilePrintpreView, MenuFilePrint menu at its input. I think everyone understands the meaning of these menus, which is the most basic of naming regulations.

Ready to do it, see how we use PrinterpageSetting to easily complete the print page settings, printer settings, and print preview dialogs.

Step 1: Stateing and instantiation in the window class and instantiateted, of course, instantiated objects can be placed in the constructor. C #: private goldprinter.printerpagesetting printerpagesetting = new goldprinter.printerpagesetting (); vb.net: private printerpagesetting as new goldprinter.printerpagesetting

Step: Write a specific process enables the printing of C #: private void PrintDocument_PrintPage_Handler (object o, System.Drawing.Printing.PrintPageEventArgs e) {System.Drawing.Graphics g = e.Graphics; if (Draw (g)) { E.hasmorepages = true; // To page print} else {e.hasmorepages = false; // Print end}}

VB.net: Private Sub printDocument_PrintPage (ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Dim g As System.Drawing.Graphics = e.Graphics If Me.Draw (g) Then e.HasMorePages = True ' To page printing else e.hasmorepages = false 'printing ends End if End Sub

Everyone can see how we make the program automatically paginate when printing, that is, set the HasMorePages property to be true. For clarity, I will independently use DRAW (). In the second step we implement the specific process of printing, in the PrinterpageSetting class, we don't know the specific implementation of printing, designed the PrintPage delegation, let the caller implement, then tell PrinterpageSetting which method implementation, that is The third step is. Step 3: Print Entrusted to double-click on the blank window and enter the relevant statement in the Form1_Load event. Of course, it can be placed in a constructor, which is convenient for describing. C #: private void Form1_Load (object sender, System.EventArgs e) {this.printerPageSetting.PrintPage = new GoldPrinter.PrintPageDelegate (PrintDocument_PrintPage_Handler);} if you do not know what is meant is the one, which one to use it: this.printerPageSetting .PrintPageValue = new GoldPrinter.PrintPageDelegate (PrintDocument_PrintPage_Handler); printing means is to tell printerPageSetting specific implementation process PrintDocument_PrintPage_Handler (second step) VB.net: Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mybasetting machine.printerpagesetting.printpageValue = new goldprinter.printpageDelegate (addressofprintDocument_printpage) End Sub

You can also try it, private withevents printdocument as system.drawing.printing.printdocument 'first step: in the declaration window-level variable me.printdocument = me.printerpagesetting.printDocument' Step 2: This will contact two quantities Plus PrintDocument_PrintPage (...) plus Handles PrintDocument.printpage

Step 4: Display Page Setup / Printer Settings / Print Preview dialog box is clicked on several menus, and the specific code is as follows:

C #: private void menufilepagesetup_click (object sender, system.eventargs e) {this.printerpagesetting.showpagesetupdialog (); // Display page settings dialog box}

Private void menufileprintpreview_click (ibject sender, system.eventargs e) {this.printerpagesetting.showprintpreviewDialog (); // Display print preview dialog box}

private void menuFilePrint_Click (object sender, System.EventArgs e) {this.printerPageSetting.ShowPrintSetupDialog (); // display the print preview dialog} VB.net: Private Sub menuFilePageSetup_Click (ByVal sender As System.Object, ByVal e As System.EventArgs Handles menufilepagesetup.click me.printerpagesetting.showpagesetupdialog () End Sub

Private Sub menuFilePrintPreview_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePrintPreview.Click Me.printerPageSetting.ShowPrintPreviewDialog () End Sub

Private sub menufileprint_click (byval e as system.Object, byval e as system.eventargs) Handles menufileprint.click me.printerpagesetting.showprintsetupDialog () end SUB

Haha, until now, we do the work that we do is basically completed, but don't forget to Draw (). C #: Private Bool Draw (System.drawing.Graphics G) {G.drawstring ("Hello World!", New Font ("Song", 15), Brushes.Black, New Pointf (0, 0)); Return False }

VB.NET: Private Function Draw (Byval g as system.drawing.graphics) as boolean g.drawstring ("Hello World!", New Font ("Song", 15), Brushes.Black, New Pointf (0, 0) RETURN FALSE END FUNCTION

Don't laugh at this draw () too simple! When you run, hey, the program does have the ability to complete the print page settings, printer settings, print preview dialog box, and set the horizontal longitudinal print, change the size of the paper size.

Expansion:

Of course, since printDocument_printpage_handler () is clear and paging from PrintDocument_PrintPage (), then I will not let everyone regret. I now use VB.Net and C # to implement an example that can be printed, and the above work is not performed, just change DRAW (). VB.NET: Private Function Draw (Byval g as system.drawing.graphics) AS Boolean Return DrawText (g, me.printerpagesetting.printDocument, me.textbox1.text) End Function

'This code was adapted from MSDN Private Function DrawText (ByVal g As System.Drawing.Graphics, ByVal pdoc As System.Drawing.Printing.PrintDocument, ByVal text As String) As Boolean g.DrawString ( "Hello World!", New Font ("Song", 15), Brushes.black, New Pointf (0, 0)) Return FalseStatic InTcurrentchar AS INT32 DIM FONT AS New Font ("Song", 10)

Dim intPrintAreaHeight, intPrintAreaWidth As Int32 With pdoc.DefaultPageSettings intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right End With

'Lateral printing, wide and high switching if pdoc.defaultpagesettings.landscape dam, intprintareaheight,

'Define Print Area Dim RectPrintingArea as New Rectanglef (Pdoc.defaultpagesettings.Margins.Left, PDoc.defaultpagesettings.Margins.top, IntprintAreawidth, IntprintareaHeight)

DIM FMT AS New StringFormat (StringFormatflags.LineLimit)

Dim intLinesFilled, intCharsFitted As Int32 g.MeasureString (Mid (text, intCurrentChar 1), font, _ New SizeF (intPrintAreaWidth, intPrintAreaHeight), fmt, _ intCharsFitted, intLinesFilled)

g.drawstring (MID (Text, Intcurrentchar 1), Font, _ Brushes.black, RectPrintingArea, FMT)

INTCURRENTCHAR = INTCHARSFITTED

IF INTCURRENTCHAR

'Two counts of value interchange private sub sw32, byref j AS int32) DIM TMP AS INT32 = i i = j j = Tmp End Sub VB.NET is implemented, then I will change an object-oriented Implement method. C #: Right click on the C # item to select Add / Add Class, named DrawText.cs, list all the contents: ///// Draw text in the drawing surface area ///

Public Class DrawText

{

Private graphics_graphics;

Private Rectanglef _Rectanglef;

Private string_text;

Private font _font;

Private brush _brush;

Private stringFormat_stringformat;

Private int _Startcha; private int _linefitted; #region field attribute public graphics graphics {get {return _graphics;} set {_graphics = value;}}

Public Rectanglef Rectanglef {Get {Return_Rectanglef;} set {_Rectanglef = Value;}}

Public string text {get {return_text;} set {_text = value;}}

Public font font {get {return _font;} set {if (value! = null) {_font = value;}}}

Public brush brush {get {return _brush;} set {ife (value! = null) {_brush = value;}}}

Public StringFormat StringFormat {Get {Return_StringFormat;} set {_stringFormat = value;}}

Public int startchar {get {return _startchar;} set {_startchar = value; if (_startchar <0) {_Startchar = 0;}}}

Public Int Charsfitted {get {return_charsfitted;}} public int Linesfilled} public {return _linesfilled;}} #ENDREGION

Public DrawText () {_text = ""; _font = new font ("Song", 10); _Rectanglef = New Rectanglef (0, 0, 0, _font.height); _brush = brushes.black;

_StartChar = 0; _LinesfiLled = 0; _Charsfitted = 0;

_StringFormat = new stringFormat (StringFormatflags.LineLimit);}

Public DrawText (String text): this () {_text = text;}

Public void draw () {ix (_graphics! = null) {int INTLINESFILED, INTCHARSFITTED

_graphics.MeasureString (_text.Substring (_startChar), _ font, new SizeF (_rectangleF.Width, _rectangleF.Height), _ stringFormat, out intCharsFitted, out intLinesFilled); _graphics.DrawString (_text.Substring (_startChar), _ font, _brush, _rectangleF _StringFormat;

THIS._LINESFILLED = INTLINESFILED; this._Charsfitted = INTCHARSFITTED;}}}

The original DRAW () is then replaced with the following statement:

private static int intCurrentCharIndex; private bool Draw (System.Drawing.Graphics g) {float width, height; width = this.printerPageSetting.PrintDocument.DefaultPageSettings.PaperSize.Width - this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Left - this. printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Right; height = this.printerPageSetting.PrintDocument.DefaultPageSettings.PaperSize.Height -this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Top - this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Bottom; // printing lateral, wide and high exchange if (this.printerPageSetting.PrintDocument.DefaultPageSettings.Landscape) {Swap (ref width, ref height);} RectangleF recPrintArea = new RectangleF (this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Left, this. Printerpagesetting.printDocument.defaultpagesettings.Margins.top, width, height);

DrawText drawText = new DrawText (this.TextBox1.Text); drawText.Graphics = g; drawText.RectangleF = recPrintArea; drawText.StartChar = intCurrentCharIndex; drawText.Draw ();

INTCURRENTCHARINDEX = DrawText.Charsfitted;

IF (intcurrentcharindex

// Two number of value interchange private void swap (ref float I, ref float j) {float tmp = i; i = j; j = tmp;}

In the implementation of VB.NET and C #, a value swap () function of two numbers of two numbers is written separately, and it is called when the page is printed.

It also needs to be added to the present article: Open Source: .NET Environment Configuration of Print Page Settings, Printer Settings, Print Preview dialog in the present article http://blog.9cbs.net/flygoldfish/archive/2004/08/17 77208.aspx in, I was slightly altered, the list is as follows: 1, will IPrinterPageSetting.cs PrintPage changed PrintPageValue, increase event PrintPageDelegate PrintPage; 2, will WebPrinterPageSetting.cs PrintPage changed PrintPageValue, increase public event GoldPrinter.PrintPageDelegate PrintPage ; 3, WinPrinterPageSetting.cs in PrintPage to PrintPageValue, increasing public event PrintPageDelegate PrintPage {add {_printDocument.PrintPage = new System.Drawing.Printing.PrintPageEventHandler (value); _printPageValue = value;} remove {_printDocument.PrintPage - = new System.Drawing.Printing.PrintPageEventHandler (value); _printPageValue = null;}} 4, PrinterPageSetting.cs in PrintPage to PrintPageValue, increasing public event PrintPageDelegate PrintPage {add {_printerPageSetting.PrintPage = new PrintPageDelegate (value);} remove { _printerpagesetting.printpage - = new printpagedelegate (value);}}

5, there will PrinterPageSetting.cs PrinterPageSetting argument constructor public PrinterPageSetting (PrintDocument printDocument) of _printerPageSetting.PrintDocument = printDocument; plus a determination, i.e., if (printDocument = null!) {_PrinterPageSetting.PrintDocument = printDocument;} Thus, the system A default PrintDocument object will be provided.

Summarize the main steps with a page preview to complete this article!

For discussion and source download and complete applications about print printers, see http://community.9cbs.net/expert/topic/3278/3278050.xml?temp =.5012934

Source program download: http://www.softreg.com.cn/shareware.asp? Id = 17260 http://www.alinksoft.com

Disclaimer: This article is copyright to Zhou Fang Yong. If you need to reprint, please keep the full content and source. Flygoldfish@sina.com

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

New Post(0)