.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现(二)
前篇说了.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现,现在到了我们应用它们的时候了。
我们需要做下面的一些事情:
1、将上篇中写的程序输出类型改为类库并编译成PrinterPageSetting.dll;
2、建立C#或VB.net项目,并在项目的引用处添加引用,在弹出的对话框的.NET标签中浏览并选择PrinterPageSetting.dll;
3、在默认窗口Form1上增加一个TextBox文本框控件,默认名TextBox1(实际项目中建议采用命名及编码规范,如frmTestPrint,txtPrint)
4、从工具箱中拖一个MainMenu到窗口Form1,创建mainMenu1菜单对象,建立主菜单menuFile
5、单击menuFile,在其下方输入处分别建立menuFilePageSetup、menuFilePrintPreview、menuFilePrint菜单。我想大家明白这几个菜单的意义了,这是命名规范最基本的。
准备工作做完了,看看我们怎么使用PrinterPageSetting轻松完成打印页面设置、打印机设置、打印预览对话框。
第一步:在窗口类中申明并实例化PrinterPageSetting,当然实例化对象可以放在构造函数中。
C#:
private GoldPrinter.PrinterPageSetting
printerPageSetting = new GoldPrinter.PrinterPageSetting();
VB.net:
Private printerPageSetting As New
GoldPrinter.PrinterPageSetting
第二步:写一个实现打印的具体过程
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;
//要分页打印
}
else
{
e.HasMorePages = false; //打印结束
}
}
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
‘要分页打印
Else
e.HasMorePages = False ‘打印结束
End If
End Sub
大家可以看到我们是怎么使程序在打印时自动分页的,就是设置HasMorePages属性为真就可以了。为了清晰可见,我将真正打印的具体过程独立出来用Draw()实现。
在第二步中我们实现打印的具体过程,在PrinterPageSetting类中,我们并不知道打印的具体实现,就设计了PrintPage委托,让调用者自己实现,然后告诉PrinterPageSetting是用哪一个方法实现,也就是第三步了。
第三步:打印委托
在窗口空白处双击,在Form1_Load事件中输入相关语句。当然也可以放在构造函数中,这里为了描述的方便。
C#:
private void Form1_Load(object sender,
System.EventArgs e)
{
this.printerPageSetting.PrintPage += new
GoldPrinter.PrintPageDelegate(PrintDocument_PrintPage_Handler);
}
如果你不知上一句是什么意思,就用这一句吧:
this.printerPageSetting.PrintPageValue = new
GoldPrinter.PrintPageDelegate(PrintDocument_PrintPage_Handler);
意思就是告诉printerPageSetting打印的具体实现过程是PrintDocument_PrintPage_Handler(第二步)
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
大家还可以一试啊,
Private WithEvents printDocument As
System.Drawing.Printing.PrintDocument ‘第一步:在申明窗口级变量
Me.printDocument =
Me.printerPageSetting.PrintDocument
‘第二步:在此将两个量联系起来
将printDocument_PrintPage(…)加上
Handles printDocument.PrintPage
第四步:显示页面设置\打印机设置\打印预览对话框
分别在几个菜单上单击,具体代码如下:
C#:
private void
menuFilePageSetup_Click(object sender, System.EventArgs e)
{
this.printerPageSetting.ShowPageSetupDialog();
//显示页面设置对话框
}
private void menuFilePrintPreview_Click(object sender,
System.EventArgs e)
{
this.printerPageSetting.ShowPrintPreviewDialog();
//显示打印预览对话框
}
private void menuFilePrint_Click(object sender,
System.EventArgs e)
{
this.printerPageSetting.ShowPrintSetupDialog();
//显示打印预览对话框
}
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 sender As
System.Object, ByVal e As System.EventArgs) Handles
menuFilePrint.Click
Me.printerPageSetting.ShowPrintSetupDialog()
End Sub
哈哈,到现在为此,我们做的工作基本完成了,不过不要忘了Draw()噢。
C#:
private bool
Draw(System.Drawing.Graphics g)
{
g.DrawString(“Hello World!”,new
Font(“宋体”,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(“宋体”, 15), Brushes.Black, New PointF(0,
0))
Return
False
End Function
大家可不要笑这个Draw()太简单哟!运行看看,嘿,程序确实能够完成打印页面设置、打印机设置、打印预览对话框的功能,还能设置横向纵向打印,改变纸张大小等设置呢。
扩展:
当然,既然PrintDocument_PrintPage_Handler()与printDocument_PrintPage()组织的清晰且能够分页,那我就不让大家遗憾了。
我现在分别用VB.net和C#实现一个能够分页打印的例子,而且上面的工作不白做,仅仅改一下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
‘这段代码改编自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(“宋体”, 15), Brushes.Black, New PointF(0,
0))
Return False
Static intCurrentChar As
Int32
Dim font As New Font(“宋体”,
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
‘
横向打印,宽与高交换
If
pdoc.DefaultPageSettings.Landscape
Then
Me.Swap(intPrintAreaWidth,
intPrintAreaHeight)
End If
‘定义打印区域
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
‘两个数的值互换
Private Sub Swap(ByRef i As
Int32, ByRef j As Int32)
Dim tmp
As Int32 = i
i =
j
j = tmp
End Sub
VB.net实现了,那我就换一个面向对象的实现方法吧。
C#:
在C#项目上右键选择添加\添加类,命名为DrawText.cs,这里列出全部内容:
///
///
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
字段属性
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(“宋体”,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;
}
}
然后将原来的Draw()用下面的语句替换掉:
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;
//横向打印,宽与高交换
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;
}
}
//两个数的值互换
private void Swap(ref
float i,ref float j)
{
float
tmp = i;
i = j;
j =
tmp;
}
在VB.net和C#的实现中,都分别写了一个互换两个数的值Swap()函数,供页面横向打印时调用,这里不再敖述。
还需要补充说明的是在上篇:
开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现
中,我又稍有改动,列表如下:
1、IPrinterPageSetting.cs中将PrintPage改为PrintPageValue,增加event PrintPageDelegate
PrintPage;
2、WebPrinterPageSetting.cs中将PrintPage改为PrintPageValue,增加public event
GoldPrinter.PrintPageDelegate PrintPage;
3、WinPrinterPageSetting.cs中PrintPage改为PrintPageValue,增加
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改为PrintPageValue,增加
public event
PrintPageDelegate
PrintPage
{
add
{
_printerPageSetting.PrintPage
+= new
PrintPageDelegate(value);
}
remove
{
_printerPageSetting.PrintPage
-= new PrintPageDelegate(value);
}
}
5、PrinterPageSetting.cs中将PrinterPageSetting的有参构造函数public
PrinterPageSetting(PrintDocument
printDocument)
的_printerPageSetting.PrintDocument = printDocument;加一个判断,即
if (printDocument != null)
{
_printerPageSetting.PrintDocument
= printDocument;
}
这样,系统会提供一个默认的PrintDocument对象。