|
Posted on 2006-08-19 07:33 东人EP 阅读(599) 评论(0) 编辑 收藏 引用 所属分类: .NET
1
using
System;
2
using
System.IO;
3
using
System.Drawing;
4
using
System.Collections;
5
using
System.ComponentModel;
6
using
System.Windows.Forms;
7
using
System.Data;
8
9
namespace
HelloGUI
10
{
11
/**/
///
<summary>
12
///
Form1 的摘要说明。
13
///
</summary>
14
public
class
FormMain : System.Windows.Forms.Form
15
{
16
变量声明
#region
变量声明
17
18
//
声明文本框textBoxEdit
19
private
System.Windows.Forms.TextBox textBoxEdit;
20
21
//
声明主菜单mainMenu
22
private
System.Windows.Forms.MainMenu mainMenu;
23
24
//
声明mainMenu的各个MenuItem
25
private
System.Windows.Forms.MenuItem menuItemFile;
26
private
System.Windows.Forms.MenuItem menuItemEdit;
27
private
System.Windows.Forms.MenuItem menuItemFormat;
28
29
//
声明menuItemFile下的MenuItem
30
private
System.Windows.Forms.MenuItem menuItemNew;
31
private
System.Windows.Forms.MenuItem menuItemoOpen;
32
private
System.Windows.Forms.MenuItem menuItemSave;
33
private
System.Windows.Forms.MenuItem menuItemSaveAs;
34
private
System.Windows.Forms.MenuItem menuItemPageSet;
35
private
System.Windows.Forms.MenuItem menuItemprint;
36
private
System.Windows.Forms.MenuItem menuItemExit;
37
private
System.Windows.Forms.MenuItem menuItem1;
38
private
System.Windows.Forms.MenuItem menuItem2;
39
40
//
声明menuItemEdit下的MenuItem
41
private
System.Windows.Forms.MenuItem menuItemUndo;
42
private
System.Windows.Forms.MenuItem menuItem3;
43
private
System.Windows.Forms.MenuItem menuItemCut;
44
private
System.Windows.Forms.MenuItem menuItemCopy;
45
private
System.Windows.Forms.MenuItem menuItemPaste;
46
private
System.Windows.Forms.MenuItem menuItemDel;
47
private
System.Windows.Forms.MenuItem menuItem4;
48
private
System.Windows.Forms.MenuItem menuItemAll;
49
50
//
声明menuItemFormat下的MenuItem
51
private
System.Windows.Forms.MenuItem menuItemWordWrap;
52
private
System.Windows.Forms.MenuItem menuItem5;
53
private
System.Windows.Forms.MenuItem menuItemFont;
54
private
System.Windows.Forms.MenuItem menuItemColor;
55
56
//
自定义变量
57
private
string
currentFileName;
58
private
System.Drawing.Printing.PrintDocument printDocument;
59
private
bool
needToSave;
60
private
System.Windows.Forms.MenuItem menuItemHelp;
61
private
System.Windows.Forms.MenuItem menuItemAbout;
62
#endregion
63
private
IContainer components;
64
65
public
FormMain()
66
{
67
//
68
//
Windows 窗体设计器支持所必需的
69
//
70
InitializeComponent();
71
72
//
73
//
TODO: 在 InitializeComponent 调用后添加任何构造函数代码
74
//
75
}
76
77
/**/
///
<summary>
78
///
清理所有正在使用的资源。
79
///
</summary>
80
///
81
private
void
InitalizeComponent()
82
{
83
}
84
protected
override
void
Dispose(
bool
disposing )
85
{
86
if
( disposing )
87
{
88
if
(components
!=
null
)
89
{
90
components.Dispose();
91
}
92
}
93
base
.Dispose( disposing );
94
}
95
96
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
97
/**/
///
<summary>
98
///
设计器支持所需的方法 - 不要使用代码编辑器修改
99
///
此方法的内容。
100
///
</summary>
101
private
void
InitializeComponent()
102
{
103
this
.components
=
new
System.ComponentModel.Container();
104
this
.textBoxEdit
=
new
System.Windows.Forms.TextBox();
105
this
.mainMenu
=
new
System.Windows.Forms.MainMenu(
this
.components);
106
this
.menuItemFile
=
new
System.Windows.Forms.MenuItem();
107
this
.menuItemNew
=
new
System.Windows.Forms.MenuItem();
108
this
.menuItemoOpen
=
new
System.Windows.Forms.MenuItem();
109
this
.menuItemSave
=
new
System.Windows.Forms.MenuItem();
110
this
.menuItemSaveAs
=
new
System.Windows.Forms.MenuItem();
111
this
.menuItem1
=
new
System.Windows.Forms.MenuItem();
112
this
.menuItemPageSet
=
new
System.Windows.Forms.MenuItem();
113
this
.menuItemprint
=
new
System.Windows.Forms.MenuItem();
114
this
.menuItem2
=
new
System.Windows.Forms.MenuItem();
115
this
.menuItemExit
=
new
System.Windows.Forms.MenuItem();
116
this
.menuItemEdit
=
new
System.Windows.Forms.MenuItem();
117
this
.menuItemUndo
=
new
System.Windows.Forms.MenuItem();
118
this
.menuItem3
=
new
System.Windows.Forms.MenuItem();
119
this
.menuItemCut
=
new
System.Windows.Forms.MenuItem();
120
this
.menuItemCopy
=
new
System.Windows.Forms.MenuItem();
121
this
.menuItemPaste
=
new
System.Windows.Forms.MenuItem();
122
this
.menuItemDel
=
new
System.Windows.Forms.MenuItem();
123
this
.menuItem4
=
new
System.Windows.Forms.MenuItem();
124
this
.menuItemAll
=
new
System.Windows.Forms.MenuItem();
125
this
.menuItemFormat
=
new
System.Windows.Forms.MenuItem();
126
this
.menuItemWordWrap
=
new
System.Windows.Forms.MenuItem();
127
this
.menuItem5
=
new
System.Windows.Forms.MenuItem();
128
this
.menuItemFont
=
new
System.Windows.Forms.MenuItem();
129
this
.menuItemColor
=
new
System.Windows.Forms.MenuItem();
130
this
.menuItemHelp
=
new
System.Windows.Forms.MenuItem();
131
this
.menuItemAbout
=
new
System.Windows.Forms.MenuItem();
132
this
.printDocument
=
new
System.Drawing.Printing.PrintDocument();
133
this
.SuspendLayout();
134
//
135
//
textBoxEdit
136
//
137
this
.textBoxEdit.Location
=
new
System.Drawing.Point(
0
,
0
);
138
this
.textBoxEdit.Multiline
=
true
;
139
this
.textBoxEdit.Name
=
"
textBoxEdit
"
;
140
this
.textBoxEdit.Size
=
new
System.Drawing.Size(
100
,
21
);
141
this
.textBoxEdit.TabIndex
=
0
;
142
this
.textBoxEdit.Text
=
"
textBox1
"
;
143
this
.textBoxEdit.TextChanged
+=
new
System.EventHandler(
this
.textBoxEdit_TextChanged);
144
//
145
//
mainMenu
146
//
147
this
.mainMenu.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
148
this
.menuItemFile,
149
this
.menuItemEdit,
150
this
.menuItemFormat,
151
this
.menuItemHelp}
);
152
//
153
//
menuItemFile
154
//
155
this
.menuItemFile.Index
=
0
;
156
this
.menuItemFile.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
157
this
.menuItemNew,
158
this
.menuItemoOpen,
159
this
.menuItemSave,
160
this
.menuItemSaveAs,
161
this
.menuItem1,
162
this
.menuItemPageSet,
163
this
.menuItemprint,
164
this
.menuItem2,
165
this
.menuItemExit}
);
166
this
.menuItemFile.Text
=
"
文件(&F)
"
;
167
//
168
//
menuItemNew
169
//
170
this
.menuItemNew.Index
=
0
;
171
this
.menuItemNew.Shortcut
=
System.Windows.Forms.Shortcut.CtrlN;
172
this
.menuItemNew.Text
=
"
新建(&N)
"
;
173
this
.menuItemNew.Click
+=
new
System.EventHandler(
this
.menuItemNew_Click);
174
//
175
//
menuItemoOpen
176
//
177
this
.menuItemoOpen.Index
=
1
;
178
this
.menuItemoOpen.Shortcut
=
System.Windows.Forms.Shortcut.CtrlO;
179
this
.menuItemoOpen.Text
=
"
打开(&O)
"
;
180
this
.menuItemoOpen.Click
+=
new
System.EventHandler(
this
.menuItemoOpen_Click);
181
//
182
//
menuItemSave
183
//
184
this
.menuItemSave.Index
=
2
;
185
this
.menuItemSave.Shortcut
=
System.Windows.Forms.Shortcut.CtrlS;
186
this
.menuItemSave.Text
=
"
保存(&S)
"
;
187
this
.menuItemSave.Click
+=
new
System.EventHandler(
this
.menuItemSave_Click);
188
//
189
//
menuItemSaveAs
190
//
191
this
.menuItemSaveAs.Index
=
3
;
192
this
.menuItemSaveAs.Text
=
"
另存为(&A)
"
;
193
this
.menuItemSaveAs.Click
+=
new
System.EventHandler(
this
.menuItemSaveAs_Click);
194
//
195
//
menuItem1
196
//
197
this
.menuItem1.Index
=
4
;
198
this
.menuItem1.Text
=
"
-
"
;
199
//
200
//
menuItemPageSet
201
//
202
this
.menuItemPageSet.Index
=
5
;
203
this
.menuItemPageSet.Text
=
"
页面设置(&U)
"
;
204
this
.menuItemPageSet.Click
+=
new
System.EventHandler(
this
.menuItemPageSet_Click);
205
//
206
//
menuItemprint
207
//
208
this
.menuItemprint.Index
=
6
;
209
this
.menuItemprint.Shortcut
=
System.Windows.Forms.Shortcut.CtrlP;
210
this
.menuItemprint.Text
=
"
打印(&P)
"
;
211
this
.menuItemprint.Click
+=
new
System.EventHandler(
this
.menuItemprint_Click);
212
//
213
//
menuItem2
214
//
215
this
.menuItem2.Index
=
7
;
216
this
.menuItem2.Text
=
"
-
"
;
217
//
218
//
menuItemExit
219
//
220
this
.menuItemExit.Index
=
8
;
221
this
.menuItemExit.Text
=
"
退出(&X)
"
;
222
this
.menuItemExit.Click
+=
new
System.EventHandler(
this
.menuItemExit_Click);
223
//
224
//
menuItemEdit
225
//
226
this
.menuItemEdit.Index
=
1
;
227
this
.menuItemEdit.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
228
this
.menuItemUndo,
229
this
.menuItem3,
230
this
.menuItemCut,
231
this
.menuItemCopy,
232
this
.menuItemPaste,
233
this
.menuItemDel,
234
this
.menuItem4,
235
this
.menuItemAll}
);
236
this
.menuItemEdit.Text
=
"
编辑(&E)
"
;
237
this
.menuItemEdit.Popup
+=
new
System.EventHandler(
this
.menuItemEdit_popup);
238
//
239
//
menuItemUndo
240
//
241
this
.menuItemUndo.Index
=
0
;
242
this
.menuItemUndo.Shortcut
=
System.Windows.Forms.Shortcut.CtrlZ;
243
this
.menuItemUndo.Text
=
"
撤消(&U)
"
;
244
this
.menuItemUndo.Click
+=
new
System.EventHandler(
this
.menuItemUndo_Click);
245
//
246
//
menuItem3
247
//
248
this
.menuItem3.Index
=
1
;
249
this
.menuItem3.Text
=
"
-
"
;
250
//
251
//
menuItemCut
252
//
253
this
.menuItemCut.Index
=
2
;
254
this
.menuItemCut.Shortcut
=
System.Windows.Forms.Shortcut.CtrlX;
255
this
.menuItemCut.Text
=
"
剪切(&T)
"
;
256
this
.menuItemCut.Click
+=
new
System.EventHandler(
this
.menuItemCut_Click);
257
//
258
//
menuItemCopy
259
//
260
this
.menuItemCopy.Index
=
3
;
261
this
.menuItemCopy.Shortcut
=
System.Windows.Forms.Shortcut.CtrlC;
262
this
.menuItemCopy.Text
=
"
复制(&C)
"
;
263
this
.menuItemCopy.Click
+=
new
System.EventHandler(
this
.menuItemCopy_Click);
264
//
265
//
menuItemPaste
266
//
267
this
.menuItemPaste.Index
=
4
;
268
this
.menuItemPaste.Shortcut
=
System.Windows.Forms.Shortcut.CtrlP;
269
this
.menuItemPaste.Text
=
"
粘贴(&V)
"
;
270
this
.menuItemPaste.Click
+=
new
System.EventHandler(
this
.menuItemPaste_Click);
271
//
272
//
menuItemDel
273
//
274
this
.menuItemDel.Index
=
5
;
275
this
.menuItemDel.Shortcut
=
System.Windows.Forms.Shortcut.Del;
276
this
.menuItemDel.Text
=
"
删除(&L)
"
;
277
this
.menuItemDel.Click
+=
new
System.EventHandler(
this
.menuItemDel_Click);
278
//
279
//
menuItem4
280
//
281
this
.menuItem4.Index
=
6
;
282
this
.menuItem4.Text
=
"
-
"
;
283
//
284
//
menuItemAll
285
//
286
this
.menuItemAll.Index
=
7
;
287
this
.menuItemAll.Shortcut
=
System.Windows.Forms.Shortcut.CtrlA;
288
this
.menuItemAll.Text
=
"
全选(&A)
"
;
289
this
.menuItemAll.Click
+=
new
System.EventHandler(
this
.menuItemAll_Click);
290
//
291
//
menuItemFormat
292
//
293
this
.menuItemFormat.Index
=
2
;
294
this
.menuItemFormat.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
295
this
.menuItemWordWrap,
296
this
.menuItem5,
297
this
.menuItemFont,
298
this
.menuItemColor}
);
299
this
.menuItemFormat.Text
=
"
格式(&O)
"
;
300
//
301
//
menuItemWordWrap
302
//
303
this
.menuItemWordWrap.Index
=
0
;
304
this
.menuItemWordWrap.Text
=
"
自动换行(&w)
"
;
305
this
.menuItemWordWrap.Click
+=
new
System.EventHandler(
this
.menuItemWordWrap_Click);
306
//
307
//
menuItem5
308
//
309
this
.menuItem5.Index
=
1
;
310
this
.menuItem5.Text
=
"
-
"
;
311
//
312
//
menuItemFont
313
//
314
this
.menuItemFont.Index
=
2
;
315
this
.menuItemFont.Text
=
"
字体(&F)
"
;
316
this
.menuItemFont.Click
+=
new
System.EventHandler(
this
.menuItemFont_Click);
317
//
318
//
menuItemColor
319
//
320
this
.menuItemColor.Index
=
3
;
321
this
.menuItemColor.Text
=
"
背景颜色(&C)
"
;
322
this
.menuItemColor.Click
+=
new
System.EventHandler(
this
.menuItemColor_Click);
323
//
324
//
menuItemHelp
325
//
326
this
.menuItemHelp.Index
=
3
;
327
this
.menuItemHelp.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
328
this
.menuItemAbout}
);
329
this
.menuItemHelp.Text
=
"
帮助(&H)
"
;
330
//
331
//
menuItemAbout
332
//
333
this
.menuItemAbout.Index
=
0
;
334
this
.menuItemAbout.Text
=
"
关于
"
;
335
this
.menuItemAbout.Click
+=
new
System.EventHandler(
this
.menuItemAbout_Click);
336
//
337
//
printDocument
338
//
339
this
.printDocument.PrintPage
+=
new
System.Drawing.Printing.PrintPageEventHandler(
this
.printDocument_PrintPage);
340
//
341
//
FormMain
342
//
343
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
6
,
14
);
344
this
.ClientSize
=
new
System.Drawing.Size(
492
,
323
);
345
this
.Controls.Add(
this
.textBoxEdit);
346
this
.Menu
=
this
.mainMenu;
347
this
.Name
=
"
FormMain
"
;
348
this
.Text
=
"
文本编辑
"
;
349
this
.Closing
+=
new
System.ComponentModel.CancelEventHandler(
this
.FormMain_Close);
350
this
.Load
+=
new
System.EventHandler(
this
.FormMain_Load);
351
this
.ResumeLayout(
false
);
352
this
.PerformLayout();
353
354
}
355
#endregion
356
357
/**/
///
<summary>
358
///
应用程序的主入口点。
359
///
</summary>
360
[STAThread]
361
static
void
Main()
362
{
363
Application.Run(
new
FormMain());
364
}
365
366
主窗体代码
#region
主窗体代码
367
//
主窗体代码
368
private
void
FormMain_Load(
object
sender, System.EventArgs e)
369
{
370
const
int
MaxLength
=
2000000
;
371
372
textBoxEdit.MaxLength
=
MaxLength;
373
textBoxEdit.Multiline
=
true
;
374
textBoxEdit.Dock
=
DockStyle.Fill;
375
textBoxEdit.Text
=
"
我的文本编辑器
"
;
376
textBoxEdit.ScrollBars
=
ScrollBars.Both;
377
textBoxEdit.WordWrap
=
false
;
378
textBoxEdit.ReadOnly
=
false
;
379
textBoxEdit.AcceptsReturn
=
true
;
380
textBoxEdit.AcceptsTab
=
true
;
381
382
}
383
384
//
提示文本改变过
385
private
void
textBoxEdit_TextChanged(
object
sender,System.EventArgs e)
386
{
387
needToSave
=
true
;
388
}
389
390
//
窗口关闭时发生
391
private
void
FormMain_Close(
object
sender,CancelEventArgs e)
392
{
393
if
(needToSave
==
true
)
394
{
395
DialogResult result
=
MessageBox.Show(
"
文本内容已经改变,需要保存吗?
"
,
"
保存文件
"
,
396
MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
397
if
(result
==
DialogResult.Cancel)
398
{
399
e.Cancel
=
true
;
400
return
;
401
}
402
if
(result
==
DialogResult.Yes)
403
{
404
menuItemSave_Click(sender,e);
405
e.Cancel
=
false
;
406
return
;
407
}
408
}
409
}
410
#endregion
411
412
编辑菜单
#region
编辑菜单
413
//
实现动态控制菜单
414
private
void
menuItemEdit_popup(
object
sender, System.EventArgs e)
415
{
416
//
根据剪贴板上是否有TEXT的类型数据,决定menuItemPaste是否可用
417
if
(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
418
{
419
menuItemPaste.Enabled
=
true
;
420
}
421
else
422
{
423
menuItemPaste.Enabled
=
false
;
424
}
425
426
//
决定menuItemCut,menuItemCopy,menuItemDel是否可用
427
if
(textBoxEdit.SelectedText.Length
>
0
)
428
{
429
menuItemCopy.Enabled
=
true
;
430
menuItemCut.Enabled
=
true
;
431
menuItemDel.Enabled
=
true
;
432
}
433
else
434
{
435
menuItemCopy.Enabled
=
false
;
436
menuItemCut.Enabled
=
false
;
437
menuItemDel.Enabled
=
false
;
438
}
439
440
//
决定menuItemUndo是否可用
441
if
(textBoxEdit.CanUndo
==
true
)
442
{
443
menuItemUndo.Enabled
=
true
;
444
}
445
else
446
{
447
menuItemUndo.Enabled
=
false
;
448
}
449
}
450
//
撤消
451
private
void
menuItemUndo_Click(
object
sender, System.EventArgs e)
452
{
453
if
(textBoxEdit.CanUndo
==
true
)
454
{
455
textBoxEdit.Undo();
456
457
textBoxEdit.ClearUndo();
458
}
459
460
}
461
462
//
剪切
463
private
void
menuItemCut_Click(
object
sender, System.EventArgs e)
464
{
465
if
(textBoxEdit.SelectedText
!=
""
)
466
{
467
textBoxEdit.Cut();
468
}
469
470
}
471
472
//
复制
473
private
void
menuItemCopy_Click(
object
sender, System.EventArgs e)
474
{
475
textBoxEdit.Copy();
476
477
}
478
479
//
粘贴
480
private
void
menuItemPaste_Click(
object
sender, System.EventArgs e)
481
{
482
if
(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)
==
true
)
483
{
484
if
(textBoxEdit.SelectionLength
>
0
)
485
{
486
DialogResult result;
487
result
=
MessageBox.Show(
"
你想覆盖掉选择的文本吗?
"
,
"
覆盖确认
"
,MessageBoxButtons.YesNo);
488
489
if
(result
==
DialogResult.No)
490
{
491
textBoxEdit.SelectionStart
=
textBoxEdit.SelectionStart
+
textBoxEdit.SelectionLength;
492
}
493
}
494
textBoxEdit.Paste();
495
}
496
497
}
498
499
//
删除
500
private
void
menuItemDel_Click(
object
sender, System.EventArgs e)
501
{
502
if
(textBoxEdit.SelectedText
!=
""
)
503
{
504
textBoxEdit.SelectedText
=
""
;
505
}
506
507
}
508
509
//
全选
510
private
void
menuItemAll_Click(
object
sender, System.EventArgs e)
511
{
512
textBoxEdit.SelectAll();
513
514
}
515
#endregion
516
517
文件菜单
#region
文件菜单
518
//
新建菜单
519
private
void
menuItemNew_Click(
object
sender, System.EventArgs e)
520
{
521
if
(needToSave
==
true
)
522
{
523
DialogResult result
=
MessageBox.Show(
"
文本文件内容已经改变,需要保存吗?
"
524
,
"
保存文件
"
,MessageBoxButtons.YesNoCancel,
525
MessageBoxIcon.Question);
526
switch
(result)
527
{
528
case
DialogResult.Yes:
529
{
530
menuItemSave_Click(sender,e);
531
textBoxEdit.Clear();
532
this
.Text
=
"
文本编辑--新建文本
"
;
533
needToSave
=
false
;
534
break
;
535
}
536
case
DialogResult.No:
537
{
538
textBoxEdit.Clear();
539
this
.Text
=
"
文本编辑--新建文本
"
;
540
needToSave
=
false
;
541
break
;
542
}
543
case
DialogResult.Cancel:
544
{
545
break
;
546
}
547
}
548
}
549
else
550
{
551
textBoxEdit.Clear();
552
this
.Text
=
"
文本编辑--新建文本
"
;
553
}
554
}
555
556
//
打开
557
private
void
menuItemoOpen_Click(
object
sender, System.EventArgs e)
558
{
559
if
(needToSave
==
true
)
560
{
561
DialogResult result
=
MessageBox.Show(
"
文本内容已经改变,需要保存吗?
"
,
"
保存文件
"
,
562
MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
563
if
(result
==
DialogResult.Cancel)
564
{
565
return
;
566
}
567
if
(result
==
DialogResult.Yes)
568
{
569
menuItemSave_Click (sender,e);
570
needToSave
=
false
;
571
}
572
string
file
=
GetOpenFile();
573
574
if
(file
==
null
)
575
{
576
return
;
577
}
578
579
else
580
{
581
currentFileName
=
file;
582
OpenFile();
583
needToSave
=
false
;
584
}
585
}
586
}
587
588
//
打开指定文件
589
private
void
OpenFile()
590
{
591
try
592
{
593
FileInfo f
=
new
FileInfo(currentFileName);
594
StreamReader reader
=
f.OpenText();
595
textBoxEdit.Text
=
reader.ReadToEnd();
596
reader.Close();
597
this
.Text
=
"
文本编辑--
"
+
f.Name;
598
}
599
catch
(Exception e)
600
{
601
MessageBox.Show(e.Message);
602
}
603
}
604
605
//
获取要打开的文件名
606
private
string
GetOpenFile ()
607
{
608
OpenFileDialog openFile
=
new
OpenFileDialog();
609
openFile.Title
=
"
打开文本文件
"
;
610
openFile.CheckFileExists
=
true
;
611
openFile.CheckPathExists
=
true
;
612
openFile.AddExtension
=
true
;
613
openFile.Multiselect
=
false
;
614
openFile.Filter
=
"
文本文件(*.txt)|*.txt|所有文件(*.*)|*.*
"
;
615
616
if
(openFile.ShowDialog()
==
DialogResult.OK)
617
{
618
return
openFile.FileName;
619
}
620
else
621
{
622
return
null
;
623
}
624
}
625
626
//
保存文件
627
private
void
menuItemSave_Click(
object
sender, System.EventArgs e)
628
{
629
if
(currentFileName
==
null
)
630
{
631
menuItemSaveAs_Click (sender,e);
632
}
633
else
634
{
635
SaveFile(textBoxEdit.Text);
636
}
637
needToSave
=
false
;
638
}
639
640
//
另存为
641
private
void
menuItemSaveAs_Click(
object
sender, System.EventArgs e)
642
{
643
string
file
=
GetSaveFile();
644
645
if
(file
==
null
)
646
{
647
return
;
648
}
649
else
650
{
651
currentFileName
=
file;
652
SaveFile(textBoxEdit.Text);
653
FileInfo f
=
new
FileInfo(currentFileName);
654
this
.Text
=
"
文本编辑--
"
+
f.Name;
655
needToSave
=
false
;
656
}
657
}
658
659
//
保存文件
660
private
void
SaveFile (
string
str)
661
{
662
try
663
{
664
StreamWriter writer
=
new
StreamWriter(currentFileName);
665
writer.Write(str);
666
writer.Close();
667
}
668
catch
(Exception e)
669
{
670
MessageBox.Show(e.Message);
671
}
672
}
673
674
//
获得保存的文件名
675
private
string
GetSaveFile()
676
{
677
SaveFileDialog saveFile
=
new
SaveFileDialog();
678
saveFile.Title
=
"
保存文本文件
"
;
679
saveFile.OverwritePrompt
=
true
;
680
saveFile.CreatePrompt
=
true
;
681
saveFile.AddExtension
=
true
;
682
saveFile.Filter
=
"
文本文件(*.txt)|*.txt|所有文件(*.*)|(*.*)
"
;
683
684
if
(saveFile.ShowDialog()
==
DialogResult.OK)
685
{
686
return
saveFile.FileName;
687
}
688
else
689
{
690
return
null
;
691
}
692
}
693
694
//
退出
695
private
void
menuItemExit_Click(
object
sender, System.EventArgs e)
696
{
697
//
关闭窗口
698
DialogResult diaRes;
699
700
if
(textBoxEdit.Text
==
""
)
701
{
702
this
.Close();
703
return
;
704
}
705
MessageBoxButtons buttons
=
MessageBoxButtons.YesNo;
706
MessageBoxDefaultButton defaultButton
=
MessageBoxDefaultButton.Button1;
707
MessageBoxIcon icon
=
MessageBoxIcon.Question;
708
MessageBoxOptions options
=
MessageBoxOptions.RightAlign;
709
string
ask
=
"
将要退出程序,是否继续
"
;
710
string
caption
=
"
退出程序
"
;
711
diaRes
=
MessageBox.Show(
this
,ask,caption,buttons,icon,defaultButton,options);
712
if
(diaRes
==
DialogResult.Yes)
713
{
714
this
.Close();
715
}
716
}
717
718
719
720
//
打印清单
721
private
void
printDocument_PrintPage(
object
sender,System.Drawing.Printing.PrintPageEventArgs e)
722
{
723
float
linesPerPage
=
0
;
724
float
yPos
=
0
;
725
int
count
=
0
;
726
float
leftMargin
=
e.MarginBounds.Left;
727
float
topMargin
=
e.MarginBounds.Top;
728
string
line
=
null
;
729
StreamReader streamToPrint
=
new
StreamReader(currentFileName);
730
SolidBrush brush
=
new
SolidBrush(textBoxEdit.ForeColor);
731
linesPerPage
=
e.MarginBounds.Height
/
textBoxEdit.Font.GetHeight(e.Graphics);
732
733
while
(count
<
linesPerPage
&&
((line
=
streamToPrint.ReadLine())
!=
null
))
734
{
735
yPos
=
topMargin
+
(count
*
textBoxEdit.Font.GetHeight(e.Graphics));
736
e.Graphics.DrawString(line,textBoxEdit.Font,brush,leftMargin,yPos,
new
StringFormat());
737
count
++
;
738
}
739
740
if
(line
!=
null
)
741
{
742
e.HasMorePages
=
true
;
743
}
744
else
745
{
746
e.HasMorePages
=
false
;
747
}
748
}
749
750
//
打印
751
private
void
menuItemprint_Click(
object
sender, System.EventArgs e)
752
{
753
PrintDialog printDialog
=
new
PrintDialog();
754
printDialog.Document
=
printDocument;
755
756
if
(printDialog.ShowDialog()
==
DialogResult.OK)
757
{
758
try
759
{
760
printDocument.Print();
761
}
762
catch
(Exception ex)
763
{
764
MessageBox.Show(ex.Message);
765
}
766
}
767
}
768
769
//
页面设置
770
private
void
menuItemPageSet_Click(
object
sender, System.EventArgs e)
771
{
772
PageSetupDialog pageSet
=
new
PageSetupDialog();
773
pageSet.Document
=
printDocument;
774
pageSet.ShowDialog();
775
}
776
#endregion
777
778
格式菜单
#region
格式菜单
779
//
改变字体
780
private
void
menuItemFont_Click(
object
sender, System.EventArgs e)
781
{
782
FontDialog fontDialog
=
new
FontDialog();
783
fontDialog.ShowColor
=
true
;
784
fontDialog.AllowScriptChange
=
true
;
785
fontDialog.AllowVectorFonts
=
true
;
786
fontDialog.ShowEffects
=
true
;
787
788
if
(fontDialog.ShowDialog()
==
DialogResult.OK)
789
{
790
textBoxEdit.Font
=
fontDialog.Font;
791
textBoxEdit.ForeColor
=
fontDialog.Color;
792
}
793
794
}
795
796
//
改变背景颜色
797
private
void
menuItemColor_Click(
object
sender, System.EventArgs e)
798
{
799
ColorDialog colorDialog
=
new
ColorDialog();
800
colorDialog.AllowFullOpen
=
true
;
801
colorDialog.AnyColor
=
true
;
802
colorDialog.FullOpen
=
true
;
803
804
if
(colorDialog.ShowDialog()
==
DialogResult.OK)
805
{
806
textBoxEdit.BackColor
=
colorDialog.Color;
807
}
808
}
809
#endregion
810
811
//
关于
812
private
void
menuItemAbout_Click(
object
sender, System.EventArgs e)
813
{
814
AboutForm aboutForm
=
new
AboutForm();
815
aboutForm.ShowDialog();
816
}
817
818
private
void
menuItemWordWrap_Click(
object
sender, EventArgs e)
819
{
820
821
}
822
823
}
824
}
825
|