打印及打印预览的实现

Posted on 2005-11-11 09:24  中国风  阅读(911)  评论(1编辑  收藏

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;

namespace EditerApp
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem menuItem2;
  private System.Windows.Forms.MenuItem menuItem3;
  private System.Windows.Forms.MenuItem menuItem4;
  private System.Windows.Forms.MenuItem menuItem5;
  private System.Windows.Forms.MenuItem menuItem6;
  private System.Windows.Forms.MenuItem menuItem7;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  private System.Windows.Forms.RichTextBox MyRTBox;
  private System.Windows.Forms.StatusBar MyStatus;
  private System.Windows.Forms.MenuItem menuItem8;
  private System.Windows.Forms.MenuItem menuItem9;
  private System.Windows.Forms.MenuItem menuItem10;
  private System.Windows.Forms.MenuItem menuItem11;
  private System.Drawing.Printing.PrintDocument MyPrintDC;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  private StringReader MySReader; //字符串流对象,一行一行读取文本

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.MyRTBox = new System.Windows.Forms.RichTextBox();
   this.MyStatus = new System.Windows.Forms.StatusBar();
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem4 = new System.Windows.Forms.MenuItem();
   this.menuItem5 = new System.Windows.Forms.MenuItem();
   this.menuItem6 = new System.Windows.Forms.MenuItem();
   this.menuItem7 = new System.Windows.Forms.MenuItem();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   this.menuItem8 = new System.Windows.Forms.MenuItem();
   this.menuItem9 = new System.Windows.Forms.MenuItem();
   this.menuItem10 = new System.Windows.Forms.MenuItem();
   this.menuItem11 = new System.Windows.Forms.MenuItem();
   this.MyPrintDC = new System.Drawing.Printing.PrintDocument();
   this.SuspendLayout();
   //
   // MyRTBox
   //
   this.MyRTBox.Dock = System.Windows.Forms.DockStyle.Fill;
   this.MyRTBox.Name = "MyRTBox";
   this.MyRTBox.Size = new System.Drawing.Size(292, 273);
   this.MyRTBox.TabIndex = 0;
   this.MyRTBox.Text = "";
   //
   // MyStatus
   //
   this.MyStatus.Location = new System.Drawing.Point(0, 251);
   this.MyStatus.Name = "MyStatus";
   this.MyStatus.Size = new System.Drawing.Size(292, 22);
   this.MyStatus.TabIndex = 1;
   this.MyStatus.Text = "新建文件";
   //
   // mainMenu1
   //
   this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem1,
                       this.menuItem7});
   //
   // menuItem1
   //
   this.menuItem1.Index = 0;
   this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem3,
                       this.menuItem2,
                       this.menuItem4,
                       this.menuItem5,
                       this.menuItem8,
                       this.menuItem9,
                       this.menuItem10,
                       this.menuItem11,
                       this.menuItem6});
   this.menuItem1.Text = "文件(&F)";
   //
   // menuItem3
   //
   this.menuItem3.Index = 0;
   this.menuItem3.Text = "新建(&N)";
   this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
   //
   // menuItem2
   //
   this.menuItem2.Index = 1;
   this.menuItem2.Text = "打开(&O)";
   this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
   //
   // menuItem4
   //
   this.menuItem4.Index = 2;
   this.menuItem4.Text = "保存(&S)";
   this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
   //
   // menuItem5
   //
   this.menuItem5.Index = 3;
   this.menuItem5.Text = "-";
   //
   // menuItem6
   //
   this.menuItem6.Index = 8;
   this.menuItem6.Text = "退出(&X)";
   this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
   //
   // menuItem7
   //
   this.menuItem7.Index = 1;
   this.menuItem7.Text = "关于(&A)";
   //
   // openFileDialog1
   //
   this.openFileDialog1.Filter = "Text Files(*.txt)|*.txt|Rich Text Format Files(*.rtf)|*.rtf|All Files(*.*)|*.*";
   //
   // saveFileDialog1
   //
   this.saveFileDialog1.FileName = "doc1";
   this.saveFileDialog1.Filter = "Text Files(*.txt)|*.txt|Rich Text Format Files(*.rtf)|*.rtf|All Files(*.*)|*.*";
   //
   // menuItem8
   //
   this.menuItem8.Index = 4;
   this.menuItem8.Text = "页面设置(&U)";
   this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
   //
   // menuItem9
   //
   this.menuItem9.Index = 5;
   this.menuItem9.Text = "打印预览(&V)";
   this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
   //
   // menuItem10
   //
   this.menuItem10.Index = 6;
   this.menuItem10.Text = "打印(&P)";
   this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
   //
   // menuItem11
   //
   this.menuItem11.Index = 7;
   this.menuItem11.Text = "-";
   //
   // MyPrintDC
   //
   this.MyPrintDC.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.MyPrintDC_PrintPage);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.MyStatus,
                    this.MyRTBox});
   this.Menu = this.mainMenu1;
   this.Name = "Form1";
   this.Text = "PrintEditorApp";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void CheckSave()
  {
   if (MyRTBox.Text != "")
   {
    if (MessageBox.Show ("是否保存当前文件?","确认" ,MessageBoxButtons.OKCancel  ) == DialogResult.OK )
    {
     MySaveFile();
    }
   }
  }

  private void MyNewFile()
  {
   CheckSave(); //检查是否保存当前文件
   MyRTBox.Clear ();
   MyStatus.Text = "新建文件";
  }

  private void MySaveFile()
  {
   MyStatus.Text = "保存文件";
   if (saveFileDialog1.ShowDialog () == DialogResult.OK )
   {
    MyRTBox.SaveFile (saveFileDialog1.FileName );
   }
  }
  private void MyOpenFile()
  {
   CheckSave(); //检查是否保存当前文件
   
   if (openFileDialog1.ShowDialog () == DialogResult.OK )
   {
    MyRTBox.LoadFile (openFileDialog1.FileName ,RichTextBoxStreamType.PlainText );
    MyStatus.Text = "打开文件";
   } 
  }
  private void MyExit()
  {
   CheckSave(); //检查是否保存当前文件

   Application.Exit ();
  }

  private void menuItem3_Click(object sender, System.EventArgs e)
  {
   MyNewFile(); //新建文件
  }

  private void menuItem2_Click(object sender, System.EventArgs e)
  {   
   MyOpenFile();//打开文件 
  }

  private void menuItem4_Click(object sender, System.EventArgs e)
  {
   MySaveFile();//保存文件
  }

  private void menuItem6_Click(object sender, System.EventArgs e)
  {
   MyExit();//退出程序
  }

  private void menuItem10_Click(object sender, System.EventArgs e)
  {
   PrintDialog MyPrintDg = new PrintDialog (); //创建打印对话框
   MyPrintDg.Document = MyPrintDC ;
   MySReader = new StringReader (MyRTBox.Text );
   if (MyPrintDg.ShowDialog () == DialogResult.OK )
   {
    try
    {
     MyPrintDC.Print ();
    }
    catch
    {   //停止打印
     MyPrintDC.PrintController.OnEndPrint (MyPrintDC, new System.Drawing.Printing.PrintEventArgs() );
    }
   }
  }

  private void MyPrintDC_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  {
   Graphics MyGraphics = e.Graphics ;
   Font MyPrintFont = MyRTBox.Font ;
   //每一页的行数
   float iLinePerPage = e.MarginBounds.Height  / MyPrintFont.GetHeight (MyGraphics);
   int iLineNumber = 0; //打印时的行计数器
   float fYPosition = 0;//打印时的纵坐标
   float fMarginLeft = e.MarginBounds .Left ;
   float fMarginTop = e.MarginBounds .Top;
   string strLine = ""; //每一行要打印的文本
   while ((iLineNumber < iLinePerPage) &&
    ((strLine = MySReader.ReadLine ())!=null))
   {
    fYPosition = fMarginTop + iLineNumber * MyPrintFont.GetHeight(MyGraphics);
    MyGraphics.DrawString (strLine, MyPrintFont, new SolidBrush (Color.Black ),fMarginLeft ,fYPosition,new StringFormat ());
    iLineNumber ++;
   }
   if (strLine!= null)
   {
    e.HasMorePages = true;//没有打印完,出发下一次PrintPage事件
   }
   else {
    e.HasMorePages = false;
   }
  }

  private void menuItem8_Click(object sender, System.EventArgs e)
  {
   PageSetupDialog MyPageSetupDg = new PageSetupDialog ();//页面设置对话框
   MyPageSetupDg.Document = MyPrintDC;
   try
   {
    MyPageSetupDg.ShowDialog ();
   }
   catch { }
  }

  private void menuItem9_Click(object sender, System.EventArgs e)
  {
   PrintPreviewDialog MyPrintPreviewDg = new PrintPreviewDialog ();
   MyPrintPreviewDg.Document = MyPrintDC;
   
            try
   {
    MyPrintPreviewDg.ShowDialog ();
   }
   catch
   {
    MyPrintDC.PrintController .OnEndPrint (MyPrintDC, new System.Drawing .Printing .PrintEventArgs() );
   }
  }
 }
}

随笔 - 4, 文章 - 4, 评论 - 9

Copyright © 2021 中国风
Powered by .NET 5.0 on Kubernetes