MainForm.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:22k
- 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.Net.Mail;
- using System.Net;
- namespace myWorkPad
- {
- public partial class MainForm : Form
- {
- private int checkPrint;
- string strFileName = "";
- string strName= "";
- string strEmail = "";
- string strUserName;
- string strPass;
- string strPOP3;
- string strSMTP;
- string strAttach = "";
- public MainForm()
- {
- InitializeComponent();
- }
- private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
- {
- checkPrint = rtMain.Print(checkPrint, rtMain.TextLength, e);
- // Check for more pages
- if (checkPrint < rtMain.TextLength)
- e.HasMorePages = true;
- else
- e.HasMorePages = false;
- }
- private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
- {
- checkPrint = 0;
- }
- private void printDocument_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
- {
- MessageBox.Show(printDocument.DocumentName + " 已经打印完成!");
- }
- private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.InitialDirectory = "c:\";//注意这里写路径时要用c:\而不是c:
- openFileDialog.Filter = "文本文件|*.txt|RTF文件|*.rtf|html文件|*.html|所有文件|*.*";
- openFileDialog.RestoreDirectory = true;
- openFileDialog.FilterIndex = 2;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- string fName = openFileDialog.FileName;
- string strExt = System.IO.Path.GetExtension(fName).ToLower();
- if (strExt == ".rtf")
- {
- rtMain.LoadFile(fName, RichTextBoxStreamType.RichText);
- }
- else if (strExt == ".txt")
- {
- rtMain.LoadFile(fName, RichTextBoxStreamType.PlainText);
- }
- else if ((strExt == ".htm") || (strExt == ".html"))
- {
- // Read the HTML format
- StreamReader sr = File.OpenText(fName);
- string strHTML = sr.ReadToEnd();
- sr.Close();
- rtMain.AddHTML(strHTML);
- }
- printDocument.DocumentName = Path.GetFileName(fName);
- strFileName = fName;
- this.Text = Path.GetFileNameWithoutExtension(fName) +"--邮件";
- }
- catch (System.IO.FileNotFoundException)
- {
- MessageBox.Show("没有找到文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- rtMain.Focus();
- }
- private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "文本文件|*.txt|RTF文件|*.rtf|html文件|*.html|所有文件|*.*";
- saveFileDialog.FilterIndex = 2;
- saveFileDialog.RestoreDirectory = true;
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- string strName = saveFileDialog.FileName;
- switch (Path.GetExtension(strName).ToLower())
- {
- case ".txt":
- rtMain.SaveFile(strName, RichTextBoxStreamType.PlainText);
- break;
- case ".rtf":
- rtMain.SaveFile(strName, RichTextBoxStreamType.RichText);
- break;
- case ".html":
- {
- try
- {
- // save as HTML format
- string strText = rtMain.GetHTML(true, true);
- if (File.Exists(strName))
- File.Delete(strName);
- StreamWriter sr = File.CreateText(strName);
- sr.Write(strText);
- sr.Close();
- }
- catch
- {
- MessageBox.Show("无法保存文件!");
- }
- break;
- }
-
- default:
- rtMain.SaveFile(strName);
- break;
- }
- }
- catch (System.Exception err)
- {
- MessageBox.Show(err.Message);
- }
- }
- rtMain.Focus();
- }
- private void toolStripMenuItem1_Click(object sender, EventArgs e)
- {
- PageSetupDialog pageSetupDialog = new PageSetupDialog();
- pageSetupDialog.Document = printDocument;
- pageSetupDialog.ShowDialog();
- }
- private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- PrintDialog printDialog = new PrintDialog();
- printDialog.Document = printDocument;
- if (printDialog.ShowDialog() != DialogResult.Cancel)
- {
- try
- {
- printDocument.Print();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
- {
- StringReader lineReader = new StringReader(rtMain.Text);
- try
- {
- PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
- printPreviewDialog.Document = printDocument;
- printPreviewDialog.FormBorderStyle = FormBorderStyle.Fixed3D;
- printPreviewDialog.ShowDialog();
- }
- catch (Exception excep)
- {
- MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- }
- private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.Undo();
- }
- private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.Redo();
- }
- private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.Cut();
- }
- private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.Copy();
- }
- private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.Paste();
- }
- private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.SelectAll();
- }
- private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.Clear();
- }
- private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
- {
- FontDialog fontDialog = new FontDialog();
- fontDialog.Color = rtMain.ForeColor;
- fontDialog.AllowScriptChange = true;
- //fontDialog.ShowColor = true;
- if (fontDialog.ShowDialog() != DialogResult.Cancel)
- {
- rtMain.SelectionFont = fontDialog.Font;//将当前选定的文字改变字体
- // rtMain.SelectionColor = fontDialog.Color;
- }
- rtMain.Focus();
- }
- private void 颜色CToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ColorDialog colorDialog = new ColorDialog();
- colorDialog.AllowFullOpen = true;
- colorDialog.FullOpen = true;
- colorDialog.ShowHelp = true;
- colorDialog.Color = Color.Black;//初始化当前文本框中的字体颜色,当用户在ColorDialog对话框中点击"取消"按钮恢复原来的值
- if (colorDialog.ShowDialog() != DialogResult.Cancel)
- {
- rtMain.SelectionColor = colorDialog.Color; //将当前选定的文字改变颜色
- }
- rtMain.Focus();
- }
- private void 图像PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- // openFileDialog.InitialDirectory = "c:\";//注意这里写路径时要用c:\而不是c:
- openFileDialog.Filter = "位图文件|*.bmp|gif文件|*.gif|TIFF文件|*.tif|jpg文件|*.jpg|所有文件|*.*";
- openFileDialog.RestoreDirectory = true;
- openFileDialog.FilterIndex = 1;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- string fName = openFileDialog.FileName;
- Image img;
- img = Image.FromFile(fName);
- Clipboard.SetDataObject(img);
- DataFormats.Format df;
- df = DataFormats.GetFormat(DataFormats.Bitmap);
- if (this.rtMain.CanPaste(df))
- {
- this.rtMain.Paste(df);
- }
- }
- catch (System.IO.FileNotFoundException)
- {
- MessageBox.Show("没有找到文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- rtMain.Focus();
- }
- private void 左对齐LToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.SelectionAlignment = HorizontalAlignment.Left;
- }
- private void 居中EToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.SelectionAlignment = HorizontalAlignment.Center;
- }
- private void 右对齐RToolStripMenuItem_Click(object sender, EventArgs e)
- {
- rtMain.SelectionAlignment = HorizontalAlignment.Right;
- }
- private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
- {
- AboutDialog dlg = new AboutDialog();
- dlg.ShowDialog();
- }
- private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (strFileName != "")
- {
- try
- {
- switch (Path.GetExtension(strFileName).ToLower())
- {
- case ".txt":
- rtMain.SaveFile(strFileName, RichTextBoxStreamType.PlainText);
- break;
- case ".rtf":
- rtMain.SaveFile(strFileName, RichTextBoxStreamType.RichText);
- break;
- case ".html":
- {
- try
- {
- // save as HTML format
- string strText = rtMain.GetHTML(true, true);
- if (File.Exists(strFileName))
- File.Delete(strFileName);
- StreamWriter sr = File.CreateText(strFileName);
- sr.Write(strText);
- sr.Close();
- }
- catch
- {
- MessageBox.Show("无法保存文件!");
- }
- break;
- }
- default:
- rtMain.SaveFile(strFileName);
- break;
- }
- }
- catch (System.Exception err)
- {
- MessageBox.Show(err.Message);
- }
- }
- else
- 另存为AToolStripMenuItem_Click(sender, e);
- }
- private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("要保存现有文件吗", "提示对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
- == DialogResult.Yes)
- 保存SToolStripMenuItem_Click(sender, e);
- else
- {
- strFileName = "";
- rtMain.Clear();
- }
- this.Text = "新建文档";
- }
- private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem ts = (ToolStripMenuItem)sender;
- ts.Checked = !(ts.Checked);
- toolMain.Visible = ts.Checked;
- }
- private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem ts = (ToolStripMenuItem)sender;
- ts.Checked = !(ts.Checked);
- statusMain.Visible = ts.Checked;
- }
-
- private void 格式栏FToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem ts = (ToolStripMenuItem)sender;
- ts.Checked = !(ts.Checked);
- toolFormat.Visible = ts.Checked;
- }
- private void tsbBold_Click(object sender, EventArgs e)
- {
- //处理粗体显示
- System.Drawing.Font oldFont = rtMain.SelectionFont;
- System.Drawing.Font newFont;
- if (oldFont.Bold)
- newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
- else
- newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
- rtMain.SelectionFont = newFont;
- rtMain.Focus();
- }
- private void ttsbItalic_Click(object sender, EventArgs e)
- {
- //处理斜体显示
- System.Drawing.Font oldFont = rtMain.SelectionFont;
- System.Drawing.Font newFont;
- if (oldFont.Italic)
- newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
- else
- newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
- rtMain.SelectionFont = newFont;
- rtMain.Focus();
- }
- private void ttsbUnderLine_Click(object sender, EventArgs e)
- {
- //处理下划线显示
- System.Drawing.Font oldFont = rtMain.SelectionFont; ;
- System.Drawing.Font newFont;
- if(oldFont.Underline)
- newFont = new Font(oldFont,oldFont.Style&~FontStyle.Underline);
- else
- newFont = new Font(oldFont,oldFont.Style|FontStyle.Underline);
- rtMain.SelectionFont = newFont;
- rtMain.Focus();
- }
- private void tsbBold_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "将选定内容变为粗体";
- }
- private void tsbBold_MouseLeave(object sender, EventArgs e)
- {
- statusLabel.Text = "邮件发送系统1.0";
- }
- private void ttsbItalic_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "将选定内容变为斜体";
- }
- private void ttsbUnderLine_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "用连续下划线格式化选定内容";
- }
- private void 新建NToolStripButton_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "创建新文档";
- }
- private void 打开OToolStripButton_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "打开已存文档";
- }
- private void 保存SToolStripButton_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "保存当前文档";
- }
- private void 打印PToolStripButton_MouseEnter(object sender, EventArgs e)
- {
- statusLabel.Text = "打印当前文档";
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- //初始化字体选择
- foreach (FontFamily oneFontFamily in FontFamily.Families)
- {
- tscbFont.Items.Add(oneFontFamily.Name);
- }
- tscbFont.SelectedItem = "宋体";
- tscbSize.SelectedIndex = 2;
- cbFormat.SelectedIndex = 0;
- cbPriority.SelectedIndex = 0;
- }
- private void tscbFont_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (tscbSize.SelectedItem == null)
- return;
- FontFamily family = new FontFamily(tscbFont.SelectedItem.ToString());
- float emSize = float.Parse(tscbSize.SelectedItem.ToString());
- System.Drawing.Font newFont = new Font(family,emSize);;
- rtMain.SelectionFont = newFont;
- rtMain.Focus();
- }
- private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- frmFind f = new frmFind(this);
- f.Show();
-
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void 替换SToolStripMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- frmReplace f = new frmReplace(this);
- f.Show();
-
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void 项目符号NToolStripMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- 项目符号NToolStripMenuItem.Checked = !项目符号NToolStripMenuItem.Checked;
- if (项目符号NToolStripMenuItem.Checked)
- {
- rtMain.BulletIndent = 10;
- rtMain.SelectionBullet = true;
- }
- else
- rtMain.SelectionBullet = false;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void btnSend_Click(object sender, EventArgs e)
- {
- if (strEmail == "")
- {
- MessageBox.Show("请先设置帐户信息!");
- return;
- }
- try
- {
- //设置代理服务器
- //System.Net.WebProxy proxy = new System.Net.WebProxy();
- //proxy.UseDefaultCredentials = true;
- //proxy.BypassProxyOnLocal = true;
- //System.Net.GlobalProxySelection.Select = proxy;
- SmtpClient sc = new SmtpClient();
- sc.Host = strSMTP;
- sc.DeliveryMethod = SmtpDeliveryMethod.Network;
- MailMessage m = new MailMessage(strEmail, tbReceive.Text);
- if(tbCC.Text!="")
- {
- MailAddress copy = new MailAddress(tbCC.Text);
- m.CC.Add(copy);
- }
- m.Subject = tbSubject.Text;
- switch (cbPriority.SelectedIndex)
- {
- case 0:
- m.Priority = MailPriority.High;
- break;
- case 1:
- m.Priority = MailPriority.Normal;
- break;
- case 2:
- m.Priority = MailPriority.Low;
- break;
- default:
- break;
- }
- if (cbFormat.SelectedIndex == 1)//text
- {
- m.IsBodyHtml = false;
- m.Body = rtMain.Text;
- }
- else//html
- {
- m.IsBodyHtml = true;
- m.Body = rtMain.GetHTML(true, true);
- }
- if (strFileName!="")
- {
- m.Attachments.Add(new Attachment(strFileName));
- }
- //以下设定身份验证
- NetworkCredential myCred = new NetworkCredential(strUserName, strPass);
- sc.Credentials = myCred;
- sc.Send(m);
- MessageBox.Show("发送成功!");
- }
- catch (SmtpException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void 邮件账户设置ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- frmSet dlg = new frmSet();
- if (dlg.ShowDialog() == DialogResult.OK)
- {
- strName = dlg.strName;
- strEmail = dlg.strEmail;
- strUserName = dlg.strUserName;
- strPass = dlg.strPass;
- strPOP3 = dlg.strPOP3;
- strSMTP = dlg.strSMTP;
- }
- }
- private void tbInsertA_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "所有文件|*.*";
- openFileDialog.FilterIndex = 2;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- strFileName = openFileDialog.FileName;
- }
- }
- }
- }