• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi实例之一个较复杂的记事本的实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

                                                                                                                                       by Chaobs

一、概述

        前几天在学习Delphi时照着书上的练习做了一个记事本程序,自己略加改进,将过程记录下来也是方便初学者学习,其中多有错误还望指点。

       功能:

              (1).基本实现了Windows自带的记事本的功能;

              (2).具有字体设置、字体颜色和自动排版的设计;

               (3).加入了日历的功能。

      运行界面(UI做的很丑诃):

            

    设计思想:

          通过Delphi组件中的RichEdit实现文本的编辑,OepnDialog和SaveDialog用于打开和保存文件,排版、复制等事件用Standar Action来实现。

二、工程结构

        (1).用户界面设计

                     Form1主窗口

                 

                 Form2 Form3 Form5 Form6 (Form4后来被我删了但是窗体的Name我没有改,所以Form4是空的,同理下文的unit4)

 

     (2).最后各组件之间的包含关系如下

                  

 

三、Source Code

          以往在网上看到很多Delphi源码都没有发布.dfm文件,这次我将会发布DFM文件。

    1.Form1.pas

       

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7   Dialogs, ImgList, ComCtrls, ToolWin, ExtCtrls, StdCtrls, Menus, ExtActns,
  8   StdActns, ActnList;
  9 
 10 type
 11   TForm1 = class(TForm)
 12     ImageList1: TImageList;
 13     ToolBar1: TToolBar;
 14     ToolButton1: TToolButton;
 15     ToolButton2: TToolButton;
 16     ToolButton3: TToolButton;
 17     ToolButton4: TToolButton;
 18     ToolButton5: TToolButton;
 19     ToolButton6: TToolButton;
 20     ToolButton7: TToolButton;
 21     ToolButton8: TToolButton;
 22     ToolButton9: TToolButton;
 23     StatusBar1: TStatusBar;
 24     Timer1: TTimer;
 25     OpenDialog1: TOpenDialog;
 26     RichEdit1: TRichEdit;
 27     SaveDialog1: TSaveDialog;
 28     ToolButton11: TToolButton;
 29     FontDialog1: TFontDialog;
 30     ToolButton12: TToolButton;
 31     FindDialog1: TFindDialog;
 32     ToolButton13: TToolButton;
 33     ReplaceDialog1: TReplaceDialog;
 34     ToolButton14: TToolButton;
 35     ColorDialog1: TColorDialog;
 36     ToolButton15: TToolButton;
 37     PrintDialog1: TPrintDialog;
 38     MainMenu1: TMainMenu;
 39     N1: TMenuItem;
 40     N2: TMenuItem;
 41     N3: TMenuItem;
 42     N4: TMenuItem;
 43     N5: TMenuItem;
 44     N6: TMenuItem;
 45     N7: TMenuItem;
 46     N8: TMenuItem;
 47     N9: TMenuItem;
 48     N10: TMenuItem;
 49     N11: TMenuItem;
 50     N12: TMenuItem;
 51     N13: TMenuItem;
 52     N14: TMenuItem;
 53     N15: TMenuItem;
 54     N16: TMenuItem;
 55     N17: TMenuItem;
 56     N18: TMenuItem;
 57     N19: TMenuItem;
 58     N20: TMenuItem;
 59     N21: TMenuItem;
 60     N22: TMenuItem;
 61     N23: TMenuItem;
 62     N24: TMenuItem;
 63     ActionList1: TActionList;
 64     EditCut1: TEditCut;
 65     EditCopy1: TEditCopy;
 66     EditPaste1: TEditPaste;
 67     EditSelectAll1: TEditSelectAll;
 68     EditUndo1: TEditUndo;
 69     EditDelete1: TEditDelete;
 70     RichEditBold1: TRichEditBold;
 71     RichEditItalic1: TRichEditItalic;
 72     RichEditUnderline1: TRichEditUnderline;
 73     N25: TMenuItem;
 74     PopupMenu1: TPopupMenu;
 75     Copy1: TMenuItem;
 76     Paste1: TMenuItem;
 77     Cut1: TMenuItem;
 78     SelectAll1: TMenuItem;
 79     Delete1: TMenuItem;
 80     N26: TMenuItem;
 81     N27: TMenuItem;
 82     N28: TMenuItem;
 83     Help1: TMenuItem;
 84     About1: TMenuItem;
 85     Contents1: TMenuItem;
 86     RichEditAlignLeft1: TRichEditAlignLeft;
 87     RichEditAlignRight1: TRichEditAlignRight;
 88     RichEditAlignCenter1: TRichEditAlignCenter;
 89     Center1: TMenuItem;
 90     AlignLeft1: TMenuItem;
 91     AlignRight1: TMenuItem;
 92     C1: TMenuItem;
 93     N29: TMenuItem;
 94     N30: TMenuItem;
 95     N31: TMenuItem;
 96     procedure FormCreate(Sender: TObject);
 97     procedure Timer1Timer(Sender: TObject);
 98     procedure RichEdit1SelectionChange(Sender: TObject);
 99     procedure RichEdit1Change(Sender: TObject);
