中国最大的web开发资源网站及技术社区,
前篇说了.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环境下有关打印页面设置、打印机设置、打印预览对话框的实现
http://blog.csdn.net/flygoldfish/archive/2004/08/17/77208.aspx
中,我又稍有改动,列表如下:
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对象。