当前位置: 首页 > news >正文

RichTextBox简单扩展

最近做个小东西,需要用的RichTextBox控件,但是自带的功能又不能满足需要,就对VS2017中的RichTextBox进行了一个简单的扩展,实现一些简单的功能,还有一些bug存在

 

自定义控件代码

窗口设计代码,继承自RichTextBoxPrintCtrl

  1 namespace RichTextControl
  2 {
  3     partial class RichTextExtend
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region 组件设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要修改
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.components = new System.ComponentModel.Container();
 32             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RichTextExtend));
 33             this.statusStrip_Bottom = new System.Windows.Forms.StatusStrip();
 34             this.toolStripStatusLabel_Row = new System.Windows.Forms.ToolStripStatusLabel();
 35             this.toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel();
 36             this.toolStrip_BasicToolBar = new System.Windows.Forms.ToolStrip();
 37             this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
 38             this.toolStripCbx_FontName = new System.Windows.Forms.ToolStripComboBox();
 39             this.toolStripCbx_FontSize = new System.Windows.Forms.ToolStripComboBox();
 40             this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
 41             this.contextMenuStrip_RichTextBox = new System.Windows.Forms.ContextMenuStrip(this.components);
 42             this.ToolStripMenuItem_SelectAll = new System.Windows.Forms.ToolStripMenuItem();
 43             this.ToolStripMenuItem_SelectCurrentLine = new System.Windows.Forms.ToolStripMenuItem();
 44             this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
 45             this.ToolStripMenuItem_Copy = new System.Windows.Forms.ToolStripMenuItem();
 46             this.ToolStripMenuItem_Cut = new System.Windows.Forms.ToolStripMenuItem();
 47             this.ToolStripMenuItem_Paste = new System.Windows.Forms.ToolStripMenuItem();
 48             this.printDoc = new System.Drawing.Printing.PrintDocument();
 49             this.printDialog = new System.Windows.Forms.PrintDialog();
 50             this.printPreviewDialog = new System.Windows.Forms.PrintPreviewDialog();
 51             this.NToolStripButton = new System.Windows.Forms.ToolStripButton();
 52             this.OToolStripButton = new System.Windows.Forms.ToolStripButton();
 53             this.SToolStripButton = new System.Windows.Forms.ToolStripButton();
 54             this.PRToolStripButton = new System.Windows.Forms.ToolStripButton();
 55             this.PPToolStripButton = new System.Windows.Forms.ToolStripButton();
 56             this.UToolStripButton = new System.Windows.Forms.ToolStripButton();
 57             this.CToolStripButton = new System.Windows.Forms.ToolStripButton();
 58             this.PToolStripButton = new System.Windows.Forms.ToolStripButton();
 59             this.toolStripBtn_B = new System.Windows.Forms.ToolStripButton();
 60             this.toolStripBtn_I = new System.Windows.Forms.ToolStripButton();
 61             this.toolStripBtn_U = new System.Windows.Forms.ToolStripButton();
 62             this.toolStripBtn_DeleteLine = new System.Windows.Forms.ToolStripButton();
 63             this.toolStripBtn_Color = new System.Windows.Forms.ToolStripButton();
 64             this.toolStripBtn_BackColor = new System.Windows.Forms.ToolStripButton();
 65             this.toolStripBtn_FontSet = new System.Windows.Forms.ToolStripButton();
 66             this.toolStripBtn_Undo = new System.Windows.Forms.ToolStripButton();
 67             this.toolStripBtn_Redo = new System.Windows.Forms.ToolStripButton();
 68             this.richTxt = new RichTextControl.RichTextBoxPrintCtrl();
 69             this.statusStrip_Bottom.SuspendLayout();
 70             this.toolStrip_BasicToolBar.SuspendLayout();
 71             this.contextMenuStrip_RichTextBox.SuspendLayout();
 72             this.SuspendLayout();
 73             // 
 74             // statusStrip_Bottom
 75             // 
 76             this.statusStrip_Bottom.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
 77             this.toolStripStatusLabel_Row,
 78             this.toolStripStatusLabel2});
 79             this.statusStrip_Bottom.Location = new System.Drawing.Point(0, 444);
 80             this.statusStrip_Bottom.Name = "statusStrip_Bottom";
 81             this.statusStrip_Bottom.Size = new System.Drawing.Size(716, 22);
 82             this.statusStrip_Bottom.TabIndex = 0;
 83             this.statusStrip_Bottom.Text = "statusStrip1";
 84             // 
 85             // toolStripStatusLabel_Row
 86             // 
 87             this.toolStripStatusLabel_Row.Name = "toolStripStatusLabel_Row";
 88             this.toolStripStatusLabel_Row.Size = new System.Drawing.Size(44, 17);
 89             this.toolStripStatusLabel_Row.Text = "当前行";
 90             // 
 91             // toolStripStatusLabel2
 92             // 
 93             this.toolStripStatusLabel2.Name = "toolStripStatusLabel2";
 94             this.toolStripStatusLabel2.Size = new System.Drawing.Size(11, 17);
 95             this.toolStripStatusLabel2.Text = "|";
 96             // 
 97             // toolStrip_BasicToolBar
 98             // 
 99             this.toolStrip_BasicToolBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
