.net打印控件基本用法

1、在winform上加如下控件

.net打印控件基本用法

2、代码和用法如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Winform
{
/*
在 Windows 窗体中进行打印主要包括以下两个方面:使用 PrintDocument 组件(Windows 窗体)组件来使用户可以打印;使用 PrintPreviewDialog 控件(Windows 窗体)控件、 PrintDialog 组件(Windows 窗体)组件和 PageSetupDialog 组件(Windows 窗体)组件向了解 Windows 操作系统的用户提供熟悉的图形界面
* printdocument使用方法:1、创建;2、在printpage事件中写每一页的打印逻辑;3、调用print方法
* printPreviewControl用法:1、创建;2、将document属性和printdocument关联;3、在printdocument的printpage事件里写打印逻辑;4、运行程序后会自动调用printpage事件;5、如果要刷新就用InvalidatePreview函数
*/
public partial class UsageOfControl6 : Form
{
string documentContents;//总打印字符
string stringToPrint;///要打印的
public UsageOfControl6()
{
InitializeComponent();
///考虑printPreviewControl组件在初始化时就调用了printdocument的printpage事件,在这里初始化打印数据
using (FileStream stream = new FileStream(@"d:\redist.txt", FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
documentContents = reader.ReadToEnd();
}
stringToPrint = documentContents;
} private void UsageOfControl6_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;//打印预览控件
printDialog1.Document = printDocument1;//打印设置控件
pageSetupDialog1.Document=printDocument1;//页面设置控件
printPreviewControl1.Document = printDocument1; using (FileStream stream = new FileStream(@"d:\redist.txt", FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
documentContents = reader.ReadToEnd();
}
stringToPrint = documentContents; if (printDialog1.ShowDialog()==DialogResult.OK)///通过printDialog1来设置printdocument1的属性:打印机,打印页数,打印方向
{
pageSetupDialog1.ShowDialog();///打印纸设置,边距,页眉,纸大小,也可以在printDocument1_PrintPage事件中获取e.PageSettings来设置
this.printPreviewDialog1.ShowDialog();///打印预览
printDocument1.Print();
printPreviewControl1.InvalidatePreview();////刷新,会再次调用printdocument的printpage事件
} } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int charactersOnPage = ;
int linesPerPage = ;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
//算出stringToPrint铺满打印页时的打印行数和字符数
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage); // Draws the string within the bounds of the page.///打印本页文字
///除了打印文字还可以打印线条,图形,屏幕截图等,可以在批定的位置打
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic); // Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);//下一页要打的 // Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > );///如果没有打印完就打印下一页,会自动调用这个事件来打印下一页 // If there are no more pages, reset the string to be printed.
if (!e.HasMorePages)
stringToPrint = documentContents; } private void button2_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage += ;///下一页
} private void button3_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage -= ;///上一页
} private void button4_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom += 0.1;///放大
} private void button5_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom -= 0.1;//缩小
}
}
}
上一篇:Bluetooth_FTP_SPEC: 蓝牙FTP介绍


下一篇:hdu 1896.Stones 解题报告