学习笔记(一)没怎么加注释 再说吧
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
namespace print_test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private PrintDocument pd ;
private int k;
private int i;
private int n;
/// <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 );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(129, 17);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "打印";
//
// richTextBox1
//
this.richTextBox1.Font = new System.Drawing.Font("宋体", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.richTextBox1.Location = new System.Drawing.Point(24, 48);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(456, 344);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "richTextBox1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(128, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "打印预览";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 429);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pd_PrintPage(object sender,PrintPageEventArgs e)
{
Graphics g = e.Graphics; //获得绘图对象
int linesPerPage = 0; //页面的行号
float yPosition = 0; //绘制字符串的纵向位置
float leftMargin = e.MarginBounds.Left; //左边距
float topMargin = e.MarginBounds.Top; //上边距
string line = null; //行字符串
Font printFont = this.richTextBox1.Font; //当前的打印字体
SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
linesPerPage = (int)(e.MarginBounds.Height / printFont.GetHeight(g));//每页可打印的行数
yPosition = topMargin + printFont.GetHeight(g);
int linecount=this.richTextBox1.Lines.Length;//获得文本的总行数
int pagecount=0;//获得要打印的页数
if(linecount>0)
{
if(linecount%linesPerPage>0)
{
pagecount=(int)(linecount/linesPerPage)+1;
}
else
{
pagecount=(int)(this.richTextBox1.Lines.Length/linesPerPage);
}
}
if(pagecount>1)
{
int ppp;
if((linecount-n)>linesPerPage)
{
ppp=linesPerPage;
}
else
{
ppp=linecount-n;
}
for(n=k;(n<(ppp+k))&&(this.richTextBox1.Lines[n]!=null);n++)
{
line=this.richTextBox1.Lines[n].ToString();
yPosition = topMargin + ((n-k)*printFont.GetHeight(g));
g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
}
if((n+1)<linecount)
{
e.HasMorePages = true;
k=n;
i=n+linesPerPage;
}
else
{
e.HasMorePages = false;
}
}
else
{
for(int n=0;n<linecount;n++)
{
line=this.richTextBox1.Lines[n].ToString();
yPosition = topMargin + (n*printFont.GetHeight(g));
g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
pd=new PrintDocument();
pd.PrintPage+=new PrintPageEventHandler(pd_PrintPage);
this.printPreviewDialog1.Document=this.pd;
try
{
printPreviewDialog1.ShowDialog();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
浙公网安备 33010602011771号