NET environment for printing page settings, printer settings, Print Preview dialog box Implementation (ii)

Source: Internet
Author: User
Tags bool constructor drawtext
Print | dialog | The front of the page says. NET environment for printing page settings, printer settings, Print Preview dialog box implementation, now it's time for us to apply them.

We need to do some of the following things:
1. The program output type written in the previous article is changed to class library and compiled into PrinterPageSetting.dll;
2, establish C # or vb.net project, and add references to the project, browse and select PrinterPageSetting.dll in the. NET tab of the pop-up dialog box.
3, in the default window Form1 add a TextBox text box control, the default name TextBox1 (in the actual project recommended by the naming and coding specifications, such as Frmtestprint,txtprint)
4, drag a MainMenu from the Toolbox to the window Form1, create mainMenu1 menu objects, build the main menu Menufile
5, click Menufile, in its lower input place respectively to establish Menufilepagesetup, Menufileprintpreview, menufileprint menu. I think we understand the meaning of these menus, this is the most basic naming norms.

When the preparations are done, see how we can use Printerpagesetting to easily complete the print page setup, printer setup, Print Preview dialog box.

Step one: Declare and instantiate printerpagesetting in the window class, and of course instantiate objects can be placed in constructors.
C #:
Private goldprinter.printerpagesetting printerpagesetting = new goldprinter.printerpagesetting ();
VB.net:
Private printerpagesetting as New goldprinter.printerpagesetting

Step two: Write a specific process to achieve printing
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 print page pagination
}
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 print paging
Else
E.hasmorepages = False ' Print end
End If
End Sub

You can see how we make the program automatically pagination when printing, that is, set the HasMorePages property is true. To make it clear, I will actually print the actual process independent of the draw () implementation.
In the second step we realize the printing of the specific process, in the Printerpagesetting class, we do not know the specific implementation of printing, the design of the PrintPage delegate, let the caller to achieve their own, and then tell Printerpagesetting is to use which method to achieve, That's the third step.

Step three: Print the delegate
Double-click in the window blank to enter the relevant statement in the Form1_Load event. Of course, it can be placed in the constructor, here for the convenience of description.
C #:
private void Form1_Load (object sender, System.EventArgs e)
{
This.printerPageSetting.PrintPage + = new Goldprinter.printpagedelegate (Printdocument_printpage_handler);
}

If you don't know what the last sentence means, use this sentence:
This.printerPageSetting.PrintPageValue = new Goldprinter.printpagedelegate (Printdocument_printpage_handler);
It means to tell printerpagesetting that the specific implementation of the printing process is Printdocument_printpage_handler (step two)

VB.net:
Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Me.printerPageSetting.PrintPageValue = New goldprinter.printpagedelegate (AddressOf printdocument_printpage)
End Sub

We can still try.
Private WithEvents PrintDocument as System.Drawing.Printing.PrintDocument ' first step: Declaring window-level variables
Me.printdocument = Me.printerPageSetting.PrintDocument ' second step: Link two quantities here
Will Printdocument_printpage (...) Plus Handles printdocument.printpage.


Fourth step: Show Page Setup \ printer settings \ Print Preview dialog box
Click on a few menus, respectively, with the following code:

C #:
private void Menufilepagesetup_click (object sender, System.EventArgs e)
{
This.printerPageSetting.ShowPageSetupDialog (); Show Page Setup dialog box
}

private void Menufileprintpreview_click (object sender, System.EventArgs e)
{
This.printerPageSetting.ShowPrintPreviewDialog (); Show Print Preview dialog box
}

private void Menufileprint_click (object sender, System.EventArgs e)
{
This.printerPageSetting.ShowPrintSetupDialog (); Show Print Preview dialog box
}

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 MENUFILEPRINTP Review. Click
Me.printerPageSetting.ShowPrintPreviewDialog ()
End Sub

Private Sub Menufileprint_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Menufileprint.click
Me.printerPageSetting.ShowPrintSetupDialog ()
End Sub

Haha, to this end, we have done the basic work, but do not forget draw () Oh.
C #:
private bool Draw (System.Drawing.Graphics g)
{
g.DrawString ("Hello world!", New Font ("XXFarEastFont-Arial"), 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 ("XXFarEastFont-Arial,"), Brushes.black, new PointF (0, 0))
Return False
End Function

Please don't laugh at this draw () too simple yo! Run a look, hey, the program is able to complete the print page settings, printer settings, Print Preview dialog box functions, but also to set horizontal portrait printing, change the size of the paper settings.



Extended:

Of course, since Printdocument_printpage_handler () and printdocument_printpage () are clearly organized and able to page, I won't let anyone regret it.
I now use vb.net and C # to achieve an example of paging printing, and the above work is not white, just change the draw () on it.

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 is 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 ("XXFarEastFont-Arial,"), Brushes.black, new PointF (0, 0))
Return False

Static Intcurrentchar as Int32
Dim font as New font ("Song Body", 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

' Transverse print, wide and high exchange
If Pdoc. Defaultpagesettings.landscape Then
Me.swap (Intprintareawidth, Intprintareaheight)
End If

' 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 < text. Length Then
Return True
Else
Intcurrentchar = 0
Return False
End If
End Function

' Two-digit value interchange
Private Sub Swap (ByRef i as Int32, ByRef J as Int32)
Dim tmp as Int32 = i
i = j
j = tmp
End Sub

VB.net realized, then I will change an object-oriented implementation method.

C#:
Right-click on the C # project to select Add \ Add Class, named DrawText.cs, which lists all of the contents:
///
Draw text within 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 _startchar;
private int _linesfilled;
private int _charsfitted;

#region Field Properties
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
{
if (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
{
Get
{
return _linesfilled;
}
}
#endregion


Public DrawText ()
{
_text = "";
_font = new Font ("Song Body", 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 ()
{
if (_graphics!= null)
{
int intlinesfilled, 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 = intlinesfilled;
this._charsfitted = intcharsfitted;
}
}


Then replace the original draw () 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;
Horizontal printing, 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 < this. TextBox1.Text.Length)
{
return true;
}
Else
{
Intcurrentcharindex = 0;
return false;
}
}

Two-digit value interchange
private void Swap (ref float i,ref float j)
{
float tmp = i;
i = j;
j = tmp;
}

In vb.net and C # implementations, an interchange of two-digit value swap () functions is written, which is called when the page is printed horizontally, and is no longer Aushu.

It is also necessary to add that in the previous article:
Open source:. NET environment for printing page settings, printer settings, Print Preview dialog box implementation
Http://blog.csdn.net/flygoldfish/archive/2004/08/17/77208.aspx
, I have a slight change in the list as follows:
1, IPrinterPageSetting.cs PrintPage changed to Printpagevalue, add event printpagedelegate PrintPage;
2, WebPrinterPageSetting.cs PrintPage changed to Printpagevalue, add public event goldprinter.printpagedelegate PrintPage;
3, WinPrinterPageSetting.cs PrintPage changed to Printpagevalue, increase

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 PrintPage changed to Printpagevalue, increase
Public event Printpagedelegate PrintPage
{
Add
{
_printerpagesetting.printpage + = new Printpagedelegate (value);
}
Remove
{
_printerpagesetting.printpage-= new Printpagedelegate (value);
}
}

5. PrinterPageSetting.cs Printerpagesetting's parametric constructor public printerpagesetting (PrintDocument PrintDocument)
_printerpagesetting.printdocument = PrintDocument; add a judgment that
if (PrintDocument!= null)
{
_printerpagesetting.printdocument = PrintDocument;
}
In this way, the system provides a default PrintDocument object.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.