using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
namespace SimpleEditor
{
/// <summary>
/// 文本编辑器
/// 新建,编辑,保存,打印等功能
/// </summary>
public partial class SimpleEditorForm : Form
{
/// <summary>
/// 文件名
/// </summary>
private string filename = "Untitled";
private string[] lines;
private int linesPrinted;
/// <summary>
/// 构造函数
/// </summary>
public SimpleEditorForm(string filename)
{
InitializeComponent();
if (filename != null)
{
this.filename = filename;
OpenFile();
}
}
/// <summary>
/// 新建
/// </summary>
private void OnFileNew(object sender, EventArgs e)
{
filename = "Untitled";
SetFormTitle();
txtEdit.Clear();
}
/// <summary>
/// 打开
/// </summary>
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
SetFormTitle();
OpenFile();
}
}
/// <summary>
/// 保存
/// </summary>
private void OnFileSave(object sender, EventArgs e)
{
if (filename == "Untitled")
{
OnFileSaveAs(sender, e);
}
else
{
SaveFile();
}
}
/// <summary>
/// 另存为
/// </summary>
private void OnFileSaveAs(object sender, EventArgs e)
{
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
filename = dlgSaveFile.FileName;
SetFormTitle();
SaveFile();
}
}
/// <summary>
/// 打开文件
/// </summary>
protected void OpenFile()
{
try
{
txtEdit.Clear();
txtEdit.Text = File.ReadAllText(filename);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// <summary>
/// 保存文件
/// </summary>
protected void SaveFile()
{
try
{
File.WriteAllText(filename, txtEdit.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// <summary>
/// 设置窗体标题
/// </summary>
private void SetFormTitle()
{
FileInfo fileInfo = new FileInfo(filename);// FileInfo 帮助获取文件名
this.Text = fileInfo.Name + " - Simple Editor";
}
/// <summary>
/// 打印
/// </summary>
private void OnFilePrint(object sender, EventArgs e)
{
try
{
if (txtEdit.SelectedText != null) // 只有在选中了一些文本后才设置改属性为 True
{
dlgPrint.AllowSelection = true;// AllowSelection 属性设置了是否启用 "选中" 选项
}
if (dlgPrint.ShowDialog() == DialogResult.OK)
{
printDocument.Print(); // 此方法在 PrintController 类的帮助下会触发 PrintPage 事件
}
}
catch (InvalidPrinterException ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 打印预览
/// </summary>
private void OnFilePrintPreview(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
/// <summary>
/// 页面设置
/// </summary>
private void OnFilePageSetup(object sender, EventArgs e)
{
dlgPageSetup.ShowDialog();
}
/// <summary>
/// 退出
/// </summary>
private void OnFileExit(object sender, EventArgs e)
{
}
/// <summary>
/// PrintPage事件 对于要打印的每一页都会发生一次
/// </summary>
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while (linesPrinted < lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
y += 15;
if (y > e.PageBounds.Height - 80) // 减去 80 是控制不需要真正打印到页面的底部
{
// 设置 true,通知控制器还有下一页,打印没有结束
// 这里将会再次调用 OnPrintPage() ,因此 x,y 的值会被重置
// 而 linesPrinted 则是成员变量,会保持先前已记录的打印行数
e.HasMorePages = true;
return;
}
}
linesPrinted = 0;
e.HasMorePages = false;
}
/// <summary>
/// 打印之前,每一次打印任务只会发生一次
/// </summary>
private void OnBeginPrint(object sender, PrintEventArgs e)
{
char[] param = { '\n' };
if (dlgPrint.PrinterSettings.PrintRange == PrintRange.Selection)
{
lines = txtEdit.SelectedText.Split(param);
}
else
{
lines = txtEdit.Text.Split(param);
}
int i = 0;
char[] trimParam = { '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
/// <summary>
/// 打印结束,主动释放一下资源让垃圾回收机制尽快回收
/// </summary>
private void OnEndPrint(object sender, PrintEventArgs e)
{
lines = null;
}
}
}