100             this.NToolStripButton,
101             this.OToolStripButton,
102             this.SToolStripButton,
103             this.PRToolStripButton,
104             this.PPToolStripButton,
105             this.UToolStripButton,
106             this.CToolStripButton,
107             this.PToolStripButton,
108             this.toolStripSeparator1,
109             this.toolStripBtn_B,
110             this.toolStripBtn_I,
111             this.toolStripBtn_U,
112             this.toolStripBtn_DeleteLine,
113             this.toolStripBtn_Color,
114             this.toolStripBtn_BackColor,
115             this.toolStripCbx_FontName,
116             this.toolStripCbx_FontSize,
117             this.toolStripBtn_FontSet,
118             this.toolStripSeparator2,
119             this.toolStripBtn_Undo,
120             this.toolStripBtn_Redo});
121             this.toolStrip_BasicToolBar.Location = new System.Drawing.Point(0, 0);
122             this.toolStrip_BasicToolBar.Name = "toolStrip_BasicToolBar";
123             this.toolStrip_BasicToolBar.Size = new System.Drawing.Size(716, 26);
124             this.toolStrip_BasicToolBar.TabIndex = 1;
125             this.toolStrip_BasicToolBar.Text = "toolStrip1";
126             // 
127             // toolStripSeparator1
128             // 
129             this.toolStripSeparator1.Name = "toolStripSeparator1";
130             this.toolStripSeparator1.Size = new System.Drawing.Size(6, 26);
131             // 
132             // toolStripCbx_FontName
133             // 
134             this.toolStripCbx_FontName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
135             this.toolStripCbx_FontName.DropDownWidth = 200;
136             this.toolStripCbx_FontName.Name = "toolStripCbx_FontName";
137             this.toolStripCbx_FontName.Size = new System.Drawing.Size(100, 26);
138             this.toolStripCbx_FontName.Sorted = true;
139             this.toolStripCbx_FontName.SelectedIndexChanged += new System.EventHandler(this.toolStripCbx_FontName_SelectedIndexChanged);
140             // 
141             // toolStripCbx_FontSize
142             // 
143             this.toolStripCbx_FontSize.DropDownWidth = 40;
144             this.toolStripCbx_FontSize.Items.AddRange(new object[] {
145             "初号",
146             "小初",
147             "一号",
148             "小一",
149             "二号",
150             "小二",
151             "三号",
152             "小三",
153             "四号",
154             "小四",
155             "五号",
156             "小五",
157             "六号",
158             "小六",
159             "七号",
160             "八号",
161             "5",
162             "5.5",
163             "6",
164             "6.5",
165             "7.5",
166             "8",
167             "9",
168             "10",
169             "10.5",
170             "11",
171             "12",
172             "14",
173             "16",
174             "18",
175             "20",
176             "22",
177             "24",
178             "26",
179             "28",
180             "36",
181             "48",
182             "72",
183             "",
184             ""});
185             this.toolStripCbx_FontSize.Name = "toolStripCbx_FontSize";
186             this.toolStripCbx_FontSize.Size = new System.Drawing.Size(75, 26);
187             this.toolStripCbx_FontSize.SelectedIndexChanged += new System.EventHandler(this.toolStripCbx_FontSize_SelectedIndexChanged);
188             this.toolStripCbx_FontSize.TextChanged += new System.EventHandler(this.toolStripCbx_FontSize_TextChanged);
189             // 
190             // toolStripSeparator2
191             // 
192             this.toolStripSeparator2.Name = "toolStripSeparator2";
193             this.toolStripSeparator2.Size = new System.Drawing.Size(6, 26);
194             // 
195             // contextMenuStrip_RichTextBox
196             // 
197             this.contextMenuStrip_RichTextBox.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
198             this.ToolStripMenuItem_SelectAll,
199             this.ToolStripMenuItem_SelectCurrentLine,
200             this.toolStripMenuItem1,
201             this.ToolStripMenuItem_Copy,
202             this.ToolStripMenuItem_Cut,
203             this.ToolStripMenuItem_Paste});
204             this.contextMenuStrip_RichTextBox.Name = "contextMenuStrip_RichTextBox";
205             this.contextMenuStrip_RichTextBox.Size = new System.Drawing.Size(137, 120);
206             // 
207             // ToolStripMenuItem_SelectAll
208             // 
209             this.ToolStripMenuItem_SelectAll.Name = "ToolStripMenuItem_SelectAll";
210             this.ToolStripMenuItem_SelectAll.Size = new System.Drawing.Size(136, 22);
211             this.ToolStripMenuItem_SelectAll.Text = "全选";
212             this.ToolStripMenuItem_SelectAll.Click += new System.EventHandler(this.ToolStripMenuItem_SelectAll_Click);
213             // 
214             // ToolStripMenuItem_SelectCurrentLine
215             // 
216             this.ToolStripMenuItem_SelectCurrentLine.Name = "ToolStripMenuItem_SelectCurrentLine";
217             this.ToolStripMenuItem_SelectCurrentLine.Size = new System.Drawing.Size(136, 22);
218             this.ToolStripMenuItem_SelectCurrentLine.Text = "选中当前行";
219             this.ToolStripMenuItem_SelectCurrentLine.Click += new System.EventHandler(this.ToolStripMenuItem_SelectCurrentLine_Click);
220             // 
221             // toolStripMenuItem1
222             // 
223             this.toolStripMenuItem1.Name = "toolStripMenuItem1";
224             this.toolStripMenuItem1.Size = new System.Drawing.Size(133, 6);
225             // 
226             // ToolStripMenuItem_Copy
227             // 
228             this.ToolStripMenuItem_Copy.Name = "ToolStripMenuItem_Copy";
229             this.ToolStripMenuItem_Copy.Size = new System.Drawing.Size(136, 22);
230             this.ToolStripMenuItem_Copy.Text = "复制";
231             this.ToolStripMenuItem_Copy.Click += new System.EventHandler(this.CToolStripButton_Click);
232             // 
233             // ToolStripMenuItem_Cut
234             // 
235             this.ToolStripMenuItem_Cut.Name = "ToolStripMenuItem_Cut";
236             this.ToolStripMenuItem_Cut.Size = new System.Drawing.Size(136, 22);
237             this.ToolStripMenuItem_Cut.Text = "剪切";
238             this.ToolStripMenuItem_Cut.Click += new System.EventHandler(this.UToolStripButton_Click);
239             // 
240             // ToolStripMenuItem_Paste
241             // 
242             this.ToolStripMenuItem_Paste.Name = "ToolStripMenuItem_Paste";
243             this.ToolStripMenuItem_Paste.Size = new System.Drawing.Size(136, 22);
244             this.ToolStripMenuItem_Paste.Text = "粘贴";
245             this.ToolStripMenuItem_Paste.Click += new System.EventHandler(this.PToolStripButton_Click);
246             // 
247             // printDoc
248             // 
249             this.printDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.printDoc_BeginPrint);
250             this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
251             // 
252             // printDialog
253             // 
254             this.printDialog.Document = this.printDoc;
255             this.printDialog.UseEXDialog = true;
256             // 
257             // printPreviewDialog
258             // 
259             this.printPreviewDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
260             this.printPreviewDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
261             this.printPreviewDialog.ClientSize = new System.Drawing.Size(400, 300);
262             this.printPreviewDialog.Document = this.printDoc;
263             this.printPreviewDialog.Enabled = true;
264             this.printPreviewDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog.Icon")));
265             this.printPreviewDialog.Name = "printPreviewDialog";
266             this.printPreviewDialog.Visible = false;
267             // 
268             // NToolStripButton
269             // 
270             this.NToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
271             this.NToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("NToolStripButton.Image")));
272             this.NToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
273             this.NToolStripButton.Name = "NToolStripButton";
274             this.NToolStripButton.Size = new System.Drawing.Size(23, 23);
275             this.NToolStripButton.Text = "新建(&N)";
276             this.NToolStripButton.Click += new System.EventHandler(this.NToolStripButton_Click);
277             // 
278             // OToolStripButton
279             // 
280             this.OToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
281             this.OToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("OToolStripButton.Image")));
282             this.OToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
283             this.OToolStripButton.Name = "OToolStripButton";
284             this.OToolStripButton.Size = new System.Drawing.Size(23, 23);
285             this.OToolStripButton.Text = "打开(&O)";
286             this.OToolStripButton.Click += new System.EventHandler(this.OToolStripButton_Click);
287             // 
288             // SToolStripButton
289             // 
290             this.SToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
291             this.SToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("SToolStripButton.Image")));
292             this.SToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
293             this.SToolStripButton.Name = "SToolStripButton";
294             this.SToolStripButton.Size = new System.Drawing.Size(23, 23);
295             this.SToolStripButton.Text = "保存(&S)";
296             this.SToolStripButton.Click += new System.EventHandler(this.SToolStripButton_Click);
297             // 
298             // PRToolStripButton
299             // 
300             this.PRToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
301             this.PRToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("PRToolStripButton.Image")));
302             this.PRToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
303             this.PRToolStripButton.Name = "PRToolStripButton";
304             this.PRToolStripButton.Size = new System.Drawing.Size(23, 23);
305             this.PRToolStripButton.Text = "打印(&P)";
306             this.PRToolStripButton.Click += new System.EventHandler(this.PRToolStripButton_Click);
307             // 
308             // PPToolStripButton
309             // 
310             this.PPToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
311             this.PPToolStripButton.Image = global::RichTextControl.Properties.Resources.print_preview;
312             this.PPToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
313             this.PPToolStripButton.Name = "PPToolStripButton";
314             this.PPToolStripButton.Size = new System.Drawing.Size(23, 23);
315             this.PPToolStripButton.Text = "打印预览";
316             this.PPToolStripButton.Click += new System.EventHandler(this.toolStripBtn_PrintPreview_Click);
317             // 
318             // UToolStripButton
319             // 
320             this.UToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
321             this.UToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("UToolStripButton.Image")));
322             this.UToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
323             this.UToolStripButton.Name = "UToolStripButton";
324             this.UToolStripButton.Size = new System.Drawing.Size(23, 23);
325             this.UToolStripButton.Text = "剪切(&U)";
326             this.UToolStripButton.Click += new System.EventHandler(this.UToolStripButton_Click);
327             // 
328             // CToolStripButton
329             // 
330             this.CToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
331             this.CToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("CToolStripButton.Image")));
332             this.CToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
333             this.CToolStripButton.Name = "CToolStripButton";
334             this.CToolStripButton.Size = new System.Drawing.Size(23, 23);
335             this.CToolStripButton.Text = "复制(&C)";
336             this.CToolStripButton.Click += new System.EventHandler(this.CToolStripButton_Click);
337             // 
338             // PToolStripButton
339             // 
340             this.PToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
341             this.PToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("PToolStripButton.Image")));
342             this.PToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
343             this.PToolStripButton.Name = "PToolStripButton";
344             this.PToolStripButton.Size = new System.Drawing.Size(23, 23);
345             this.PToolStripButton.Text = "粘贴(&P)";
346             this.PToolStripButton.Click += new System.EventHandler(this.PToolStripButton_Click);
347             // 
348             // toolStripBtn_B
349             // 
350             this.toolStripBtn_B.BackColor = System.Drawing.SystemColors.Control;
351             this.toolStripBtn_B.CheckOnClick = true;
352             this.toolStripBtn_B.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
353             this.toolStripBtn_B.Font = new System.Drawing.Font("Microsoft YaHei UI", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
354             this.toolStripBtn_B.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_B.Image")));
355             this.toolStripBtn_B.ImageTransparentColor = System.Drawing.Color.Magenta;
356             this.toolStripBtn_B.Name = "toolStripBtn_B";
357             this.toolStripBtn_B.Size = new System.Drawing.Size(23, 23);
358             this.toolStripBtn_B.Text = "B";
359             this.toolStripBtn_B.ToolTipText = "设置粗体(B)";
360             this.toolStripBtn_B.Click += new System.EventHandler(this.toolStripBtn_B_Click);
361             // 
362             // toolStripBtn_I
363             // 
364             this.toolStripBtn_I.CheckOnClick = true;
365             this.toolStripBtn_I.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
366             this.toolStripBtn_I.Font = new System.Drawing.Font("Microsoft YaHei UI", 10.5F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
367             this.toolStripBtn_I.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_I.Image")));
368             this.toolStripBtn_I.ImageTransparentColor = System.Drawing.Color.Magenta;
369             this.toolStripBtn_I.Name = "toolStripBtn_I";
370             this.toolStripBtn_I.Size = new System.Drawing.Size(23, 23);
371             this.toolStripBtn_I.Text = "I";
372             this.toolStripBtn_I.ToolTipText = "设置斜体";
373             this.toolStripBtn_I.Click += new System.EventHandler(this.toolStripBtn_I_Click);
374             // 
375             // toolStripBtn_U
376             // 
377             this.toolStripBtn_U.CheckOnClick = true;
378             this.toolStripBtn_U.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
379             this.toolStripBtn_U.Font = new System.Drawing.Font("Microsoft YaHei UI", 10.5F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
380             this.toolStripBtn_U.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_U.Image")));
381             this.toolStripBtn_U.ImageTransparentColor = System.Drawing.Color.Magenta;
382             this.toolStripBtn_U.Name = "toolStripBtn_U";
383             this.toolStripBtn_U.Size = new System.Drawing.Size(24, 23);
384             this.toolStripBtn_U.Text = "U";
385             this.toolStripBtn_U.ToolTipText = "设置下划线";
386             this.toolStripBtn_U.Click += new System.EventHandler(this.toolStripBtn_U_Click);
387             // 
388             // toolStripBtn_DeleteLine
389             // 
390             this.toolStripBtn_DeleteLine.CheckOnClick = true;
391             this.toolStripBtn_DeleteLine.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
392             this.toolStripBtn_DeleteLine.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Strikeout))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
393             this.toolStripBtn_DeleteLine.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_DeleteLine.Image")));
394             this.toolStripBtn_DeleteLine.ImageTransparentColor = System.Drawing.Color.Magenta;
395             this.toolStripBtn_DeleteLine.Name = "toolStripBtn_DeleteLine";
396             this.toolStripBtn_DeleteLine.Size = new System.Drawing.Size(33, 23);
397             this.toolStripBtn_DeleteLine.Text = "abc";
398             this.toolStripBtn_DeleteLine.ToolTipText = "设置删除线";
399             this.toolStripBtn_DeleteLine.Click += new System.EventHandler(this.toolStripBtn_DeleteLine_Click);
400             // 
401             // toolStripBtn_Color
402             // 
403             this.toolStripBtn_Color.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
404             this.toolStripBtn_Color.ForeColor = System.Drawing.Color.Black;
405             this.toolStripBtn_Color.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_Color.Image")));
406             this.toolStripBtn_Color.ImageTransparentColor = System.Drawing.Color.Magenta;
407             this.toolStripBtn_Color.Name = "toolStripBtn_Color";
408             this.toolStripBtn_Color.Size = new System.Drawing.Size(23, 23);
409             this.toolStripBtn_Color.Text = "A";
410             this.toolStripBtn_Color.ToolTipText = "设置字体颜色";
411             this.toolStripBtn_Color.Click += new System.EventHandler(this.toolStripBtn_Color_Click);
412             // 
413             // toolStripBtn_BackColor
414             // 
415             this.toolStripBtn_BackColor.BackColor = System.Drawing.SystemColors.InactiveCaption;
416             this.toolStripBtn_BackColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
417             this.toolStripBtn_BackColor.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_BackColor.Image")));
418             this.toolStripBtn_BackColor.ImageTransparentColor = System.Drawing.Color.Magenta;
419             this.toolStripBtn_BackColor.Name = "toolStripBtn_BackColor";
420             this.toolStripBtn_BackColor.Size = new System.Drawing.Size(23, 23);
421             this.toolStripBtn_BackColor.Text = "A";
422             this.toolStripBtn_BackColor.ToolTipText = "设置当前文字的背景颜色";
423             this.toolStripBtn_BackColor.Click += new System.EventHandler(this.toolStripBtn_BackColor_Click);
424             // 
425             // toolStripBtn_FontSet
426             // 
427             this.toolStripBtn_FontSet.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
428             this.toolStripBtn_FontSet.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
429             this.toolStripBtn_FontSet.Image = ((System.Drawing.Image)(resources.GetObject("toolStripBtn_FontSet.Image")));
430             this.toolStripBtn_FontSet.ImageTransparentColor = System.Drawing.Color.Magenta;
431             this.toolStripBtn_FontSet.Name = "toolStripBtn_FontSet";
432             this.toolStripBtn_FontSet.Size = new System.Drawing.Size(24, 23);
433             this.toolStripBtn_FontSet.Text = "";
434             this.toolStripBtn_FontSet.Click += new System.EventHandler(this.toolStripBtn_FontSet_Click);
435             // 
436             // toolStripBtn_Undo
437             // 
438             this.toolStripBtn_Undo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
439             this.toolStripBtn_Undo.Image = global::RichTextControl.Properties.Resources.undo;
440             this.toolStripBtn_Undo.ImageTransparentColor = System.Drawing.Color.Magenta;
441             this.toolStripBtn_Undo.Name = "toolStripBtn_Undo";
442             this.toolStripBtn_Undo.Size = new System.Drawing.Size(23, 23);
443             this.toolStripBtn_Undo.Text = "拆销";
444             this.toolStripBtn_Undo.Click += new System.EventHandler(this.toolStripBtn_Undo_Click);
445             // 
446             // toolStripBtn_Redo
447             // 
448             this.toolStripBtn_Redo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
449             this.toolStripBtn_Redo.Image = global::RichTextControl.Properties.Resources.redo;
450             this.toolStripBtn_Redo.ImageTransparentColor = System.Drawing.Color.Magenta;
451             this.toolStripBtn_Redo.Name = "toolStripBtn_Redo";
452             this.toolStripBtn_Redo.Size = new System.Drawing.Size(23, 23);
453             this.toolStripBtn_Redo.Text = "重做";
454             this.toolStripBtn_Redo.Click += new System.EventHandler(this.toolStripBtn_Redo_Click);
455             // 
456             // richTxt
457             // 
458             this.richTxt.AcceptsTab = true;
459             this.richTxt.ContextMenuStrip = this.contextMenuStrip_RichTextBox;
460             this.richTxt.Cursor = System.Windows.Forms.Cursors.Arrow;
461             this.richTxt.Dock = System.Windows.Forms.DockStyle.Fill;
462             this.richTxt.EnableAutoDragDrop = true;
463             this.richTxt.ImeMode = System.Windows.Forms.ImeMode.On;
464             this.richTxt.Location = new System.Drawing.Point(0, 26);
465             this.richTxt.Name = "richTxt";
466             this.richTxt.Size = new System.Drawing.Size(716, 418);
467             this.richTxt.TabIndex = 2;
468             this.richTxt.Text = "";
469             this.richTxt.MouseClick += new System.Windows.Forms.MouseEventHandler(this.richTxt_MouseClick);
470             this.richTxt.TextChanged += new System.EventHandler(this.richTxt_TextChanged);
471             // 
472             // RichTextExtend
473             // 
474             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
475             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
476             this.Controls.Add(this.richTxt);
477             this.Controls.Add(this.toolStrip_BasicToolBar);
478             this.Controls.Add(this.statusStrip_Bottom);
479             this.Name = "RichTextExtend";
480             this.Size = new System.Drawing.Size(716, 466);
481             this.Load += new System.EventHandler(this.RichTextControl_Load);
482             this.statusStrip_Bottom.ResumeLayout(false);
483             this.statusStrip_Bottom.PerformLayout();
484             this.toolStrip_BasicToolBar.ResumeLayout(false);
485             this.toolStrip_BasicToolBar.PerformLayout();
486             this.contextMenuStrip_RichTextBox.ResumeLayout(false);
487             this.ResumeLayout(false);
488             this.PerformLayout();
489 
490         }
491 
492         #endregion
493         private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2;
494         private System.Windows.Forms.ToolStripButton UToolStripButton;
495         private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
496         public System.Windows.Forms.ToolStrip toolStrip_BasicToolBar;
497         /// <summary>
498         /// RichTextBox控件,其所有属性方法都可用
499         /// </summary>
500         //public System.Windows.Forms.RichTextBox richTxt;
501         RichTextBoxPrintCtrl richTxt;
502         public System.Windows.Forms.StatusStrip statusStrip_Bottom;
503         private System.Windows.Forms.ToolStripButton CToolStripButton;
504         private System.Windows.Forms.ToolStripButton PToolStripButton;
505         private System.Windows.Forms.ToolStripButton toolStripBtn_B;
506         private System.Windows.Forms.ToolStripButton toolStripBtn_I;
507         private System.Windows.Forms.ToolStripButton toolStripBtn_U;
508         private System.Windows.Forms.ToolStripComboBox toolStripCbx_FontName;
509         private System.Windows.Forms.ToolStripComboBox toolStripCbx_FontSize;
510         private System.Windows.Forms.ToolStripButton toolStripBtn_FontSet;
511         private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
512         private System.Windows.Forms.ToolStripButton toolStripBtn_DeleteLine;
513         private System.Windows.Forms.ToolStripButton toolStripBtn_Color;
514         private System.Windows.Forms.ToolStripButton toolStripBtn_Undo;
515         private System.Windows.Forms.ToolStripButton toolStripBtn_Redo;
516         private System.Windows.Forms.ToolStripButton toolStripBtn_BackColor;
517         private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel_Row;
518         private System.Windows.Forms.ContextMenuStrip contextMenuStrip_RichTextBox;
519         private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem_SelectAll;
520         private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem_SelectCurrentLine;
521         private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
522         private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem_Copy;
523         private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem_Cut;
524         private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem_Paste;
525         private System.Windows.Forms.ToolStripButton NToolStripButton;
526         private System.Windows.Forms.ToolStripButton OToolStripButton;
527         private System.Windows.Forms.ToolStripButton SToolStripButton;
528         private System.Windows.Forms.ToolStripButton PRToolStripButton;
529         private System.Windows.Forms.PrintDialog printDialog;
530         private System.Windows.Forms.PrintPreviewDialog printPreviewDialog;
531         private System.Drawing.Printing.PrintDocument printDoc;
532         private System.Windows.Forms.ToolStripButton PPToolStripButton;
533     }
534 }
View Code

