winform(c#)中实现打印机相关功能
2008-04-14 10:00
525 查看
下面代码主要涉及到的功能有:打印设置、页面设置、打印预览、打印
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
using System.IO;
namespace dayin
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private System.Windows.Forms.Button FileMenuItem_PrintSet;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button FileMenuItem_PageSet;
private System.Windows.Forms.Button FileMenuItem_PrintView;
private System.Windows.Forms.Button FileMenuItem_Print;
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
///
PrintDocument printDocument;
StringReader lineReader;
private void InitializeComponent()
{
this.printDocument = new System.Drawing.Printing.PrintDocument();
this.FileMenuItem_PrintSet = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.FileMenuItem_PageSet = new System.Windows.Forms.Button();
this.FileMenuItem_PrintView = new System.Windows.Forms.Button();
this.FileMenuItem_Print = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printDocument
//
this.printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument_PrintPage);
//
// FileMenuItem_PrintSet
//
this.FileMenuItem_PrintSet.Location = new System.Drawing.Point(0, 0);
this.FileMenuItem_PrintSet.Name = "FileMenuItem_PrintSet";
this.FileMenuItem_PrintSet.TabIndex = 0;
this.FileMenuItem_PrintSet.Text = "打印设置";
this.FileMenuItem_PrintSet.Click += new System.EventHandler(this.FileMenuItem_PrintSet_Click);
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(24, 72);
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(240, 184);
this.textBox.TabIndex = 1;
this.textBox.Text = "dfgfdghfthfgdhhgdfgf";
//
// FileMenuItem_PageSet
//
this.FileMenuItem_PageSet.Location = new System.Drawing.Point(88, 0);
this.FileMenuItem_PageSet.Name = "FileMenuItem_PageSet";
this.FileMenuItem_PageSet.TabIndex = 2;
this.FileMenuItem_PageSet.Text = "页面设置";
this.FileMenuItem_PageSet.Click += new System.EventHandler(this.FileMenuItem_PageSet_Click);
//
// FileMenuItem_PrintView
//
this.FileMenuItem_PrintView.Location = new System.Drawing.Point(176, 0);
this.FileMenuItem_PrintView.Name = "FileMenuItem_PrintView";
this.FileMenuItem_PrintView.TabIndex = 3;
this.FileMenuItem_PrintView.Text = "打印预览";
this.FileMenuItem_PrintView.Click += new System.EventHandler(this.FileMenuItem_PrintView_Click);
//
// FileMenuItem_Print
//
this.FileMenuItem_Print.Location = new System.Drawing.Point(88, 48);
this.FileMenuItem_Print.Name = "FileMenuItem_Print";
this.FileMenuItem_Print.TabIndex = 4;
this.FileMenuItem_Print.Text = "打印";
this.FileMenuItem_Print.Click += new System.EventHandler(this.FileMenuItem_Print_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(312, 325);
this.Controls.Add(this.FileMenuItem_Print);
this.Controls.Add(this.FileMenuItem_PrintView);
this.Controls.Add(this.FileMenuItem_PageSet);
this.Controls.Add(this.textBox);
this.Controls.Add(this.FileMenuItem_PrintSet);
this.Name = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1()); --”Form1“可以换成自己的
}
private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
{
Graphics g = e.Graphics;
float linesPerPage=0;
float yPosition=0;
int Count = 0;
float LeftMargin=e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string line = null;
Font printFont = this.textBox.Font;
SolidBrush MyBrush = new SolidBrush(Color.Black);
linesPerPage = e.MarginBounds.Height;
while(Count < linesPerPage && ((line=lineReader.ReadLine()) != null))
{
yPosition = TopMargin + (Count * printFont.GetHeight(g));
g.DrawString(line, printFont, MyBrush, LeftMargin, yPosition, new StringFormat());
Count++;
}
if(line!=null)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
}
}
private void FileMenuItem_PrintSet_Click(object sender, System.EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
dlgPrint.Document=printDocument;
dlgPrint.ShowDialog();
}
private void FileMenuItem_PageSet_Click(object sender, System.EventArgs e)
{
PageSetupDialog pageupSetupDialog = new PageSetupDialog();
pageupSetupDialog.Document=printDocument;
pageupSetupDialog.ShowDialog();
}
private void FileMenuItem_PrintView_Click(object sender, System.EventArgs e)
{
PrintPreviewDialog dlgPreviewPrint = new PrintPreviewDialog();
dlgPreviewPrint.Document=printDocument;
lineReader = new StringReader(textBox.Text);
try
{
dlgPreviewPrint.ShowDialog();
}
catch
{
MessageBox.Show(" 打印出错 ");
}
}
private void FileMenuItem_Print_Click(object sender, System.EventArgs e)
{
PrintDialog dlgPrint= new PrintDialog();
dlgPrint.Document=printDocument;
lineReader = new StringReader(textBox.Text);
if(dlgPrint.ShowDialog()==DialogResult.OK)
{
try
{
printDocument.Print();
}
catch
{
MessageBox.Show(" 打印出错 ");
}
}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
using System.IO;
namespace dayin
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private System.Windows.Forms.Button FileMenuItem_PrintSet;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button FileMenuItem_PageSet;
private System.Windows.Forms.Button FileMenuItem_PrintView;
private System.Windows.Forms.Button FileMenuItem_Print;
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
///
PrintDocument printDocument;
StringReader lineReader;
private void InitializeComponent()
{
this.printDocument = new System.Drawing.Printing.PrintDocument();
this.FileMenuItem_PrintSet = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.FileMenuItem_PageSet = new System.Windows.Forms.Button();
this.FileMenuItem_PrintView = new System.Windows.Forms.Button();
this.FileMenuItem_Print = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printDocument
//
this.printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument_PrintPage);
//
// FileMenuItem_PrintSet
//
this.FileMenuItem_PrintSet.Location = new System.Drawing.Point(0, 0);
this.FileMenuItem_PrintSet.Name = "FileMenuItem_PrintSet";
this.FileMenuItem_PrintSet.TabIndex = 0;
this.FileMenuItem_PrintSet.Text = "打印设置";
this.FileMenuItem_PrintSet.Click += new System.EventHandler(this.FileMenuItem_PrintSet_Click);
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(24, 72);
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(240, 184);
this.textBox.TabIndex = 1;
this.textBox.Text = "dfgfdghfthfgdhhgdfgf";
//
// FileMenuItem_PageSet
//
this.FileMenuItem_PageSet.Location = new System.Drawing.Point(88, 0);
this.FileMenuItem_PageSet.Name = "FileMenuItem_PageSet";
this.FileMenuItem_PageSet.TabIndex = 2;
this.FileMenuItem_PageSet.Text = "页面设置";
this.FileMenuItem_PageSet.Click += new System.EventHandler(this.FileMenuItem_PageSet_Click);
//
// FileMenuItem_PrintView
//
this.FileMenuItem_PrintView.Location = new System.Drawing.Point(176, 0);
this.FileMenuItem_PrintView.Name = "FileMenuItem_PrintView";
this.FileMenuItem_PrintView.TabIndex = 3;
this.FileMenuItem_PrintView.Text = "打印预览";
this.FileMenuItem_PrintView.Click += new System.EventHandler(this.FileMenuItem_PrintView_Click);
//
// FileMenuItem_Print
//
this.FileMenuItem_Print.Location = new System.Drawing.Point(88, 48);
this.FileMenuItem_Print.Name = "FileMenuItem_Print";
this.FileMenuItem_Print.TabIndex = 4;
this.FileMenuItem_Print.Text = "打印";
this.FileMenuItem_Print.Click += new System.EventHandler(this.FileMenuItem_Print_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(312, 325);
this.Controls.Add(this.FileMenuItem_Print);
this.Controls.Add(this.FileMenuItem_PrintView);
this.Controls.Add(this.FileMenuItem_PageSet);
this.Controls.Add(this.textBox);
this.Controls.Add(this.FileMenuItem_PrintSet);
this.Name = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1()); --”Form1“可以换成自己的
}
private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
{
Graphics g = e.Graphics;
float linesPerPage=0;
float yPosition=0;
int Count = 0;
float LeftMargin=e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string line = null;
Font printFont = this.textBox.Font;
SolidBrush MyBrush = new SolidBrush(Color.Black);
linesPerPage = e.MarginBounds.Height;
while(Count < linesPerPage && ((line=lineReader.ReadLine()) != null))
{
yPosition = TopMargin + (Count * printFont.GetHeight(g));
g.DrawString(line, printFont, MyBrush, LeftMargin, yPosition, new StringFormat());
Count++;
}
if(line!=null)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
}
}
private void FileMenuItem_PrintSet_Click(object sender, System.EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
dlgPrint.Document=printDocument;
dlgPrint.ShowDialog();
}
private void FileMenuItem_PageSet_Click(object sender, System.EventArgs e)
{
PageSetupDialog pageupSetupDialog = new PageSetupDialog();
pageupSetupDialog.Document=printDocument;
pageupSetupDialog.ShowDialog();
}
private void FileMenuItem_PrintView_Click(object sender, System.EventArgs e)
{
PrintPreviewDialog dlgPreviewPrint = new PrintPreviewDialog();
dlgPreviewPrint.Document=printDocument;
lineReader = new StringReader(textBox.Text);
try
{
dlgPreviewPrint.ShowDialog();
}
catch
{
MessageBox.Show(" 打印出错 ");
}
}
private void FileMenuItem_Print_Click(object sender, System.EventArgs e)
{
PrintDialog dlgPrint= new PrintDialog();
dlgPrint.Document=printDocument;
lineReader = new StringReader(textBox.Text);
if(dlgPrint.ShowDialog()==DialogResult.OK)
{
try
{
printDocument.Print();
}
catch
{
MessageBox.Show(" 打印出错 ");
}
}
}
}
}
相关文章推荐
- winform(c#)中实现打印机相关功能
- winform(c#)中实现打印机相关功能(转)
- winform(c#)中实现打印机相关功能
- C# WinForm 实现增删改查等功能(Access版) 系列之八-导出数据到Excel
- 【转】c# winform TreeView控件中实现拖拽的功能
- C#-WinForm登录窗体实现记住密码的功能(仿QQ实现)
- C#+AE实现类似IDentify功能及对高亮显示相关接口的总结
- C# WinForm 实现增删改查等功能(Access版) 系列之一
- C# WinForm 实现增删改查等功能(Access版) 系列之七-图片处理
- C# WinForm 实现增删改查等功能(Access版) 系列之五-查询
- Winform实现百度搜索框智能提示功能( C# )
- C#实现二维码功能,winform 以及 asp.net均可以用
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- C# Winform DataGridView分页功能的实现
- C#之winform实现文件拖拽功能
- C#Winform Linq_to_Sql 实现增删改查功能
- C# WinForm 实现增删改查等功能(Access版) 系列之二
- C# WinForm 实现增删改查等功能(Access版) 系列之二-增加
- C# WinForm 实现增删改查等功能(Access版) 系列之六-分页
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)