100     procedure ToolButton2Click(Sender: TObject);
101     procedure ToolButton3Click(Sender: TObject);
102     procedure ToolButton11Click(Sender: TObject);
103     procedure ToolButton12Click(Sender: TObject);
104     procedure FindDialog1Find(Sender: TObject);
105     procedure ToolButton13Click(Sender: TObject);
106     procedure ReplaceDialog1Replace(Sender: TObject);
107     procedure ToolButton14Click(Sender: TObject);
108     procedure ToolButton15Click(Sender: TObject);
109     procedure ToolButton1Click(Sender: TObject);
110     procedure N6Click(Sender: TObject);
111     procedure About1Click(Sender: TObject);
112     procedure Contents1Click(Sender: TObject);
113     procedure C1Click(Sender: TObject);
114     procedure N30Click(Sender: TObject);
115     procedure N31Click(Sender: TObject);
116   private
117     { Private declarations }
118   public
119     { Public declarations }
120   end;
121 
122 var
123   Form1: TForm1;
124 
125 implementation
126 
127 uses Unit2, Unit3, Unit5, Unit6;
128 
129 {$R *.dfm}
130 
131 {此过程用于窗口创建时状态栏时间的显示}
132 procedure TForm1.FormCreate(Sender: TObject);
133 begin
134   StatusBar1.Panels[3].Text:=DateTimeToStr(Now);
135 end;
136 
137 {此过程用于 动态修改状态栏时间}
138 procedure TForm1.Timer1Timer(Sender: TObject);
139 begin
140   StatusBar1.Panels[3].Text:=DateTimeToStr(Now);
141 end;
142 
143 {此过程用于显示光标所在的位置}
144 procedure TForm1.RichEdit1SelectionChange(Sender: TObject);
145 begin
146   StatusBar1.Panels[0].Text:='位置:'+IntToStr(RichEdit1.CaretPos.Y+1)+''
147     +IntToStr(RichEdit1.CaretPos.X)+'';
148 end;
149 
150 {此过程用于在状态栏显示文件是否被修改}
151 procedure TForm1.RichEdit1Change(Sender: TObject);
152 begin
153   if RichEdit1.Modified then
154     StatusBar1.Panels[1].Text:='修改'
155 end;
156 
157 {打开文本文件操作}
158 procedure TForm1.ToolButton2Click(Sender: TObject);
159 var
160   FName:string;
161 begin
162    with OpenDialog1 do
163      begin
164        Filter:='文本文件(*.txt|*.txt';      {文件类型过滤器,防止打开非文本文件}
165        DefaultExt:='txt';
166        Filename:='';
167        Options:=[ofHideReadOnly,ofFileMustExist,ofPathMustExist];
168        if execute
169          then
170            if ofExtensionDifferent in Options
171              then MessageDlg('这不是文本文件。',mtError,[mbOK],0)
172              else
173                begin
174                  RichEdit1.Lines.LoadFromFile(OpenDialog1.FileName);
175                  FName:=OpenDialog1.FileName;
176                  StatusBar1.Panels[2].Text:=FName;
177                end;
178      end;
179 end;
180 
181 {此过程用于保存文件}
182 procedure TForm1.ToolButton3Click(Sender: TObject);
183 var
184   fname:string;
185 begin
186    with SaveDialog1 do
187      if Execute
188        then
189          begin
190            DefaultExt:='txt';
191            RichEdit1.Lines.SaveToFile(FileName);
192            RichEdit1.Modified:=false;
193            FName:=SaveDialog1.FileName;
194            StatusBar1.Panels[2].Text:=FName;
195          end;
196 end;
197 
198 {改变字体的过程}
199 procedure TForm1.ToolButton11Click(Sender: TObject);
200 begin
201   If RichEdit1.SelLength>0
202     then
203       begin
204         FontDialog1.Font.Assign(RichEdit1.SelAttributes);{使用FontDialog}
205         if FontDialog1.Execute
206           then RichEdit1.SelAttributes.Assign(FontDialog1.Font);
207       end
208     else
209       ShowMessage('请先选中一段文本。')    {没有选中任何字}
210 end;
211 
212 {此过程用于启动查找}
213 procedure TForm1.ToolButton12Click(Sender: TObject);
214 begin
215   FindDialog1.Execute
216 end;
217 
218 {通过 FindDialog 提供的方法来实现查找文本}
219 procedure TForm1.FindDialog1Find(Sender: TObject);
220 var
221   FoundPos,InitPos:integer;
222 begin
223   InitPos:=RichEdit1.SelStart+RichEdit1.SelLength;
224   FoundPos:=Pos(FindDialog1.FindText,Copy(RichEdit1.Text,
225              InitPos+1,Length(RichEdit1.Text)-InitPos));
226   {用pos函数定位字符串位置}
227   if FoundPos>0   {文档中不止一个}
228     then
229       begin
230         RichEdit1.SetFocus;
231         RichEdit1.SelStart:=InitPos+FoundPos-1;
232         RichEdit1.SelLength:=Length(FindDialog1.FindText);
233       end
234     else     {没有找到}
235       MessageDlg('没有找到要查找的文本',mtInFormation,[mbOK],0);
236   InitPos:=RichEdit1.SelStart+RichEdit1.SelLength;
237 end;
238 
239 {启动替换}
240 procedure TForm1.ToolButton13Click(Sender: TObject);
241 begin
242   ReplaceDialog1.Execute;
243 end;
244 
245 {此过程用于实现替换功能}
246 procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
247 var
248   i,n:integer;
249   s,find,replace:string;
250 begin
251   s:=RichEdit1.Text;
252   find:=ReplaceDialog1.FindText;
253   n:=Length(find);
254   Replace:=ReplaceDialog1.ReplaceText;
255   i:=Pos(find,s);
256   RichEdit1.SelStart:=i-1;
257   RichEdit1.SelLength:=n;
258   RichEdit1.SelText:=Replace;
259   if frReplaceAll in ReplaceDialog1.Options
260     then
261       repeat
262         s:=RichEdit1.Text;
263         i:=pos(find,s);
264         if i<>0
265           then
266             RichEdit1.Text:=Copy(s,1,i-1)+Copy(s,i+n,length(s)-(i+n)+1);
267             {将文件分成三段:1--(i-1),i--(i+n),(i+n+1)--Length(s)}
268       until i=0;
269 end;
270 
271 {此过程实现文字颜色的改变}
272 procedure TForm1.ToolButton14Click(Sender: TObject);
273 begin
274   with ColorDialog1 do
275     begin
276       Color:=RichEdit1.SelAttributes.Color;
277       if Execute
278         then
279           RichEdit1.SelAttributes.Color:=Color;
280     end;
281 end;
282 
283 {此过程用于实现打印功能}
284 procedure TForm1.ToolButton15Click(Sender: TObject);
285 begin
286   with PrintDialog1 do
287     begin
288       Options:=[poPrintToFile];
289       PrintToFile:=true;
290       if execute
291         then
292           begin
293             if PrintToFile
294               then
295                  begin
296                    SaveDialog1.Title:='打印到文件: ';
297                    if SaveDialog1.Execute
298                      then RichEdit1.Lines.SaveToFile(SaveDialog1.FileName+
299                            '.prt');
300                  end
301               else RichEdit1.PaintTo(Handle,10,10);
302           end;
303     end;
304 
305 end;
306 
307 {此过程用于实现“新建”,注意:这将会清空所有未保存的内容}
308 procedure TForm1.ToolButton1Click(Sender: TObject);
309 begin
310   RichEdit1.Clear;
311 end;
312 
313 {退出}
314 procedure TForm1.N6Click(Sender: TObject);
315 begin
316   Form1.Close;
317 end;
318 
319 {弹出作者姓名}
320 procedure TForm1.About1Click(Sender: TObject);
321 begin
322   ShowMessage('(C)Choabs,2014') ;
323 end;
324 
325 {打开帮助窗口}
326 procedure TForm1.Contents1Click(Sender: TObject);
327 begin
328   Form2.Show;
329 end;
330 
331 {打开日历窗口}
332 procedure TForm1.C1Click(Sender: TObject);
333 begin
334   Form3.Show;
335 end;
336 
337 {打开法律信息窗口}
338 procedure TForm1.N30Click(Sender: TObject);
339 begin
340   form5.Show;
341 end;
342 
343 {打开感谢窗口}
344 procedure TForm1.N31Click(Sender: TObject);
345 begin
346   form6.show;
347 end;
348 
349 end.

 

