1.WinForms记事本打开
WinForms记事本打开
使用打印 dlgPageSetup printDocument_txt
菜单和工具栏menuStrip1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using Microsoft.Win;
namespace 记事本
{
public partial class Form_Notepad : Form
{
private string filename = "无标题";
private bool isEdited = false;
private Font font;
private Color color;
private string[] lines;
private int linesPrinted;
private bool 是源码否自动换行;
private AboutBox_Me aboutMe = new AboutBox_Me();
private string FileName
{
get { return filename; }
set
{
this.filename = value;
try
{
this.richTextBox_EditField.Clear();
this.richTextBox_EditField.Text = File.ReadAllText(filename, System.Text.Encoding.GetEncoding("GB"));
}
catch (IOException ioex)
{
MessageBox.Show(ioex.Message, "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
SetTitle();
}
}
private Font TxtFont
{
set
{
this.font = value;
this.richTextBox_EditField.Font = font;
}
}
public Form_Notepad()
{
InitializeComponent();
this.font = this.richTextBox_EditField.Font;
this.color = this.richTextBox_EditField.ForeColor;
this.richTextBox_EditField.WordWrap = false;
try
{
string msg = "版本 1.0";
//Registry.CurrentConfig.CreateSubKey("Software\\记事本");
Registry.SetValue("HKEY_CURRENT_CONFIG\\Software\\记事本", "记事本 ", msg, RegistryValueKind.String);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (是否自动换行)
{
}
}
private void SetTitle()
{
string[] spt = filename.Split(new char[] { '\\' });
this.Text = spt[spt.Length - 1] + " - 记事本";
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isEdited)
保存ToolStripMenuItem_Click(sender, e);
this.filename = "无标题";
SetTitle();
this.richTextBox_EditField.Clear();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dlgOpenFile = new OpenFileDialog();
dlgOpenFile.Title = "打开文件";
string dir = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
dlgOpenFile.InitialDirectory = dir;
dlgOpenFile.Filter = "Text documents (*.txt)|*.txt|All Files|*.*";
dlgOpenFile.ShowHelp = true;//显示帮助按钮(在右下角)
dlgOpenFile.HelpRequest += new EventHandler(ofd_HelpRequest);
dlgOpenFile.ValidateNames = true;//自动检查文件是否有效
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
this.FileName = dlgOpenFile.FileName;
}
void ofd_HelpRequest(object sender, EventArgs e)
{
MessageBox.Show("暂时无帮助");
}
void SaveFile()
{
try
{
File.WriteAllText(filename, this.richTextBox_EditField.Text, Encoding.UTF8);
}
catch (IOException ioex)
{
MessageBox.Show(ioex.Message, "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (filename == "无标题" || filename == "")
另存为ToolStripMenuItem_Click(sender,e);
else
try
{
File.WriteAllText(filename, this.richTextBox_EditField.Text);
}
catch (IOException ioex)
{
MessageBox.Show(ioex.Message, "记事本",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSaveFile = new SaveFileDialog();
dlgSaveFile.Filter = "Text documents (*.txt)|*.txt|All Files|*.*";
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
filename = dlgSaveFile.FileName;
SaveFile();
this.FileName = dlgSaveFile.FileName;
}
}
private void printDocument_txt_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while(linesPrinted < lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++], font, Brushes.Black, x, y);
y += (int)font.GetHeight() + 5;
if (y >= e.PageBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
linesPrinted = 0;
e.HasMorePages = false;
}
private void printDocument_txt_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
lines = this.richTextBox_EditField.Text.Split(new char[] { '\n' });
int i = 0;
char[] trimParam ={ '\r' };
foreach (string str in lines)
lines[i++] = str.TrimEnd(trimParam);
}
private void printDocument_txt_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
lines = null;
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
dlgPageSetup.ShowDialog();
}
catch (InvalidPrinterException ex)
{
MessageBox.Show(ex.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog dlgFont = new FontDialog();
dlgFont.Font = font;
if (dlgFont.ShowDialog() == DialogResult.OK)
this.TxtFont = dlgFont.Font;
}
private void richTextBox_EditField_TextChanged(object sender, EventArgs e)
{
this.isEdited = true;
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.自动换行ToolStripMenuItem.Checked)
{
this.richTextBox_EditField.WordWrap = true;
this.richTextBox_EditField.ScrollBars = RichTextBoxScrollBars.Vertical;
}
else
{
this.richTextBox_EditField.WordWrap = false;
this.richTextBox_EditField.ScrollBars = RichTextBoxScrollBars.Both;
}
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox_EditField.Undo();
}
//操作剪贴板
/* IDataObject d = Clipboard.GetDataObject () ;
if ( d.GetDataPresent ( DataFormats.Bitmap ) )
{
//"当前剪切板中的数据类型是位图!"
}
else if ( d.GetDataPresent ( DataFormats.Text ) )
{
//"当前剪切板中的源码头条源码系统源码数据类型是文本!"
}
else if ( d.GetDataPresent ( DataFormats.Html ) )
{
//"当前剪切板中的源码杭创的源码数据类型是超文本!"
}
else
{
//"当前剪切板中的源码erlang 源码如何安装数据类型是其他类型数据!"
}
Bitmap b = ( Bitmap ) d.GetData ( DataFormats.Bitmap ) ;//位图
String c = ( String ) d.GetData ( DataFormats.Text ) ;//文本
*/
private void 剪切ToolStripMenuItem_Click(object sender,源码android 源码 引入jar EventArgs e)
{
this.richTextBox_EditField.Cut();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetData(DataFormats.StringFormat, this.richTextBox_EditField.SelectedText);
//this.richTextBox_EditField.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox_EditField.Paste();
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox_EditField.SelectedText = "";
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form_Notepad_Load(object sender, EventArgs e)
{
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.DocumentName = filename;
doc.PrintPage += new PrintPageEventHandler(PrintPageEvent);
doc.Print();
}
private void PrintPageEvent(object sender, PrintPageEventArgs e)
{
int count = 0;
foreach (string line in richTextBox_EditField.Lines)
{
e.Graphics.DrawString(line, font, new SolidBrush(color), e.MarginBounds.Left, e.MarginBounds.Top + (count * font.GetHeight(e.Graphics)), new StringFormat());
count++;
}
}
}
}