实现代码

  1 namespace RichTextControl
  2 {
  3     /// <summary>
  4     /// RichTextBox控件的扩展
  5     /// </summary>
  6     [Description("扩展的RichText"), Category("RichTextExtend"),
  7         ToolboxItem(true),ToolboxBitmap(typeof(System.Windows.Forms.RichTextBox))]
  8     public partial class RichTextExtend: UserControl
  9     {
 10         private int checkPrint;
 11         private string stringFileName;//文件名,不包含路径
 12         /// <summary>
 13         /// 字体选择
 14         /// </summary>
 15         private FontSelect fontSelect;
 16 
 17         #region 是否显示工具栏
 18         private bool showStatusToolbar = true;
 19         [Browsable(true),Category("Toolbars"),Description("是否显示基础工具栏"),DefaultValue(true)]
 20         public bool ShowBasicToolbar { get; set; } = true;
 21         [Browsable(true),Category("Toolbars"), Description("是否显示状态栏"),DefaultValue(true)]
 22         public bool ShowStatusToolbar { get => showStatusToolbar; set => showStatusToolbar = value; }
 23         #endregion 
 24 
 25         #region 是否显示 BasicToolbar里的控件
 26         private bool showCut = true;
 27         private bool showCopy = true;
 28         private bool showPaste = true;
 29         private bool showFontSet = true;
 30         private bool showEffects = true;
 31         private bool showBackColor = true;
 32         private bool showUndo = true;
 33         private bool showRedo = true;
 34         private bool showNew = true;
 35         private bool showOpen = true;
 36         private bool showSave = true;
 37         private bool showPrint = true;
 38 
 39         [Description("是否显示剪切按钮"),Category("BasicToolbar"), DefaultValue(true)]
 40         public bool ShowCut { get => showCut; set => showCut = value; }
 41         [Description("是否显示复制按钮"), Category("BasicToolbar"), DefaultValue(true)]
 42         public bool ShowCopy { get => showCopy; set => showCopy = value; }
 43         [Description("是否显示粘贴按钮"), Category("BasicToolbar"), DefaultValue(true)]
 44         public bool ShowPaste { get => showPaste; set => showPaste = value; }
 45         [Description("是否显示字体设置项按钮"), Category("BasicToolbar"), DefaultValue(true)]
 46         public bool ShowFontSet { get => showFontSet; set => showFontSet = value; }
 47         [Description("是否显示下划线、删除线、字体颜色按钮"), Category("BasicToolbar"), DefaultValue(true)]
 48         public bool ShowEffects { get => showEffects; set => showEffects = value; }
 49         [Description("是否显示字体背景颜色按钮"), Category("BasicToolbar"), DefaultValue(true)]
 50         public bool ShowBackColor { get => showBackColor; set => showBackColor = value; }
 51         [Description("是否显示拆销按钮"), Category("BasicToolbar"), DefaultValue(true)]
 52         public bool ShowUndo { get => showUndo; set => showUndo = value; }
 53         [Description("是否显示重做按钮"), Category("BasicToolbar"), DefaultValue(true)]
 54         public bool ShowRedo { get => showRedo; set => showRedo = value; }
 55         [Description("是否显示新建按钮"), Category("BasicToolbar"), DefaultValue(true)]
 56         public bool ShowNew { get => showNew; set => showNew = value; }
 57         [Description("是否显示打开按钮"), Category("BasicToolbar"), DefaultValue(true)]
 58         public bool ShowOpen { get => showOpen; set => showOpen = value; }
 59         [Description("是否显示保存按钮"), Category("BasicToolbar"), DefaultValue(true)]
 60         public bool ShowSave { get => showSave; set => showSave = value; }
 61         [Description("是否显示打印按钮"), Category("BasicToolbar"), DefaultValue(true)]
 62         public bool ShowPrint { get => showPrint; set => showPrint = value; }
 63 
 64         #endregion
 65 
 66         #region 窗体事件
 67         public RichTextExtend()
 68         {
 69             InitializeComponent();
 70         }
 71 
 72         /// <summary>
 73         /// 加载事件
 74         /// </summary>
 75         /// <param name="sender"></param>
 76         /// <param name="e"></param>
 77         private void RichTextControl_Load(object sender, EventArgs e)
 78         {
 79             LoadDefaultSet();
 80         }
 81         /// <summary>
 82         /// 加载默认设置
 83         /// </summary>
 84         private void LoadDefaultSet()
 85         {
 86             this.statusStrip_Bottom.Visible = ShowStatusToolbar;
 87             this.toolStrip_BasicToolBar.Visible = ShowBasicToolbar;
 88             if (ShowBasicToolbar)
 89             {
 90                 #region 显示新建 保存 打印 复制、剪切、粘贴
 91                 NToolStripButton.Visible = ShowNew;
 92                 SToolStripButton.Visible = ShowSave;
 93                 PRToolStripButton.Visible = ShowPrint;
 94                 PPToolStripButton.Visible = ShowPrint;
 95                 UToolStripButton.Visible = ShowCut;
 96                 CToolStripButton.Visible = ShowCopy;
 97                 PToolStripButton.Visible = ShowPaste;
 98                 #endregion
 99 
100                 #region 显示字体设置
101                 toolStripBtn_B.Visible = ShowFontSet;
102                 toolStripBtn_I.Visible = ShowFontSet;
103                 toolStripBtn_U.Visible = ShowEffects;
104                 toolStripBtn_DeleteLine.Visible = ShowEffects;
105                 toolStripBtn_Color.Visible = ShowEffects;
106                 toolStripBtn_BackColor.Visible = ShowBackColor;
107                 toolStripCbx_FontName.Visible = ShowFontSet;
108                 toolStripCbx_FontSize.Visible = ShowFontSet;
109                 toolStripBtn_FontSet.Visible = ShowFontSet;
110                 #endregion
111 
112                 #region 显示重做、拆捎
113                 toolStripBtn_Undo.Visible = ShowUndo;
114                 toolStripBtn_Redo.Visible = ShowRedo;
115                 #endregion
116                 fontSelect = new FontSelect();
117                 //加载字体
118                 System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
119                 int intSontTi = 0;
120                 foreach (FontFamily item in fonts.Families)
121                 {
122                     this.toolStripCbx_FontName.Items.Add(item.Name);
123                 }
124                 intSontTi = this.toolStripCbx_FontName.Items.IndexOf("宋体");
125                 this.toolStripCbx_FontName.SelectedIndex = intSontTi;
126                 this.toolStripCbx_FontSize.SelectedIndex = this.toolStripCbx_FontSize.Items.IndexOf("五号");
127                 fontSelect.FontName = "宋体";//默认选择的是宋体
128                 fontSelect.FontSize = fontSelect.GetFontSize("五号");
129                 fontSelect.Blod = toolStripBtn_B.Checked;
130                 fontSelect.Italic = toolStripBtn_I.Checked;
131                 fontSelect.Underline = toolStripBtn_U.Checked;
132                 fontSelect.Deleteline = toolStripBtn_DeleteLine.Checked;
133                 fontSelect.FontColor = Color.Black;
134                 fontSelect.BackColor = SystemColors.Window;
135             }
136         }
137 
138         #endregion
139 
140         #region 新建 打开 保存 打印 剪切、复制、粘贴
141         /// <summary>
142         /// 新建
143         /// </summary>
144         /// <param name="sender"></param>
145         /// <param name="e"></param>
146         private void NToolStripButton_Click(object sender, EventArgs e)
147         {
148             LoadDefaultSet();
149             this.richTxt.Text = "";
150         }
151         /// <summary>
152         /// 打开
153         /// </summary>
154         /// <param name="sender"></param>
155         /// <param name="e"></param>
156         private void OToolStripButton_Click(object sender, EventArgs e)
157         {
158             OpenFileDialog ofd = new OpenFileDialog();
159             ofd.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|DOC文件|*.doc|DOCX文件|*.docx|所有文件|*.*";
160             if (ofd.ShowDialog()==DialogResult.OK)
161             {
162                 try
163                 {
164                     string fileName = ofd.FileName;
165                     stringFileName = GetFileName(fileName,false);//提取文件名出来,用于后面打印
166                     FileInfo fInfo = new FileInfo(fileName);
167                     if (!fInfo.Exists)//如果文件不存在,提示
168                     {
169                         throw new Exception("文件不存在!");
170                     }
171                     //文件扩展名
172                     string fileExt = fInfo.Extension;
173                     switch (fileExt.ToLower())
174                     {
175                         case ".txt":
176                             richTxt.LoadFile(fileName, RichTextBoxStreamType.PlainText);
177                             break;
178                         case ".rtf":
179                             richTxt.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText);
180                             break;
181                         case ".doc":
182                         case ".docx":
183                             richTxt.Rtf = OpenDocAndTxtFile(ofd);
184                             //OpenDocAndTxtFile(ofd);
185                             //richTxt.Paste();
186                             break;
187                         default:
188                             throw new Exception("打开文件失败!");
189                     }
190                 }
191                 catch (Exception ex) { MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);}
192             }
193         }
194         /// <summary>
195         /// 返回文件名,boolExt确认是否包含路径
196         /// </summary>
197         /// <param name="strFileName">包含路径的文件名</param>
198         /// <param name="boolExt">是否包含扩展名</param>
199         /// <returns></returns>
200         public string GetFileName(string strFileName,bool boolExt)
201         {
202             string strResult = string.Empty;
203             //开始位置
204             int intStart = strFileName.LastIndexOf('\\');
205             //结束位置
206             int intEnd = strFileName.IndexOf('.');
207             strResult = !boolExt?strFileName.Substring(intStart + 1):
208                 strFileName.Substring(intStart+1,intEnd-intStart-1);
209             return strResult;
210         }
211         /// <summary>
212         /// 打开文件文件及doc文件
213         /// </summary>
214         /// <param name="ofd"></param>
215         /// <returns></returns>
216         private string OpenDocAndTxtFile(OpenFileDialog ofd)
217         {
218             string strResult = string.Empty;
219             Clipboard.Clear();//清空剪贴板内容
220             Word.Application wordApp = new Word.Application();         
221             Word.Document doc = null;
222             object missing = Missing.Value;
223             object fileName = ofd.FileName;
224             object readOnly = true;
225             object isVisible = false;
226             object index = 0;
227             try
228             {
229                 doc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
230                 doc.ActiveWindow.Selection.WholeStory();
231                 doc.ActiveWindow.Selection.Copy();
232                 //richTxt.Paste();
233                 IDataObject data = Clipboard.GetDataObject();
234                 strResult = data.GetData(DataFormats.Rtf).ToString();  
235             }
236             catch(Exception ex)
237             {
238                 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
239             }finally
240             {
241                 if (doc != null)
242                 {
243                     doc.Close(ref missing, ref missing, ref missing);
244                     doc = null;
245                 }
246                 if (wordApp != null)
247                 {
248                     wordApp.Quit(ref missing, ref missing, ref missing);
249                     wordApp = null;
250                 }
251             }
252             return strResult;
253         }
254         /// <summary>
255         /// 保存
256         /// </summary>
257         /// <param name="sender"></param>
258         /// <param name="e"></param>
259         private void SToolStripButton_Click(object sender, EventArgs e)
260         {
261             if (richTxt.TextLength <= 0)
262             {
263                 MessageBox.Show("没有任何内容!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
264                 return;
265             }
266             SaveFileDialog sfd = new SaveFileDialog();
267             sfd.Filter = "文本文件|*.txt|RTF文件|*.rtf|DOC文件|*.doc|DOCX文件|*.docx|所有文件|*.*";
268             sfd.FilterIndex = 1;
269             sfd.CheckPathExists = true;
270             if (sfd.ShowDialog()==DialogResult.OK)
271             {
272                 try
273                 {
274                     string fileName = sfd.FileName;
275                     stringFileName = GetFileName(fileName,false);
276                     FileInfo fInfo = new FileInfo(fileName);
277                     if (fInfo.Exists)//如果文件存在,提示
278                     {
279                         if (MessageBox.Show("文件已经存在,是否覆盖?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.Cancel)
280                         {
281                             return;
282                         }
283                         else
284                         {//删除文件
285                             fInfo.Delete();
286                         }
287                     }
288                     //文件扩展名
289                     string fileExt = fInfo.Extension;
290                     switch (fileExt.ToLower())
291                     {
292                         case ".txt":
293                             richTxt.SaveFile(fileName, RichTextBoxStreamType.PlainText);
294                             break;
295                         case ".rtf":
296                         case ".doc":
297                         case ".docx":
298                             richTxt.SaveFile(fileName);
299                             break;
300                         default:
301                             throw new Exception("保存文件失败!");
302                     }
303                 }
304                 catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
305             }
306             if (File.Exists(sfd.FileName))
307             {
308                 MessageBox.Show("保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
309             }
310         }
311 
312         #region 打印
313         /// <summary>
314         /// 直接打印
315         /// </summary>
316         /// <param name="sender"></param>
317         /// <param name="e"></param>
318         private void PRToolStripButton_Click(object sender, EventArgs e)
319         {
320             if (richTxt.TextLength<=0)
321             {
322                 MessageBox.Show("没有任何内容!","打印",MessageBoxButtons.OK,MessageBoxIcon.Information);
323                 return;
324             }
325             if (printDialog.ShowDialog()==DialogResult.OK)
326             {
327                 printDoc.DocumentName = stringFileName;
328                 printDoc.PrinterSettings = printDialog.PrinterSettings;
329                 printDoc.Print();
330             }
331         }
332         /// <summary>
333         /// 打印预览
334         /// </summary>
335         /// <param name="sender"></param>
336         /// <param name="e"></param>
337         private void toolStripBtn_PrintPreview_Click(object sender, EventArgs e)
338         {
339             if (richTxt.TextLength <= 0)
340             {
341                 MessageBox.Show("没有任何内容!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Information);
342                 return;
343             }
344             if (printPreviewDialog.ShowDialog() == DialogResult.OK)
345             {
346                 printDoc.DocumentName = stringFileName;
347                 printDoc.Print();
348             }
349         }
350         private void printDoc_BeginPrint(object sender, PrintEventArgs e)
351         {
352             checkPrint = 0;
353         }
354 
355         private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
356         {
357             // Print the content of RichTextBox. Store the last character printed.
358             checkPrint = richTxt.Print(checkPrint, richTxt.TextLength, e);
359 
360             // Check for more pages
361             if (checkPrint < richTxt.TextLength)
362                 e.HasMorePages = true;
363             else
364                 e.HasMorePages = false;
365         }
366         #endregion 
367 
368         /// <summary>
369         /// 剪切
370         /// </summary>
371         /// <param name="sender"></param>
372         /// <param name="e"></param>
373         private void UToolStripButton_Click(object sender, EventArgs e)
374         {
375             //Clipboard.SetText(this.richTxt.SelectedRtf);
376             //this.richTxt.SelectedText = "";
377             richTxt.Cut();
378         }
379         /// <summary>
380         /// 复制
381         /// </summary>
382         /// <param name="sender"></param>
383         /// <param name="e"></param>
384         private void CToolStripButton_Click(object sender, EventArgs e)
385         {
386             //Clipboard.Clear();
387             //Clipboard.SetText(this.richTxt.SelectedRtf);
388             richTxt.Copy();
389         }
390         /// <summary>
391         /// 粘贴
392         /// </summary>
393         /// <param name="sender"></param>
394         /// <param name="e"></param>
395         private void PToolStripButton_Click(object sender, EventArgs e)
396         {
397             //if (this.richTxt.SelectionLength>0)
398             //{
399             //    this.richTxt.SelectionStart = this.richTxt.SelectionStart + this.richTxt.SelectionLength;
400             //}
401             //this.richTxt.SelectedRtf = Clipboard.GetText();
402             richTxt.Paste();
403         }
404         #endregion 
405 
406         #region 状态栏
407         private void richTxt_MouseClick(object sender, MouseEventArgs e)
408         {
409             try
410             {
411                 //如果要显示状态栏
412                 if (showStatusToolbar)
413                 {
414                     ShowLinesAndColumnStatus();
415                 }
416                 //如果不显示基础工具栏
417                 if (!ShowBasicToolbar)
418                 {
419                     return;
420                 }
421                 //设置工具栏font属性
422                 //当没有文本时返回
423                 if (richTxt.Text.Length <= 0)
424                 {
425                     return;
426                 }
427                 //当有选中多个文字时,返回不更新字体设置
428                 if (richTxt.SelectedText.Length >1)
429                 {
430                     toolStripBtn_B.Checked = false;
431                     toolStripBtn_I.Checked = false;
432                     toolStripBtn_U.Checked = false;
433                     toolStripBtn_DeleteLine.Checked = false;
434                     return;
435                 }
436                 //如果当前位置后面还有字符,则更新当前BIUS
437                 if (richTxt.SelectionStart != richTxt.TextLength)
438                 {
439                     //获取当前位置字体样式
440                     Font fontNow = richTxt.SelectionFont;
441                     toolStripBtn_B.Checked = fontNow.Bold;
442                     toolStripBtn_I.Checked = fontNow.Italic;
443                     toolStripBtn_U.Checked = fontNow.Underline;
444                     toolStripBtn_DeleteLine.Checked = fontNow.Strikeout;
445                     toolStripCbx_FontName.Text = fontNow.FontFamily.Name;
446                     toolStripCbx_FontSize.Text = fontSelect.GetFontStringSize(fontNow.Size.ToString());
447                     toolStripBtn_Color.ForeColor = richTxt.SelectionColor;
448                     toolStripBtn_BackColor.BackColor = richTxt.SelectionBackColor;
449 
450                     //设置按钮的背景颜色
451                     //toolStripBtn_B.BackColor = toolStripBtn_B.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
452                     //toolStripBtn_I.BackColor=toolStripBtn_I.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
453                     //toolStripBtn_U.BackColor=toolStripBtn_U.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
454                     //toolStripBtn_DeleteLine.BackColor=toolStripBtn_DeleteLine.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
455                 }
456                 else
457                 {
458                     //设置当前位置字体样式
459                     SetFontBIUD();
460                 }
461 
462             }
463             catch (Exception ex) { MessageBox.Show(ex.Message); }
464         }
465         /// <summary>
466         /// 显示当前行列信息
467         /// </summary>
468         private void ShowLinesAndColumnStatus()
469         {
470             //得到光标的行号
471             int line = GetCurrentLine()+1;
472             int intLines = GetAllLines();
473             int column = GetCurrentColumn();
474             //this.richTxt.Paste(Clipboard.GetDataObject());
475             this.toolStripStatusLabel_Row.Text = string.Format("第{0}行{1}列,共{2}行", line.ToString(), column.ToString(), intLines.ToString());
476         }
477        
478         /// <summary>
479         /// richText的text有变化时
480         /// </summary>
481         /// <param name="sender"></param>
482         /// <param name="e"></param>
483         private void richTxt_TextChanged(object sender, EventArgs e)
484         {
485             ShowLinesAndColumnStatus();
486 
487         }
488 
489 
490         #endregion
491 
492         #region 字体
493 
494         /// <summary>
495         /// 加粗
496         /// </summary>
497         /// <param name="sender"></param>
498         /// <param name="e"></param>
499         private void toolStripBtn_B_Click(object sender, EventArgs e)
500         {
501             SetFontBIUD();
502             //fontSelect.Blod = (sender as ToolStripButton).Checked ? true : false;
503         }
504         /// <summary>
505         /// 设置字体的Blod,Italic,UnderLine,Strikeout,字体,大小
506         /// </summary>
507         private void SetFontBIUD()
508         {
509             #region 
510             if (toolStripBtn_B.Checked &&
511                 toolStripBtn_I.Checked &&
512                 toolStripBtn_U.Checked &&
513                 toolStripBtn_DeleteLine.Checked)//4
514             {
515                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
516                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
517                  FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout);
518             } else if (toolStripBtn_B.Checked &&
519                  toolStripBtn_I.Checked == false &&
520                 toolStripBtn_U.Checked &&
521                 toolStripBtn_DeleteLine.Checked)//3
522             {
523                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
524                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
525                  FontStyle.Bold | FontStyle.Underline | FontStyle.Strikeout);
526             }else if (toolStripBtn_B.Checked==false &&
527                 toolStripBtn_I.Checked &&
528                 toolStripBtn_U.Checked &&
529                 toolStripBtn_DeleteLine.Checked)//3
530             {
531                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
532                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
533                 FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout);
534             }else if (toolStripBtn_B.Checked &&
535                 toolStripBtn_I.Checked &&
536                 toolStripBtn_U.Checked==false &&
537                 toolStripBtn_DeleteLine.Checked)//3
538             {
539                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
540                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
541                 FontStyle.Bold | FontStyle.Italic  | FontStyle.Strikeout);
542             }else if (toolStripBtn_B.Checked &&
543                toolStripBtn_I.Checked &&
544                toolStripBtn_U.Checked &&
545                toolStripBtn_DeleteLine.Checked==false)//3
546             {
547                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
548                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
549                 FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
550             }else if (toolStripBtn_B.Checked &&
551                toolStripBtn_I.Checked &&
552                toolStripBtn_U.Checked==false &&
553                toolStripBtn_DeleteLine.Checked==false)//2-b
554             {
555                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
556                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
557                 FontStyle.Bold | FontStyle.Italic);
558             }else if (toolStripBtn_B.Checked &&
559                toolStripBtn_I.Checked ==false&&
560                toolStripBtn_U.Checked &&
561                toolStripBtn_DeleteLine.Checked==false)//2-b
562             {
563                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
564                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
565                 FontStyle.Bold | FontStyle.Underline);
566             }else if (toolStripBtn_B.Checked &&
567                toolStripBtn_I.Checked==false &&
568                toolStripBtn_U.Checked==false &&
569                toolStripBtn_DeleteLine.Checked)//2-b
570             {
571                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
572                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
573                 FontStyle.Bold  | FontStyle.Strikeout);
574             }else if (toolStripBtn_B.Checked==false &&
575                toolStripBtn_I.Checked &&
576                toolStripBtn_U.Checked &&
577                toolStripBtn_DeleteLine.Checked==false)//2-i
578             {
579                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
580                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
581                  FontStyle.Italic | FontStyle.Underline );
582             }else if (toolStripBtn_B.Checked==false &&
583                toolStripBtn_I.Checked &&
584                toolStripBtn_U.Checked==false &&
585                toolStripBtn_DeleteLine.Checked)//2-i
586             {
587                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
588                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
589                 FontStyle.Italic  | FontStyle.Strikeout);
590             }else if (toolStripBtn_B.Checked==false &&
591                toolStripBtn_I.Checked==false &&
592                toolStripBtn_U.Checked &&
593                toolStripBtn_DeleteLine.Checked)//2-u
594             {
595                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
596                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
597                 FontStyle.Underline | FontStyle.Strikeout);
598             }else if (toolStripBtn_B.Checked &&
599                toolStripBtn_I.Checked==false &&
600                toolStripBtn_U.Checked==false &&
601                toolStripBtn_DeleteLine.Checked==false)//1-b
602             {
603                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
604                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
605                 FontStyle.Bold );
606             }else if (toolStripBtn_B.Checked==false &&
607                toolStripBtn_I.Checked &&
608                toolStripBtn_U.Checked==false &&
609                toolStripBtn_DeleteLine.Checked==false)//1-i
610             {
611                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
612                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
613                 FontStyle.Italic );
614             }else if (toolStripBtn_B.Checked==false &&
615                toolStripBtn_I.Checked==false &&
616                toolStripBtn_U.Checked &&
617                toolStripBtn_DeleteLine.Checked==false)//1-u
618             {
619                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
620                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
621                 FontStyle.Underline );
622             }else if (toolStripBtn_B.Checked ==false&&
623                toolStripBtn_I.Checked ==false&&
624                toolStripBtn_U.Checked ==false&&
625                toolStripBtn_DeleteLine.Checked)//1-d
626             {
627                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
628                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
629                 FontStyle.Strikeout);
630             }else//都没有点击,FontSytel为普通文本
631             {
632                 richTxt.SelectionFont = new Font(toolStripCbx_FontName.Text,
633                 fontSelect.GetFontSize(this.toolStripCbx_FontSize.Text),
634                 FontStyle.Regular);
635             }
636             #endregion
637             //字体名称等信息更新到fontSelect中
638             fontSelect.FontName = toolStripCbx_FontName.Text;
639             fontSelect.FontSize = fontSelect.GetFontSize(toolStripCbx_FontSize.Text);
640             fontSelect.Blod = toolStripBtn_B.Checked;
641             fontSelect.Italic = toolStripBtn_I.Checked;
642             fontSelect.Underline = toolStripBtn_U.Checked;
643             fontSelect.Deleteline = toolStripBtn_DeleteLine.Checked;
644             //fontSelect.BackColor = toolStripBtn_BackColor.BackColor;
645 
646             //设置按钮的背景颜色
647             //toolStripBtn_B.BackColor = toolStripBtn_B.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
648             //toolStripBtn_I.BackColor = toolStripBtn_I.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
649             //toolStripBtn_U.BackColor = toolStripBtn_U.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
650             //toolStripBtn_DeleteLine.BackColor = toolStripBtn_DeleteLine.Checked ? Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))) : SystemColors.Control;
651 
652             richTxt.SelectionColor = fontSelect.FontColor;
653             richTxt.SelectionBackColor = fontSelect.BackColor;
654         }
655 
656         /// <summary>
657             /// 斜体
658             /// </summary>
659             /// <param name="sender"></param>
660             /// <param name="e"></param>
661         private void toolStripBtn_I_Click(object sender, EventArgs e)
662         {
663             SetFontBIUD();
664             //fontSelect.Italic = (sender as ToolStripButton).Checked ? true : false;
665             //richTxt.SelectionFont = new Font(fontSelect.FontName, 
666             //    fontSelect.GetFontSize(toolStripCbx_FontSize.Text),
667             //    fontSelect.Italic?FontStyle.Italic : FontStyle.Regular);
668         }
669         /// <summary>
670         /// 下划线
671         /// </summary>
672         /// <param name="sender"></param>
673         /// <param name="e"></param>
674         private void toolStripBtn_U_Click(object sender, EventArgs e)
675         {
676             SetFontBIUD();
677             //fontSelect.Underline = (sender as ToolStripButton).Checked ? true : false;
678             //richTxt.SelectionFont = new Font(fontSelect.FontName, 
679             //    fontSelect.GetFontSize(toolStripCbx_FontSize.Text),
680             //    fontSelect.Underline?FontStyle.Underline:FontStyle.Regular);
681         }
682         /// <summary>
683         /// 删除线
684         /// </summary>
685         /// <param name="sender"></param>
686         /// <param name="e"></param>
687         private void toolStripBtn_DeleteLine_Click(object sender, EventArgs e)
688         {
689             SetFontBIUD();
690             //fontSelect.Deleteline = (sender as ToolStripButton).Checked? true : false;
691             //richTxt.SelectionFont = new Font(fontSelect.FontName, 
692             //    fontSelect.GetFontSize(toolStripCbx_FontSize.Text),
693             //    fontSelect.Deleteline?FontStyle.Strikeout:FontStyle.Regular);
694         }
695         /// <summary>
696         /// 字体颜色
697         /// </summary>
698         /// <param name="sender"></param>
699         /// <param name="e"></param>
700         private void toolStripBtn_Color_Click(object sender, EventArgs e)
701         {
702             ColorDialog color = new ColorDialog();
703             color.Color = fontSelect.FontColor;
704             if (color.ShowDialog() == DialogResult.OK)
705             {
706                 this.toolStripBtn_Color.ForeColor = color.Color;
707                 fontSelect.FontColor = color.Color;
708                 richTxt.SelectionColor = fontSelect.FontColor;
709             }
710         }
711         /// <summary>
712         /// 设置当前位置或者选中文本的背景色
713         /// </summary>
714         /// <param name="sender"></param>
715         /// <param name="e"></param>
716         private void toolStripBtn_BackColor_Click(object sender, EventArgs e)
717         {
718             ColorDialog color = new ColorDialog();
719             color.Color = fontSelect.BackColor;
720             if (color.ShowDialog() == DialogResult.OK)
721             {
722                 this.toolStripBtn_BackColor.BackColor = color.Color;
723                 fontSelect.BackColor = color.Color;
724                 richTxt.SelectionBackColor = fontSelect.BackColor;
725             }
726         }
727         /// <summary>
728         /// 字体名称更改时,更新fontSelect
729         /// </summary>
730         /// <param name="sender"></param>
731         /// <param name="e"></param>
732         private void toolStripCbx_FontName_SelectedIndexChanged(object sender, EventArgs e)
733         {
734             //fontSelect.FontName = (sender as ToolStripComboBox).SelectedText;
735             SetFontBIUD();
736             //richTxt.SelectionFont = new Font(fontSelect.FontName, fontSelect.GetFontSize(toolStripCbx_FontSize.Text));
737         }
738         /// <summary>
739         /// 字体大小
740         /// </summary>
741         /// <param name="sender"></param>
742         /// <param name="e"></param>
743         private void toolStripCbx_FontSize_SelectedIndexChanged(object sender, EventArgs e)
744         {
745             //fontSelect.FontSize = fontSelect.GetFontSize((sender as ToolStripComboBox).SelectedText);
746             SetFontBIUD();
747             //richTxt.SelectionFont = new Font(fontSelect.FontName, fontSelect.GetFontSize(toolStripCbx_FontSize.Text));
748         }
749         /// <summary>
750         /// 字体大小
751         /// </summary>
752         /// <param name="sender"></param>
753         /// <param name="e"></param>
754         private void toolStripCbx_FontSize_TextChanged(object sender, EventArgs e)
755         {
756             //fontSelect.FontSize = fontSelect.GetFontSize((sender as ToolStripComboBox).SelectedText);
757             SetFontBIUD();
758             //richTxt.SelectionFont = new Font(fontSelect.FontName, fontSelect.GetFontSize(toolStripCbx_FontSize.Text));
759         }
760         /// <summary>
761         /// 字体选择窗口
762         /// </summary>
763         /// <param name="sender"></param>
764         /// <param name="e"></param>
765         private void toolStripBtn_FontSet_Click(object sender, EventArgs e)
766         {
767             try
768             {
769                 FontDialog font = new FontDialog();
770                 font.ShowColor = ShowEffects;
771                 font.ShowEffects = ShowEffects;
772                 if (font.ShowDialog() == DialogResult.OK)
773                 {
774                     //根据字体设置,设置状态栏上按钮
775                     this.toolStripBtn_B.Checked = font.Font.Bold ? true : false;
776                     this.toolStripBtn_I.Checked = font.Font.Italic ? true : false;
777                     this.toolStripBtn_U.Checked = font.Font.Underline ? true : false;
778                     this.toolStripBtn_DeleteLine.Checked = font.Font.Strikeout ? true : false;
779                     this.toolStripCbx_FontName.SelectedIndex = this.toolStripCbx_FontName.Items.IndexOf(font.Font.Name);
780                     this.toolStripCbx_FontSize.Text = fontSelect.GetFontStringSize(font.Font.Size.ToString());
781                     
782                     //设置fontSelect
783                     fontSelect.Blod = font.Font.Bold;
784                     fontSelect.Italic = font.Font.Italic;
785                     fontSelect.Underline = font.Font.Underline;
786                     fontSelect.Deleteline = font.Font.Strikeout;
787                     fontSelect.FontName = font.Font.Name;
788                     fontSelect.FontSize = font.Font.Size;
789                     fontSelect.FontColor = font.Color;
790 
791                     richTxt.SelectionColor = fontSelect.FontColor;
792                     SetFontBIUD();
793                 }
794             }catch  (Exception ex)
795             {
796                 MessageBox.Show(ex.Message);
797             }
798         }
799 
800         #endregion
801 
802         #region 拆销 重做
803         private void toolStripBtn_Undo_Click(object sender, EventArgs e)
804         {
805             richTxt.Undo();
806         }
807 
808         private void toolStripBtn_Redo_Click(object sender, EventArgs e)
809         {
810             richTxt.Redo();
811         }
812 
813         #endregion
814 
815         #region 快捷菜单 全选 选择当前行
816         /// <summary>
817         /// 全选
818         /// </summary>
819         /// <param name="sender"></param>
820         /// <param name="e"></param>
821         private void ToolStripMenuItem_SelectAll_Click(object sender, EventArgs e)
822         {
823             richTxt.SelectAll();
824         }
825         /// <summary>
826         /// 选当前行
827         /// </summary>
828         /// <param name="sender"></param>
829         /// <param name="e"></param>
830         private void ToolStripMenuItem_SelectCurrentLine_Click(object sender, EventArgs e)
831         {
832             SelectLine(GetCurrentLine());
833         }
834         
835 
836         #endregion
837 
838         #region RichTextBox 相关方法
839         /// <summary>
840         /// 获取当前所在列
841         /// </summary>
842         /// <returns></returns>
843         private int GetCurrentColumn()
844         {
845             //当前行索引
846             int index = GetCurrentRowIndex();
847             //得到列的索引  
848             /*SelectionStart得到光标所在位置的索引 
849                 再减去 
850                 当前行第一个字符的索引 
851                 = 光标所在的列数(从0开始)
852             */
853             return this.richTxt.SelectionStart - index + 1;
854         }
855         /// <summary>
856         /// 获取当前行索引
857         /// </summary>
858         /// <returns></returns>
859         private int GetCurrentRowIndex()
860         {
861             return this.richTxt.GetFirstCharIndexOfCurrentLine();
862         }
863         /// <summary>
864         /// 获取当前光标所在行,行号从0开始的
865         /// </summary>
866         /// <returns></returns>
867         private int GetCurrentLine()
868         {
869             //当前行索引
870             int index = this.richTxt.GetFirstCharIndexOfCurrentLine();
871             //得到光标的行号
872             int line = this.richTxt.GetLineFromCharIndex(index);
873             return line;
874         }
875         /// <summary>
876         /// 获取行数,共有多少行
877         /// </summary>
878         /// <returns></returns>
879         private int GetAllLines()
880         {
881             //共有行数
882             return  this.richTxt.GetLineFromCharIndex(this.richTxt.TextLength) + 1;
883         }
884 
885         /// <summary>
886         /// 选择行
887         /// </summary>
888         /// <param name="line"></param>
889         private void SelectLine(int line)
890         {
891 
892             int a = this.richTxt.GetFirstCharIndexFromLine(line);
893             int b = this.richTxt.GetFirstCharIndexFromLine(++line);
894             if (a == -1)
895                 return;
896             else if (b == -1)
897                 b = this.richTxt.TextLength - a;
898             else
899                 b = b - a;
900             this.richTxt.Select(a, b);
901         }
902 
903         /// <summary>
904         /// 获取RichTextBox内容为byte[]类型
905         /// </summary>
906         /// <param name="txt">RichTextBox控件名称</param>
907         /// <returns></returns>
908         public byte[] GetRichTextBoxData(RichTextBox txt)
909         {
910             byte[] bs = null;
911             using (MemoryStream ms = new MemoryStream())
912             {
913                 txt.SaveFile(ms, RichTextBoxStreamType.RichText);
914                 bs = ms.ToArray();
915             }
916             using (MemoryStream ms = new MemoryStream())
917             {
918                 using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress))
919                 {
920                     zip.Write(bs, 0, bs.Length);
921                 }
922                 return ms.ToArray();
923             }
924         }
925         /// <summary>
926         /// 读取byte[]数据到RichTextBox控件
927         /// </summary>
928         /// <param name="txt">RichTextBox控件</param>
929         /// <param name="data">byte[]类型的数据</param>
930         public void LoadRichTextBoxData(RichTextBox txt, byte[] data)
931         {
932             using (MemoryStream ms = new MemoryStream(data))
933             {
934                 using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
935                 {
936                     using (MemoryStream ms2 = new MemoryStream())
937                     {
938                         int b = -1;
939                         while ((b = zip.ReadByte()) != -1)
940                         {
941                             ms2.WriteByte((byte)b);
942                         }
943                         ms2.Position = 0;
944                         txt.LoadFile(ms2, RichTextBoxStreamType.RichText);
945                     }
946                 }
947             }
948         }
949 
950         #endregion
951         
952     }
953 }
View Code

