winform(c#)中實現打印機相關功能(轉)

*下面代碼主要涉及到的功能有:打印設置、頁面設置、打印預覽、打印

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

}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章