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(" 打印出错 ");
     }
    }
   }

}
}