RichTextBoxPrintCtrl ,来自软件官网代码,实现打印功能

 1 namespace RichTextControl
 2 {
 3     public class RichTextBoxPrintCtrl : RichTextBox
 4     {
 5         //Convert the unit used by the .NET framework (1/100 inch) 
 6         //and the unit used by Win32 API calls (twips 1/1440 inch)
 7         private const double anInch = 14.4;
 8 
 9         [StructLayout(LayoutKind.Sequential)]
10         private struct RECT
11         {
12             public int Left;
13             public int Top;
14             public int Right;
15             public int Bottom;
16         }
17 
18         [StructLayout(LayoutKind.Sequential)]
19         private struct CHARRANGE
20         {
21             public int cpMin;         //First character of range (0 for start of doc)
22             public int cpMax;           //Last character of range (-1 for end of doc)
23         }
24 
25         [StructLayout(LayoutKind.Sequential)]
26         private struct FORMATRANGE
27         {
28             public IntPtr hdc;             //Actual DC to draw on
29             public IntPtr hdcTarget;       //Target DC for determining text formatting
30             public RECT rc;                //Region of the DC to draw to (in twips)
31             public RECT rcPage;            //Region of the whole DC (page size) (in twips)
32             public CHARRANGE chrg;         //Range of text to draw (see earlier declaration)
33         }
34 
35         private const int WM_USER = 0x0400;
36         private const int EM_FORMATRANGE = WM_USER + 57;
37 
38         [DllImport("USER32.dll")]
39         private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
40 
41         // Render the contents of the RichTextBox for printing
42         //Return the last character printed + 1 (printing start from this point for next page)
43         public int Print(int charFrom, int charTo, PrintPageEventArgs e)
44         {
45             //Calculate the area to render and print
46             RECT rectToPrint;
47             rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
48             rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
49             rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
50             rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
51 
52             //Calculate the size of the page
53             RECT rectPage;
54             rectPage.Top = (int)(e.PageBounds.Top * anInch);
55             rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
56             rectPage.Left = (int)(e.PageBounds.Left * anInch);
57             rectPage.Right = (int)(e.PageBounds.Right * anInch);
58 
59             IntPtr hdc = e.Graphics.GetHdc();
60 
61             FORMATRANGE fmtRange;
62             fmtRange.chrg.cpMax = charTo;//Indicate character from to character to 
63             fmtRange.chrg.cpMin = charFrom;
64             fmtRange.hdc = hdc;                    //Use the same DC for measuring and rendering
65             fmtRange.hdcTarget = hdc;              //Point at printer hDC
66             fmtRange.rc = rectToPrint;             //Indicate the area on page to print
67             fmtRange.rcPage = rectPage;            //Indicate size of page
68 
69             IntPtr res = IntPtr.Zero;
70 
71             IntPtr wparam = IntPtr.Zero;
72             wparam = new IntPtr(1);
73 
74             //Get the pointer to the FORMATRANGE structure in memory
75             IntPtr lparam = IntPtr.Zero;
76             lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
77             Marshal.StructureToPtr(fmtRange, lparam, false);
78 
79             //Send the rendered data for printing 
80             res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);
81 
82             //Free the block of memory allocated
83             Marshal.FreeCoTaskMem(lparam);
84 
85             //Release the device context handle obtained by a previous call
86             e.Graphics.ReleaseHdc(hdc);
87 
88             //Return last + 1 character printer
89             return res.ToInt32();
90         }
91 
92     }
93 }
View Code
  1 namespace RichTextControl
  2 {
  3     /// <summary>
  4     /// 字体选择实体,保存选择的字体样式
  5     /// </summary>
  6     public class FontSelect
  7     {
  8         private bool blod;
  9         private bool italic;
 10         private bool underline;
 11         private bool deleteline;
 12         private Color fontColor;
 13         private string fontName;
 14         private float fontSize;
 15         private Color backColor;
 16         /// <summary>
 17         /// 粗体
 18         /// </summary>
 19         public bool Blod { get => blod; set => blod = value; }
 20         /// <summary>
 21         /// 斜体
 22         /// </summary>
 23         public bool Italic { get => italic; set => italic = value; }
 24         /// <summary>
 25         /// 下划线
 26         /// </summary>
 27         public bool Underline { get => underline; set => underline = value; }
 28         /// <summary>
 29         /// 删除线
 30         /// </summary>
 31         public bool Deleteline { get => deleteline; set => deleteline = value; }
 32         /// <summary>
 33         /// 字体颜色
 34         /// </summary>
 35         public Color FontColor { get => fontColor; set => fontColor = value; }
 36         /// <summary>
 37         /// 字体名称
 38         /// </summary>
 39         public string FontName { get => fontName; set => fontName = value; }
 40         /// <summary>
 41         /// 字体大小
 42         /// </summary>
 43         public float FontSize { get => fontSize; set => fontSize = value; }
 44         /// <summary>
 45         /// 字体背景颜色
 46         /// </summary>
 47         public Color BackColor { get => backColor; set => backColor = value; }
 48         public FontSelect() { }
 49         /// <summary>
 50         /// 获取字体大小
 51         /// </summary>
 52         /// <param name="strFontSize"></param>
 53         /// <returns></returns>
 54         public float GetFontSize(string strFontSize)
 55         {
 56             float floatSelectFontSize = 0.0f;
 57             try
 58             {
 59                 if (string.IsNullOrWhiteSpace(strFontSize))
 60                 {
 61                     floatSelectFontSize = 10.5f;
 62                 }
 63                 else
 64                 {
 65                     #region 初始华
 66                     Dictionary<string, float> dicFontSizes = new Dictionary<string, float>();
 67                     dicFontSizes.Add("八号", 5);
 68                     dicFontSizes.Add("七号", 5.5f);
 69                     dicFontSizes.Add("小六", 6.5f);
 70                     dicFontSizes.Add("六号", 7.5f);
 71                     dicFontSizes.Add("小五", 9);
 72                     dicFontSizes.Add("五号", 10.5f);
 73                     dicFontSizes.Add("小四", 12);
 74                     dicFontSizes.Add("四号", 14);
 75                     dicFontSizes.Add("小三", 15);
 76                     dicFontSizes.Add("三号", 16);
 77                     dicFontSizes.Add("小二", 18);
 78                     dicFontSizes.Add("二号", 22);
 79                     dicFontSizes.Add("小一", 24);
 80                     dicFontSizes.Add("一号", 26);
 81                     dicFontSizes.Add("小初", 36);
 82                     dicFontSizes.Add("初号", 42);
 83                     #endregion
 84                     //如果字典里有,则返回字典里的
 85                     if (dicFontSizes.Keys.Contains(strFontSize))
 86                     {
 87                         floatSelectFontSize = dicFontSizes[strFontSize];
 88                     }
 89                     else
 90                     {//字典里没有就返回数值
 91                         float.TryParse(strFontSize, out floatSelectFontSize);
 92                     }
 93                 }
 94             }
 95             catch (Exception ex) { System.Windows.Forms.MessageBox.Show("字体大小错误!\n" + ex.Message); }
 96             return floatSelectFontSize;
 97         }
 98         /// <summary>
 99         /// 获取字体大小,返回string类型
100         /// </summary>
101         /// <param name="strFontSize"></param>
102         /// <returns></returns>
103         public string GetFontStringSize(string strFontSize)
104         {
105             string strSelectFontSize = "";
106             try
107             {
108                 if (string.IsNullOrWhiteSpace(strFontSize))
109                 {
110                     strSelectFontSize = "五号";
111                 }
112                 else
113                 {
114                     #region 初始华
115                     Dictionary<string, float> dicFontSizes = new Dictionary<string, float>();
116                     dicFontSizes.Add("八号", 5);
117                     dicFontSizes.Add("七号", 5.5f);
118                     dicFontSizes.Add("小六", 6.5f);
119                     dicFontSizes.Add("六号", 7.5f);
120                     dicFontSizes.Add("小五", 9);
121                     dicFontSizes.Add("五号", 10.5f);
122                     dicFontSizes.Add("小四", 12);
123                     dicFontSizes.Add("四号", 14);
124                     dicFontSizes.Add("小三", 15);
125                     dicFontSizes.Add("三号", 16);
126                     dicFontSizes.Add("小二", 18);
127                     dicFontSizes.Add("二号", 22);
128                     dicFontSizes.Add("小一", 24);
129                     dicFontSizes.Add("一号", 26);
130                     dicFontSizes.Add("小初", 36);
131                     dicFontSizes.Add("初号", 42);
132                     #endregion
133                     bool boolFind = false;
134                     //如果字典里有,则返回字典里的
135                     foreach (var item in dicFontSizes)
136                     {
137                         if(item.Value == float.Parse(strFontSize))
138                         { strSelectFontSize = item.Key; boolFind = true; }
139                     }
140                     //字典里有就返回数值,没有原样返回
141                     strSelectFontSize = boolFind?strSelectFontSize: strFontSize;
142                     
143                 }
144             }
145             catch (Exception ex) { System.Windows.Forms.MessageBox.Show("字体大小错误!\n" + ex.Message); }
146             return strSelectFontSize;
147         }
148     }
149 }
FontSelect

 