2.Form2.pas

   

 1 {帮助信息窗口}
 2 unit Unit2;
 3 
 4 interface
 5 
 6 uses
 7   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 8   Dialogs, StdCtrls, ComCtrls, ExtCtrls;
 9 
10 type
11   TForm2 = class(TForm)
12     PageControl1: TPageControl;
13     TabSheet1: TTabSheet;
14     TabSheet2: TTabSheet;
15     TabSheet3: TTabSheet;
16     Label1: TLabel;
17     Label2: TLabel;
18     Label3: TLabel;
19     Label4: TLabel;
20     Label5: TLabel;
21     Label6: TLabel;
22     Label7: TLabel;
23     Button1: TButton;
24     Button2: TButton;
25     Button3: TButton;
26     Label8: TLabel;
27     Label9: TLabel;
28     Label10: TLabel;
29     Label11: TLabel;
30     Image1: TImage;
31     Label12: TLabel;
32     Label13: TLabel;
33     Label14: TLabel;
34     Label15: TLabel;
35     Label16: TLabel;
36     Label17: TLabel;
37     Label18: TLabel;
38     Label19: TLabel;
39     TabSheet4: TTabSheet;
40     Memo1: TMemo;
41     procedure Button1Click(Sender: TObject);
42   private
43     { Private declarations }
44   public
45     { Public declarations }
46   end;
47 
48 var
49   Form2: TForm2;
50 
51 implementation
52 
53 {$R *.dfm}
54 
55 {关闭帮助窗口}
56 procedure TForm2.Button1Click(Sender: TObject);
57 begin
58   Form2.Close;
59 end;
60 
61 end.

 

3.Form3.pas

 1 {日历窗口}
 2 unit Unit3;
 3 
 4 interface
 5 
 6 uses
 7   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 8   Dialogs, ComCtrls, Grids, Calendar, StdCtrls, ImgList;
 9 
10 type
11   TForm3 = class(TForm)
12     PageControl1: TPageControl;
13     TabSheet1: TTabSheet;
14     TabSheet2: TTabSheet;
15     ImageList1: TImageList;
16     Edit1: TEdit;
17     MonthCalendar1: TMonthCalendar;
18     Calendar1: TCalendar;
19     DateTimePicker1: TDateTimePicker;
20     DateTimePicker2: TDateTimePicker;
21     procedure FormCreate(Sender: TObject);
22     procedure PageControl1Change(Sender: TObject);
23     procedure DateTimePicker1Change(Sender: TObject);
24     procedure Calendar1Change(Sender: TObject);
25     procedure PageControl1Changing(Sender: TObject;
26       var AllowChange: Boolean);
27   private
28      
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB 7.0从入门到精通(修订版)发布时间:2022-07-18
下一篇:
Matlab如何创建带标记的线图发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap