⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 print.cs

📁 自己写的多窗口文本编辑器
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace 窗口编辑器
{
	/// <summary>
	/// Print 的摘要说明。
	/// </summary>
	public class Print : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.PrintPreviewDialog ppDialog;
		private System.Drawing.Printing.PrintDocument pDoc;
		public System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton tbnP;
		private System.Windows.Forms.ToolBarButton tbnPP;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.TextBox textBox;
		private System.ComponentModel.IContainer components;

		public Print()
		{
			//
			// 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()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Print));
			this.textBox = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
			this.pDoc = new System.Drawing.Printing.PrintDocument();
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.tbnP = new System.Windows.Forms.ToolBarButton();
			this.tbnPP = new System.Windows.Forms.ToolBarButton();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.SuspendLayout();
			// 
			// textBox
			// 
			this.textBox.AllowDrop = true;
			this.textBox.Location = new System.Drawing.Point(24, 40);
			this.textBox.Multiline = true;
			this.textBox.Name = "textBox";
			this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox.Size = new System.Drawing.Size(320, 264);
			this.textBox.TabIndex = 0;
			this.textBox.Text = "textBox1";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(376, 32);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "预览";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// ppDialog
			// 
			this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
			this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
			this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
			this.ppDialog.Document = this.pDoc;
			this.ppDialog.Enabled = true;
			this.ppDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("ppDialog.Icon")));
			this.ppDialog.Location = new System.Drawing.Point(194, 32);
			this.ppDialog.MinimumSize = new System.Drawing.Size(375, 250);
			this.ppDialog.Name = "ppDialog";
			this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
			this.ppDialog.Visible = false;
			// 
			// pDoc
			// 
			this.pDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pDoc_PrintPage);
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.tbnP,
																						this.tbnPP});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageList1;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(480, 28);
			this.toolBar1.TabIndex = 2;
			this.toolBar1.Visible = false;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// tbnP
			// 
			this.tbnP.ImageIndex = 1;
			this.tbnP.Tag = "tbnP";
			// 
			// tbnPP
			// 
			this.tbnPP.ImageIndex = 0;
			this.tbnPP.Tag = "tbnPP";
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// Print
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(480, 325);
			this.Controls.Add(this.toolBar1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox);
			this.Name = "Print";
			this.Text = "Print";
			this.ResumeLayout(false);

		}
		#endregion
        int lineNo=0;
		private void pDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			Font font =new Font ("宋体",12);
			Brush brush= Brushes.Red ;
			float h=Font.GetHeight (e.Graphics );
			int lineCount=(int)(e.MarginBounds .Height/h);
			int i=0;

			
			
		
			for( ;lineNo<textBox.Lines .Length ;lineNo++,i++)
			{
				PointF p=new PointF (e.MarginBounds.Left ,e.MarginBounds .Top +h*i);
				e.Graphics .DrawString (textBox.Lines [lineNo],font,brush,p);
				SizeF s=e.Graphics .MeasureString (textBox.Lines [lineNo],font,e.MarginBounds .Width );
				RectangleF r=new RectangleF(p,s);
				if(i+1>=lineCount)
				{
					if(lineNo+1<textBox.Lines .Length )
						e.HasMorePages =true;
					lineNo++;
					 break;
				
				}
				
			}
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			ppDialog.ShowDialog(); 
		}

		public void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch(e.Button .Tag .ToString ())
			{
				case "tbnP":textBox.Cut();
					break;
				case "tbnPP": textBox.Paste ();
					break;
				
			}
		}

	
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -