winform(c#)中实现打印机相关功能
来源:互联网 发布:淘宝小号怎么找回来 编辑:程序博客网 时间:2024/04/30 14:17
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#)中实现打印机相关功能
- winform(c#)中实现打印机相关功能
- Winform 判断打印机是否可用,实现设置默认打印机功能
- C#WinForm程序中选择打印机打印
- C#实现打印机功能
- C#+Winform 实现切换用户功能
- C#winform实现锁屏功能
- 在WinForm中实现收缩功能
- Winform中实现TAB键的功能
- 在Winform中实现拖放的功能
- 栈相关功能的实现(C语言+C++)
- C#.NET vs2010中使用IrisSkin4.dll轻松实现WinForm窗体换肤功能
- winform 选择打印机功能(功能点:存取app.config节点)
- C#-WinForm登录窗体实现记住密码的功能(仿QQ实现)
- C#-WinForm登录窗体实现记住密码的功能(仿QQ实现)
- C#WinForm中实现异形窗体/控件
- C#学习之接口
- 关于测试代码
- new 和 override 的区别
- PopCalendar 3.50注释版
- 密码强度验证
- winform(c#)中实现打印机相关功能
- UML类图(类之间的关系)
- 关于将临时变量置为null是否有助于快速垃圾回收
- 判断全/半角问题
- C# 2.0 - 泛型(Generics)
- 将分页作为一个单独的控件
- 关于静态方法
- dataset
- 在.Net如何制作自定义的快捷方式(转)