转载于:https://www.cnblogs.com/baibanr/p/9474474.html

相关文章:

  • 深入剖析Retrofit系列(一)来自官方的Retrofit开发手册(中英互译)
  • java编程——高并发大容量NoSQL解决方案探索
  • Android 模拟器下载、编译及调试
  • [译]常见网页设计错误一览
  • 面试官告诉你如何准备Java初级和高级的技术面试
  • Apache Pulsar 2.1 重磅发布
  • java监听器实现与原理
  • Jenkisn行RFS脚本问题汇总
  • 【模板】最小生成树
  • angular1.x 性能优化
  • sublime打开文本时会记忆上次关闭时鼠标停留的位置
  • 模板汇总——AC自动机
  • vue keep-alive组件
  • SQL Server CONVERT() 函数
  • JS一元操作符递增与递减
  • 【译】JS基础算法脚本:字符串结尾
  • 〔开发系列〕一次关于小程序开发的深度总结
  • 3.7、@ResponseBody 和 @RestController
  • CentOS6 编译安装 redis-3.2.3
  • CSS3 变换
  • es的写入过程
  • Facebook AccountKit 接入的坑点
  • Github访问慢解决办法
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • Java多态
  • JS+CSS实现数字滚动
  • LeetCode算法系列_0891_子序列宽度之和
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • OSS Web直传 (文件图片)
  • PhantomJS 安装
  • React-redux的原理以及使用
  • React-生命周期杂记
  • storm drpc实例
  • TCP拥塞控制
  • Vue UI框架库开发介绍
  • 阿里研究院入选中国企业智库系统影响力榜
  • 测试如何在敏捷团队中工作?
  • 订阅Forge Viewer所有的事件
  • 记一次删除Git记录中的大文件的过程
  • 理解在java “”i=i++;”所发生的事情
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 微信开放平台全网发布【失败】的几点排查方法
  • 系统认识JavaScript正则表达式
  • 一个完整Java Web项目背后的密码
  • const的用法,特别是用在函数前面与后面的区别
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • ​iOS安全加固方法及实现
  • ​MySQL主从复制一致性检测
  • ​比特币大跌的 2 个原因
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #微信小程序:微信小程序常见的配置传值
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (27)4.8 习题课