forked from sunminghong/MEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMain.cs
715 lines (577 loc) · 19 KB
/
frmMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.AvalonEdit;
using MEditor.Processers;
using MRU;
namespace MEditor
{
public partial class frmMain : Form
{
//private PrintDocument printDocument = new PrintDocument();
private MRUManager mruManager=null;
public frmMain()
{
InitializeComponent();
}
private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
meditorManager.Open("");
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.Open("");
}
private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
openfile();
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openfile();
}
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
meditorManager.Save();
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.Save();
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.SaveAs();
}
private void 剪切UToolStripButton_Click(object sender, EventArgs e)
{
if (HaveSelection())
meditorManager.GetTextBox().Cut();
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
if (HaveSelection())
meditorManager.GetTextBox().Cut();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (HaveSelection())
meditorManager.GetTextBox().Cut();
}
private void 复制CToolStripButton_Click(object sender, EventArgs e)
{
if (HaveSelection())
meditorManager.GetTextBox().Copy();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
if (HaveSelection())
meditorManager.GetTextBox().Copy();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (HaveSelection())
meditorManager.GetTextBox().Copy();
}
private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Paste();
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Paste();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Paste();
}
private void 撤销toolStripButton5_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Undo();
}
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Undo();
}
private void 撤销ToolStripMenuItem2_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Undo();
}
private void 重做toolStripButton6_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Redo();
}
private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().Redo();
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().SelectAll();
}
private void 全选AToolStripMenuItem1_Click(object sender, EventArgs e)
{
meditorManager.GetTextBox().SelectAll();
}
private void 时间日期F5ToolStripMenuItem_Click(object sender, EventArgs e)
{
TextEditor rtb = meditorManager.GetTextBox();
string dat=System.DateTime.Now.ToString();
rtb.Document.Insert(rtb.CaretOffset,dat);
}
private bool HaveSelection()
{
var editor = meditorManager.GetTextBox();
return editor != null &&
editor.SelectionLength>0;
}
private void 字体toolStripButton10_Click(object sender, EventArgs e)
{
SelectFont();
}
private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
SelectForeColor();
}
private void 字体颜色toolStripButton11_Click(object sender, EventArgs e)
{
SelectForeColor();
}
#region 格式设计辅助
private void 加粗toolStripButton7_Click(object sender, EventArgs e)
{
ProcesserFactory.Processe(meditorManager.GetTextBox(), EMark.bold);
PreviewHtml();
}
private void 倾斜toolStripButton8_Click(object sender, EventArgs e)
{
// Font newFont = new Font(meditorManager.GetTextBox().SelectionFont, meditorManager.GetTextBox().SelectionFont.Italic ?
// meditorManager.GetTextBox().SelectionFont.Style & ~FontStyle.Italic : meditorManager.GetTextBox().SelectionFont.Style | FontStyle.Italic);
// meditorManager.GetTextBox().SelectionFont = newFont;
}
private void 下划线toolStripButton9_Click(object sender, EventArgs e)
{
// Font newFont = new Font(meditorManager.GetTextBox().SelectionFont, meditorManager.GetTextBox().SelectionFont.Underline ?
// meditorManager.GetTextBox().SelectionFont.Style & ~FontStyle.Underline : meditorManager.GetTextBox().SelectionFont.Style | FontStyle.Underline);
// meditorManager.GetTextBox().SelectionFont = newFont;
}
private void 左对齐toolStripButton3_Click(object sender, EventArgs e)
{
// meditorManager.GetTextBox().SelectionBullet = true;
//
//
// meditorManager.GetTextBox().SelectionIndent = 0;
// meditorManager.GetTextBox().SelectionHangingIndent = 0;
// meditorManager.GetTextBox().SelectionRightIndent = 0;
}
private void 右对齐toolStripButton1_Click(object sender, EventArgs e)
{
// meditorManager.GetTextBox().SelectionIndent = 0;
// meditorManager.GetTextBox().SelectionHangingIndent = 0;
// meditorManager.GetTextBox().SelectionRightIndent =0;
}
private void 居中toolStripButton2_Click(object sender, EventArgs e)
{
// meditorManager.GetTextBox().SelectionIndent = 8;
// meditorManager.GetTextBox().SelectionHangingIndent = 3;
// meditorManager.GetTextBox().SelectionRightIndent = 12;
}
private void 两端对齐toolStripButton4_Click(object sender, EventArgs e)
{
// meditorManager.GetTextBox().SelectionIndent = 0;
// meditorManager.GetTextBox().SelectionHangingIndent = 3;
// meditorManager.GetTextBox().SelectionRightIndent = 0;
}
#endregion
#region 打印控制还没有开放
////打印控制开始
//void printDocument_BeginPrint(object sender, PrintEventArgs e)
//{
// //设置打印文本
// text = meditorManager.GetTextBox().Text;
//}
//void printDocument_PrintPage(object sender, PrintPageEventArgs e)
//{
// //用DrawString方法输出字符串,在参数中可以设置打印的字体大小和颜色,此处设置为绿色
// e.Graphics.DrawString(text, new Font("Arial", 10), Brushes.Green, 50, 50);
//}
//private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
//{
// //关联打印预处理代码
// printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
// //关联打印代码
// printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
// //定义打印对话框
// PrintDialog printDialog = new PrintDialog();
// //获得用户输入
// DialogResult dr = printDialog.ShowDialog();
// //若确认则打印
// if (dr == DialogResult.OK)
// {
// printDocument.Print();
// }
// else
// {
// return;
// }
//}
//private void 打印PToolStripButton_Click(object sender, EventArgs e)
//{
// //关联打印预处理代码
// printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
// //关联打印代码
// printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
// //定义打印对话框
// PrintDialog printDialog = new PrintDialog();
// //获得用户输入
// DialogResult dr = printDialog.ShowDialog();
// //若确认则打印
// if (dr == DialogResult.OK)
// {
// printDocument.Print();
// }
// else
// {
// return;
// }
//}
////打印控制结束
#endregion
private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
{
PreviewHtml();
////页面设置对话框
//PageSetupDialog psd = new PageSetupDialog();
////定义所设置的文档
//psd.Document = printDocument;
////页面设置
//psd.PageSettings = new PageSettings();
////打印机设置
//psd.ShowNetwork = false;
////设置对话框
//DialogResult dr = psd.ShowDialog();
////确认时设置打印文档
//if (dr == DialogResult.OK)
//{
// printDocument.PrinterSettings = psd.PrinterSettings;
// printDocument.DefaultPageSettings = psd.PageSettings;
//}
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
//先保存
meditorManager.SaveAll();
//关闭程序
Close();
}
private void frmMain_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
public void frmMain_DragDrop(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string []data=(string [])e.Data.GetData(DataFormats.FileDrop);
openfiles(data);//, RichTextBoxStreamType.PlainText);
}
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.Close();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
PreviewHtml();
}
private void 关闭所有ToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.CloseAll();
}
private void 保存所有ToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.SaveAll();
}
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
TabSelectRec.Rec(e.TabPageIndex);
PreviewHtml();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
meditorManager.SaveAll();
}
#region 无标题栏移动窗口代码
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "ReleaseCapture")]
private static extern int ReleaseCapture();
private void toolStrip1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle.ToInt32(), 0x0112, 0xF012, 0);
}
}
#endregion
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.Save();
}
private void 关闭ToolStripMenuItem1_Click(object sender, EventArgs e)
{
meditorManager.Close();
}
private void 打开所在文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
{
meditorManager.openDir();
}
private void markdown格式ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 自动转行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动转行ToolStripMenuItem.Checked)
meditorManager.SetWordWrap(true);
else
meditorManager.SetWordWrap(false);
}
private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
SelectBackColor();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
SelectBackColor();
}
private void 还原系统原设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
//meditorManager.SetOldStyle();
//meditorManager.SetStyle(rtbHtml);
}
private void frmMain_Load(object sender, EventArgs e)
{
this.Text = Application.ProductName + " V" + Application.ProductVersion;
//初始化最近打开的文件
mruManager = new MRUManager();
mruManager.Initialize(this,文件ToolStripMenuItem, 最近打开的文件ToolStripMenuItem, // Recent Files menu item
"Software\\YihuiStudio\\MEditor"); // Registry path to keep MRU list
mruManager.CurrentDir = "....."; // default is current directory
mruManager.MaxMRULength = 10; // default is 10
mruManager.MaxDisplayNameLength = 40;
//定义编辑管理器
meditorManager = new MarkdownEditorManager(this, tabControl1, mruManager,webBrowser1);
ReadCss();
//webBrowser1.Navigate("about:blank");
meditorManager.SetStyle(rtbHtml);
//webBrowser1.DocumentText = meditorManager.GetHTMLStyle("");
_filemonitor = new FileMonitor(fsw_Changed);
string command = Environment.CommandLine;//获取进程命令行参数
if (!string.IsNullOrEmpty(command))
{
string[] para = command.Split('\"');
if (para.Length > 2)
{
string pathC = para[2];//获取打开的文件的路径
if (pathC.Length > 3)
{
openfile(pathC);
}
else
{
meditorManager.Open("");
}
}
}
else
{
meditorManager.Open("");
}
//this.timer1.Start();
rtbHtml.EnableAutoDragDrop = false;
rtbHtml.AllowDrop = true;
// rtbHtml.KeyDown += rtbHtml_KeyDown;
rtbHtml.DragDrop += new DragEventHandler(frmMain_DragDrop);
rtbHtml.DragEnter += new DragEventHandler(rtbHtml_DragEnter);
tabControl1.MouseDown += new MouseEventHandler(tabControl1_MouseDown);
tabControl2.MouseDown+=new MouseEventHandler(tabControl1_MouseDown);
//tabControl1.GotFocus += new EventHandler(tabControl1_GotFocus);
}
void tabControl1_GotFocus(object sender, EventArgs e)
{
SendKeys.Send("{tab}");
}
void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TabControl tabc = (TabControl)sender;
for (int i = 0; i < tabc.TabCount; i++)
{
Rectangle recTab = tabc.GetTabRect(i);
if (recTab.Contains(e.X, e.Y))
{
tabc.SelectedIndex = i;
}
}
}
}
void rtbHtml_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void 界面布局左右互换ToolStripMenuItem_Click(object sender, EventArgs e)
{
SwithFlaout();
}
private void 关联md扩展名ToolStripMenuItem_Click(object sender, EventArgs e)
{
regExtFile();
}
private void 转到行ToolStripMenuItem_Click(object sender, EventArgs e)
{
MarkdownEditor me= meditorManager.GetCurrEditor();
if(me==null)return ;
if (me.GetTextBox().Document.LineCount > 0)
{
//Show the dialog
GoToLineDialog gtl = new GoToLineDialog(me.GetTextBox());
gtl.ShowDialog();
}
}
private void 转为大写ToolStripMenuItem_Click(object sender, EventArgs e)
{
MarkdownEditor me= meditorManager.GetCurrEditor();
if(me==null)return ;
me.GetTextBox().SelectedText=me.GetTextBox().SelectedText.ToUpper();
}
private void 转为小写ToolStripMenuItem_Click(object sender, EventArgs e)
{
MarkdownEditor me = meditorManager.GetCurrEditor();
if (me == null) return;
me.GetTextBox().SelectedText=me.GetTextBox().SelectedText.ToLower();
}
private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
MarkdownEditor me = meditorManager.GetCurrEditor();
if (me == null) return;
}
private void 查找替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
MarkdownEditor me = meditorManager.GetCurrEditor();
if (me == null) return;
}
private void splitContainer1_DoubleClick(object sender, EventArgs e)
{
if (isLeft)
{
splitContainer1.Panel2Collapsed = !splitContainer1.Panel2Collapsed;
}
else
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
}
}
private void 经典黑底白字ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 取消md文件关联ToolStripMenuItem_Click(object sender, EventArgs e)
{
UnRegExtfile();
}
#region MEditor links
private void markdown语法介绍ToolStripMenuItem_Click(object sender, EventArgs e)
{
//showSyntax("https://github.com/5d13cn/MEditor/blob/master/resoucesdocs/syntax.md");
showSyntax("http://www.cnblogs.com/yihuiso/archive/2011/04/13/markdown.html");
}
private void markdown语法介绍精简版ToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("http://www.cnblogs.com/yihuiso/archive/2011/04/13/minimarkdown.html");
}
private void markdown语法介绍二ToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("https://github.com/othree/markdown-syntax-zhtw/blob/master/syntax.md");
}
private void markdownSytnxToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("http://daringfireball.net/projects/markdown/syntax");
}
private void 关于MEditorToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("https://github.com/5d13cn/MEditor/blob/master/resoucesdocs/about.md");
}
private void mEditor快捷键ToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("https://github.com/5d13cn/MEditor/blob/master/resoucesdocs/shortcut.md");
}
private void mEditor网站ToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("https://github.com/5d13cn/MEditor");
}
private void 检查最新版ToolStripMenuItem_Click(object sender, EventArgs e)
{
showSyntax("https://github.com/5d13cn/MEditor/raw/master/lastver.txt");
}
#endregion
private void 字体OToolStripMenuItem_Click(object sender, EventArgs e)
{
SelectFont();
}
private void html预览样式设计ToolStripMenuItem_Click(object sender, EventArgs e)
{
editCss();
}
private void 通用选项ToolStripMenuItem_Click(object sender, EventArgs e)
{
editCss();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
bool isCancel=!meditorManager.CloseAll();
if (isCancel)
e.Cancel = true;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
string url = e.Url.ToString();
if (url == "about:blank") return;
if (url.StartsWith("http://") || url.StartsWith("https://"))
{
return;
}
//if (url.EndsWith(".md"))
//{
url = url.Remove(0, 6);
MarkdownEditor me=meditorManager.GetCurrEditor();
if(me==null) return ;
url=Path.Combine(Path.GetDirectoryName(me.FileName),url);
if (_regexExtMarkdowns.IsMatch(url) || _regexExtOthertext.IsMatch(url))
{
e.Cancel = true;
openfile(url);
}
//webBrowser1.Navigate(url);
//}
}
}
}