Windows 打印控件

时间:2023-03-09 17:57:57
Windows 打印控件

Windows窗体的PrintDocument组件用于设置一些属性,这些属性说明,在基于Windows的应用程序中要打印说明内容以及打印文档的能力,可将它与PrintDialog组件一起使用来控制文档打印的各个方面。

要想建立输出到打印机的输出程序,就要首先创建PrintDocument对象。

PrintDocument pd = new PrintDocument();

PrinterSettings属性:

用于获取或设置一个值,指示对文档进行打印的打印机。

DefaultPageSettings属性:

获取或设置打印纸信息,这些页设置用作要打印的所有页的默认设置

DocumentName属性:

获取或设置打印文档时要显示的文档名。

PrintController属性:

获取或设置指导打印进程的打印控制器

Container属性:

获取Icontainer接口,它包含Component类

Print方法

是最重要也是最常用的方法,它将开始打印进程,并触发PrintPage事件。在PrintPage事件中绘制将要打印的内容。

public DialogResult ShowDialog()

public void  Print()

指定打印输出的方法是处理PrintPage事件并使用PrintPageEventArgs参数中包含的Graphics对象。

使用PrinterSettings.PrinterName属性可以指定使用哪一台打印机来打印文档。Print方法在打印文档的时不使用打印对话框,若要为用户提供选择打印设置的能力,则使用PrintDialog组件。

组件的事件

PrintPage事件在需要为当前页打印输出时发生。该事件应当与PrintPageHandle参数委托的签名匹配。

public delegate void PrintPageHandler(object sender,PrintPageEventArgs e );

PrintPageEventArgs参数为打印提供必要的系统数据,相关参数的说明如下:

Cancel ,设置为true时取消打印

Graphics 用于写到打印机的graphics对象

HasMorePages 指示是否还有待打印的页面默认为False

MarginBounds 表示整个页面的矩形区域

PageBounds 表示整个页面的矩形区域

Pagesettings 待打印的页面设置

PrintPreviewDialog组件

是预先配置的对话框,用于显示PrintDocument组件在打印时的外观。

Document属性:

用于获取或设置一个值,该值指示要预览的文档。

Public PrintDocument Document {get;set}

属性值:PrintDocument ,它表示要预览的文档

UseAntiAlias 属性:

获取或设置一个值,该值指示打印是否使用操作系统的放锯齿功能。

AutoSize 获取或设置一个值,该值指示PrintViewDialog是否自动调整大小以完整显示其内容。

private void button1_Click(object sender, EventArgs e)

{

//printDocument1.Print();

if (MessageBox.Show("是否要预览打印文档", "打印预览", MessageBoxButtons.YesNo) == DialogResult.Yes)

{

this.printPreviewDialog1.UseAntiAlias = true;

//设置要预览的文档

this.printPreviewDialog1.Document = this.printDocument1;

printPreviewDialog1.ShowDialog();

}

else

{

//调用Print方法直接打印文档

this.printDocument1.Print();

}

}

PrintPreviewControl组件

用于按文档打印时的外观显示PrintDocument组件。

Zoom属性:

获取或设置一个值,该值指示页面的大小。

Public double Zoom{get;set}

值1.0指示实际大小。

private void button1_Click(object sender, EventArgs e)

{

//设置打印页面为默认打印页面

printDocument1.DefaultPageSettings.Landscape = true;

//打印文档

this.printDocument1.Print();

}

//设置打印数据

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

string strPat = Application.StartupPath.Substring(0,Application .StartupPath .Substring(0,Application .StartupPath .IndexOf ("\\")).LastIndexOf ("\\"));

strPat += @"c.jpg";

e.Graphics.DrawString("阳光快乐",new Font ("宋体",50,FontStyle .Bold ),Brushes .Black,160,130 );

e.Graphics.DrawImage(Image .FromFile (strPat ),200,300,500,500);

}

//设置缩放比例尺

private void numericUpDown1_ValueChanged(object sender, EventArgs e)

{

int zoomNo;

zoomNo = Convert.ToInt32(numericUpDown1 .Value );

printPreviewControl1.Size = new Size(zoomNo *10,zoomNo *10);

printPreviewControl1.AutoZoom = true;

}

private void Form1_Load(object sender, EventArgs e)

{

this.printPreviewControl1.Document = this.printDocument1;

}

PrintDialog 组件

Windows窗体PrintDialog组件式一个预先配置的对话框,可用于在基于Windows应用程序中选择打印机、选择打印的页以及确定其他与打印相关的设置。

//启用“打印到文件”复选框

printDialog1.AllowPrintToFile = true;

//显示当前项按钮

printDialog1.AllowCurrentPage = true;

//启用选择按钮

printDialog1.AllowSelection = true;

//启用页按钮

printDialog1.AllowSomePages = true;

//要打印的文档

printDialog1.Document = this.printDocument1;

if (printDialog1 .ShowDialog ()==DialogResult.OK )

{

this.printPreviewDialog1.Document = this.printDocument1;

printDialog1.Document.Print();

//打印预览对话框设置

printPreviewDialog1.Document = this.printDocument1;

printPreviewDialog1.ShowDialog();

}

PageSetUpDialog 组件

是一个预先配置的页面对话框,用在基于Windows的应用程序设置页面信息,以便于打印。

//显示设置

this.pageSetupDialog1.Document = this.printDocument1;

//启用边距

this.pageSetupDialog1.AllowMargins = true;

this.pageSetupDialog1.AllowOrientation = true;

this.pageSetupDialog1.AllowPaper = true;

this.pageSetupDialog1.AllowPrinter = true;

//显示页面设置对话框

this.pageSetupDialog1.ShowDialog();

//设置打印预览对话框

this.printPreviewDialog1.Document = this.printDocument1;

//显示打印预览对话框

this.printPreviewDialog1.ShowDialog();

//使用打印控件打印

this.printDocument1.Print();