用delphi编写的记事本代码.
Delphi中ScriptControl的高级应用(二)
Delphi中ScriptControl的高级应用(二)5、Delphi中使用javascript的Base64的加解密算法搜索google:javascript base64加密就会找到很多base64加密的代码,以下就是本人搜索到的一段代码(以下代码版权归其原作者):这么长的一段代码,我们怎样才能以AddCode()的方法加入进去呢?1)第一种方法:把这么长的代码定义给一个字符串。
这个工作量肯定非常大,而且容易出错。
2)第二种方法:把它定义到一个记事本里,程序运行的时候读入。
安全性很低,容易被人修改,导致出错。
3)第三种方法:把它以资源文件的形式存放在一个Dll里。
这种方法比较适合。
以下我们就用这种方法来处理。
生成DLL的步骤:1)新建一个记事本,粘贴上面的代码,最后保存为Base64.txt。
2)新建一个记事本,写上以下代码,最后保存为 Base64.rc。
3)同目录下新建一个批处理文件,保存为Base64.bat。
4)编译成资源文件Res:双击执行Base64.bat,完后会生成一个Base64.res的文件,这个就是JavaScript脚本的资源文件。
5)新建一个DLL,保存为Base64.dpr,加上以下代码,然后编译成Base64.dll。
这样,我们的资源文件打包成dll就完成了!下面,我们的工作就是通过dll来读取JavaScript代码。
下面要做的工作就是通过ScriptControl控件的AddCode()来添加自定义JavaScript函数里。
初始化ScriptControl组件,请参看Delphi中ScriptControl的高级应用(一)/juwuyi/archive/2005/03/19/324041.asp x。
delphi查找文件代码
在Windows程序设计中WinAPI也为我们提供了类似的函数,它们就是WinExec()和ShellExecute(),下面就来讨论一下这两个函数的用法。
1)WinExec() 函数原型:UINT WinExec(LPCSTR lpCmdLine,UINTuCmdShow);参数说明:(激活的意思是能接受焦点,即标题栏变成蓝色)lpCmdLine:以0结尾的字符串,命令行参数。
uCmdShow:新的应用程序的运行方式。
其取值如下:SW_HIDE 隐藏SW_MAXIMIZE 最大化SW_MINIMIZE 最小化,并把Z order顺序在此窗口之后(即窗口下一层)的窗口激活SW_RESTORE 激活窗口并还原为初始化大小SW_SHOW 以当前大小和状态激活窗口SW_SHOWDEFAULT 以默认方式运行SW_SHOWMAXIMIZED 激活窗口并最大化SW_SHOWMINIMIZED 激活窗口并最小化SW_SHOWMINNOACTIVE 最小化但不改变当前激活的窗口SW_SHOWNA 以当前状态显示窗口但不改变当前激活的窗口SW_SHOWNOACTIVATE 以初始化大小显示窗口但不改变当前激活的窗口SW_SHOWNORMAL 激活并显示窗口,如果是最大(小)化,窗口将会还原。
第一次运行程序时应该使用这个值比如说,我想要用记事本打开"C:\HDC.TXT",以正常方式运行:WinExec("notepad c:\\hdc.txt",SW_SHOWNORMAL);如果调用成功,这个函数会返回一个不小于31的值,否则调用失败,其返回值的意义如下:0 系统内存或资源不足ERROR_BAD_FORMAT .EXE文件格式无效(比如不是32位应用程序)ERROR_FILE_NOT_FOUND 指定的文件设有找到ERROR_PATH_NOT_FOUND 指定的路径没有找到这个函数和system()非常类似,只能运行.EXE文件,这样在WINDOWS中有它不尽人意的地方,比如不能用此方法通过关联的方法打开文件,例如WinExec("1.html",SW_SHOWNA);就不能打开此文档。
Delphi高仿windows记事本源码
主要功能都已添加上并测试可用。
效果图:源码:unit Unit1Calculator;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls, ComCtrls;typeTForm1 = class(TForm)ColorDialog1: TColorDialog;SaveDialog1: TSaveDialog;ColorDialog2: TColorDialog;MainMenu1: TMainMenu;N1: TMenuItem;myNew: TMenuItem;mySave: TMenuItem;N3: TMenuItem;myPage: TMenuItem;myPrint: TMenuItem;myQuit: TMenuItem;myEdit: TMenuItem;myUndo: TMenuItem;N4: TMenuItem;myCut: TMenuItem;myCopy: TMenuItem;myPaste: TMenuItem;myDelete: TMenuItem;N6: TMenuItem;myFind: TMenuItem;myReplace: TMenuItem;myFormat: TMenuItem;myFont: TMenuItem;myView: TMenuItem;myStatus: TMenuItem;myHelp: TMenuItem;myHelpTopics: TMenuItem;N9: TMenuItem;myAbout: TMenuItem;OpenDialog1: TOpenDialog;FontDialog1: TFontDialog;myOpen: TMenuItem;FindDialog1: TFindDialog;ReplaceDialog1: TReplaceDialog;FontDialog2: TFontDialog;PageSetupDialog1: TPageSetupDialog;PrintDialog1: TPrintDialog;RichEdit1: TRichEdit;procedure myNewClick(Sender: TObject);procedure FormCreate(Sender: TObject);procedure myOpenClick(Sender: TObject);procedure mySaveClick(Sender: TObject);procedure myQuitClick(Sender: TObject);procedure myUndoClick(Sender: TObject);procedure myCutClick(Sender: TObject);procedure myCopyClick(Sender: TObject);procedure myPasteClick(Sender: TObject);procedure myDeleteClick(Sender: TObject);procedure myFindClick(Sender: TObject);procedure myReplaceClick(Sender: TObject);procedure myFontClick(Sender: TObject);procedure myPageClick(Sender: TObject);procedure myPrintClick(Sender: TObject);procedure myAboutClick(Sender: TObject);procedure myHelpTopicsClick(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction); private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}var fName: String;procedure TForm1.myNewClick(Sender: TObject);var msg : integer;beginif (richEdit1.Modified) thenbeginmsg := Application.MessageBox('文件已被修改,是否保存?', 'Delphi记事本', mb_YesNoCancel);case msg ofidYes: beginmySaveClick(Sender);richEdit1.Clear;fName := '文档1';Caption := fName + '--Delphi记事本';end;idNo: beginrichEdit1.Clear;fName := '文档1';Caption := fName + '--Delphi记事本';end;idCancel: ;end;endelse beginrichEdit1.Clear;fName := '文档1';Caption := fName + '--Delphi记事本';end;end;procedure TForm1.FormCreate(Sender: TObject);beginfName := '文档1';richEdit1.Clear;Caption := fName + '--Delphi记事本';end;procedure TForm1.myOpenClick(Sender: TObject);var msg : integer;beginif (richEdit1.Modified) thenbeginmsg := Application.MessageBox('文件已被修改,是否保存?', 'Delphi记事本', mb_YesNoCancel);case msg ofidYes: beginmySaveClick(Sender);end;idNo: if (openDialog1.Execute) thenbeginfName := openDialog1.FileName;richEdit1.Lines.LoadFromFile(fName);form1.Caption := fName + 'Delphi记事本';end;idCancel: ;end;endelse if (openDialog1.Execute) thenbeginfName := openDialog1.FileName;richEdit1.Lines.LoadFromFile(fName);form1.Caption := fName + 'Delphi记事本';end;end;procedure TForm1.mySaveClick(Sender: TObject);beginif (fName <> '文档1') thenbeginrichEdit1.Lines.SaveToFile(fName);endelse beginsaveDialog1.FileName := fName;if (saveDialog1.Execute) thenbeginfName := saveDialog1.FileName;richEdit1.Lines.SaveToFile(fName);form1.Caption := fName;end;end;richEdit1.Modified := false;end;procedure TForm1.myQuitClick(Sender: TObject);beginif (richEdit1.Modified) then mySaveClick(Sender); Application.Terminate;end;procedure TForm1.myUndoClick(Sender: TObject);beginrichEdit1.Undo;end;procedure TForm1.myCutClick(Sender: TObject);beginrichEdit1.CutToClipboard;end;procedure TForm1.myCopyClick(Sender: TObject);beginrichEdit1.CopyToClipboard;end;procedure TForm1.myPasteClick(Sender: TObject);beginrichEdit1.PasteFromClipboard;end;procedure TForm1.myDeleteClick(Sender: TObject);beginrichEdit1.ClearSelection;end;procedure TForm1.myFindClick(Sender: TObject);beginfindDialog1.Execute;end;procedure TForm1.myReplaceClick(Sender: TObject);beginreplaceDialog1.Execute;end;procedure TForm1.myFontClick(Sender: TObject);beginfontDialog1.Font := richEdit1.Font; //初始化字体对话框if (fontDialog1.Execute) then richEdit1.SelAttributes.Assign(fontDialog1.Font); end;procedure TForm1.myPageClick(Sender: TObject);beginpageSetupDialog1.Execute;end;procedure TForm1.myPrintClick(Sender: TObject);beginif (printDialog1.Execute) thenbeginrichEdit1.Print(fName);end;end;procedure TForm1.myAboutClick(Sender: TObject);beginShowMessage('Delphi记事本1.0' + #13 + ' 记念于12.5.14');end;procedure TForm1.myHelpTopicsClick(Sender: TObject);beginmyAboutClick(Sender);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); beginmyQuitClick(Sender);end;end.。
delphi代码
实验7:BeginIf RadoButton1.Checked=true thenBeginWith Open Dialog1 doBeginFilter:=’文本文件’(*.txt)*.txt’ ;DefaultExt:=’txt’;Filename:=”;Options:=[ofHideReadOnly,ifFilemustexist,ofpathmustexist];If execute thenIf ofextensiondifferent in options thenMessigedlg(不存在扩展名为.txt的文件’,mterror,[mbok],0) ElseMemol1.Lines.loadfromfile(filename);End;EndIf radiobutton2.cheaked=true thenBeginWith optiondialog1 doBeginFilter:=’任何类型文件(*.*)|*.*‘;Filename:=”;Options:=[];If execute thenMemo1.lines.loadfromfile(filename);End;End;End;Procedure tform1.button2click(sender:tobject);BeginIf radiobutton1.checked=true thenBeginWith savedialog1 doBeginFilter:=’文本文件(*.txt)|*.txt’;Defaultext:=’txt’;Filename:=”;Options:=[ofhidereadonly,ofpathmustexist];If execute thenIf ofextensiondifferent in options thenMessagedlg(‘不是扩展名为.txt的文件’,mterror,[mbok],0) ElseMemo1.lines.savetofile(filename);End;If radiobutton2.checked=true thenBeginWith savedialog1 doBeginFile:=’任何类型文件(*.*)|*.*’;Filename:=”;Options:=[ofpathmustexist];If execute thenMemo1.lines.savetofile(filename);End;End;End;Procedure tform1.button3click(sender:tobject); BeginWith fondialog1 doBeginOptions:=[fdeffects,fdtruetypeonly,fdforcefontexist]; If execute thenMemo1.font:=font;End;Procedure tform1.button4click(sender:tobject);BeginClose;End;实验8Procedure twinform.button1.checked=true thenBeginIf radiobutton1.checked=true thenBeginWith openfiledialog1 doBeginFilter:=’文本文件(*.txt)|*.txt’;Defaultext:=’txt’;Filename:=”;Restoredirectory:=true;If showdialog=system.windos.forms.dialogresult.ok thenRich textbox1.loadfile(filename,richtextboxstreamtype.plaintext); End;End;If radiobutton2.checked=true thenWith optionfiledialog1 doBeginFilename:=’任何类型文件(*.*)|*.*‘;Filename:=”;Restoredictory:=true;If showdialog=system.windows.forms.dialogresult.ok thenRichtextbox1.loadfile(filename,richtextboxstreamtype.plaintext);End;End;End;Proceduretwinform.button2_click(sender:system.object;e:system.eventargs);BeginWith savefiledialog1.doBeginFilter:=’文本文件’(*.txt)|*.txt’;Defaultext:=’txt’;Filename:=”;If showdialog=system.windows.forms.dialogresult.ok thenRichtextbox1.savefile(filename,richtextboxstreamtype.plaintext);End;End;If radiobutton2.checked=true thenBeginWith savefiledialog1 doBeginFilter:=’任何类型文件’(*.*)|*.*’;Filename:=”;If showdialog=system.windows.forms.dialogresult.ok thenRichtextbox1.savefile(filename,richtextboxstreamtype.plaintext);End;End;End;Proceduretwinform.button3_chick(sender:system.object;e:system.eventargs);BeginWith fontdialog1 do beginIf showdialog=system.windows.forms.dialogresult.ok thenRichtextbox1.font:=font;End;End;Proceduretwinform.button4_click(sender:system.object;e:system.eventargs);BeginClose;End;实验9Procedure tform1.new1click(sender:tobject);BeginForm1.caption:=’文档1’;Txtname1:= ’文档1’;Form1.richedit1.lines.clear;End;Procedure tform1.open1click(sender:tobject);BeginIf form1.opendialog1.execute thenBeginForm1.richedit1.lines.loadfromfile(form1Opendialog1.filename);Form1.caption:=form1.optiondialog1.filename;End;End;Procedure tform1.save1click(sender:tobject); BeginIf richedit1.modified thenIf form1.caption<>’文档1’thenForm1.richedit1.lines.savetofile(txtname) Else if form1.savedialog1.execute then BeginTxtname:=form1.savedialog1.filename; Form1.richedit1.lines.savetofile(txtname); End;End;Procedure tform1.exit1click(sender:tobject); BeginClose;End;Procedure tform1.printclick(seder:tobject); BeginForm1.printdialog1.execute;End;Procedure tform1.cut1click(sender:tobject); BeginForm1.richedit1.undo;Procedure tform1.cut1click(sender:tobject);BeginForm1.richedit1.cuttoclpboard;End;Procedure tform1.copyclick(sender:tobject);BeginForm1.richedit1.copytoclipboard;End;Proceduretform1.richedit1.paste1click(sender:tobject); BeginForm1.richedit1.pastefromclipboard;End;Procedure tform1.replacedialog1.extcute;End;Procedure tform1.toolbutton1click(sender:tobject); BeginForm1.caption:=’文档1’;Txtname:= ’文档1’;Form1.richedit1.lines.clear;End;Procedure tform1.toolbutton2click(sender:object);If form1.opendialog1.execute thenBeginIf form1.opendialog1.execute thenBeginForm1.richedit1.lines.loadfromfile(form1.opendialog1.filename); Form1.caption:=form1.opendialog1.filename;End;End;Procedure tform1.toolbutton1click(sender:tobject);BeginIf richedit1.modified thenIf form1.caption<>’文档1’thenForm1.richedit1.lines.savetofile(txtname);Else if form1.savedialog1.execute thenBeginTxtname:=form1.savedialog1.filename;Form1.richedit1.lines.savetofile(txtname);End;End;Procedure tform1.toolbutton4click(sender:tobject);BeginForm1.printdialog1.execute;End;Procedure tform1.toolbutton5click(sender:tobject); BeginForm1.richedit1.cuttoclipboard;End;Procedure tform1.toolbutton6click(sender:tobject); BeginForm1.richedit1.copytoclipboard;End;Procedure tform1.toolbutton7click(sender:tobject); BeginForm1.richedit1.paste1fromclipboard;End;Procedure tform1.toolbutton8click(sender:tobject); BeginForm1.richedit1.font.style:=[fsbold];End;Procedure tform1.toolbutton9click(sender:tobject); BeginForm1.richedit1.font.style:=[fsunderline];End;Procedure tform1.toolbutton10click(sender:tobject);BeginForm1.richedit1.font.style:=[fsitalic];End;Procedure tform1.formcreat(sender:tobject);BeginForm1.caption:=’文档1’;txtname:=’文档1’;form1.statusbar1.panels[0].text:=txtname;form1.statusbar1.panels[1].text:=timetostr;form1.statusbar1.panels[2].text:=datetostr;end;实验十Proceduretform.menultem2_click(sender:system.object;e:system.eventargs);BeginText:=’文档1’;Txtname:=’文档1’;Richtextbox1.clear;End;Proceduretform.menultem3_click(sender:system.object;e:system.eventargs);BeginIf openfiledialog1.showdialog=system.windows.forms.dialogrsult.ok thenBeginRichtextbox1.loadfile(openfiledialog1.filename,richtextboxstreamty pe.plaintext);Text:=openfiledialog1.filename;End;End;Proceduretform.menultem4_click(sender:system.object;e:system.eventargs);BeginIf richtextbox1.modidied thenIf text<>’文档1’thenRichtextbox1.savefile(txtname);Else if savefiledialog1.showdialog=system.wimdows.forms.dialogresutl.ok then BeginTxtname:=savefiledialog1.filename;Richtextbox1.savefile(txtname);End;End;Proceduretform.menultem6_click(sender:system.object;e:system.eventargs);BeginClose;End;Proceduretform.menultem8_click(sender:system.object;e:system.eventargs);BeginRichtextbox1.undo;End;Proceduretform.menultem9_click(sender:system.object;e:system.eventargs);BeginRichtextbox1.cut;End;Proceduretform.menultem10_click(sender:system.object;e:system.eventargs);BeginRichtextbox1.copy;End;Proceduretform.menultem11_click(sender:system.object;e:system.eventargs);BeginRichtextbox1.paste;End;Proceduretwinform.twform_load(sender:system.object;e:system.event);BeginText:=’文档1’;txtname:=’文档1’;statusbar1.panels[0].text:=txtname;statusbar1.panels[1].text:=timetostr(time);statusbar1.panels[0].text:=datetostr(date);end;实验十一Procedure tformmainMminewexecute(sender:tobject);BeginControlbar1.visible:=false;Statusbar1.visible:=tfrmeditor.create(self);Procedure tformmaintbopenclick (sender:tobject);beginopendialog1.execute;if opendialog1.filename<>”thembegincontrobar1.visible:=false;statusbar1.visible:=false;with tformeditor.create(self)doopenfile(opendialog1.filename);end;end;Procedure tformmainmmihelpextcute(sender:tobject);consthelp_tab=15;contents_active=-3;beginapplication.helpcommand(help_tab,contents_active); end;application.helpcommand(help_tab,contents_active); end;procedure tfmmain.mmiaboutexecute(sender:tobject);beginwith taboutbox.create(self)dotryshowmodal;finallyfree;end;end;procedure tfrmmain.formtreate(sender.tobjec);beginapplication.onhint:=showhint;application.helpfile:=exetracefilepath(application.exename)+textedi tor.hlp’;end;。
简易文本编辑器(Delphi源码)
简易⽂本编辑器(Delphi源码)设计⼀个简易的⽂本编辑器,具有创建、编辑、保存普通⽂本⽂件的功能。
unit U nit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Panel1: TPanel;Memo1: TMemo;OpenDialog1: TOpenDialog;SaveDialog1: TSaveDialog;Button1: TButton;Button2: TButton;Button3: TButton;Button4: TButton;Button5: TButton;Button6: TButton;Button7: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button4Click(Sender: TObject);procedure Button5Click(Sender: TObject);procedure Button6Click(Sender: TObject);procedure Button7Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;varf:textfile;wfilename:string;flag:boolean;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginassignfile(f,'newfile.txt');form1.Caption:='newfile.txt';wfilename:='newfile.txt';rewrite(f);memo1.Enabled:=true;button1.Enabled:=false;button2.Enabled:=false;button3.Enabled:=false;button4.Enabled:=true;button5.Enabled:=true;button6.Enabled:=true;button7.Enabled:=true;closefile(f);end;procedure TForm1.Button2Click(Sender: TObject);varline:string;beginif opendialog1.Execute thenbeginassignfile(f,opendialog1.FileName);reset(f);form1.Caption:=opendialog1.FileName;memo1.Enabled:=false;flag:=true;while not eof(f) dobeginreadln(f,line);memo1.Lines.Add(line);end;button1.Enabled:=false;button2.Enabled:=false;button3.Enabled:=false;button4.Enabled:=false;button5.Enabled:=false;button6.Enabled:=true;button7.Enabled:=true;closefile(f);end;end;procedure TForm1.Button3Click(Sender: TObject); varline:string;beginif opendialog1.Execute thenbeginwfilename:=opendialog1.FileName;assignfile(f,wfilename);form1.Caption:=wfilename;reset(f);memo1.Enabled:=true;while not eof(f) dobeginreadln(f,line);memo1.Lines.Add(line);end;button1.Enabled:=false;button2.Enabled:=false;button3.Enabled:=false;button4.Enabled:=true;button5.Enabled:=true;button6.Enabled:=true;button7.Enabled:=true;closefile(f);end;end;procedure TForm1.Button4Click(Sender: TObject); vari,lastline:integer;beginlastline:=memo1.Lines.Add('')-1;assignfile(f,wfilename);rewrite(f);for i:=0 to lastline dobeginwrite(f,memo1.lines[i]);end;closefile(f);end;procedure TForm1.Button5Click(Sender: TObject); vari,lastline:integer;writefile:textfile;beginif savedialog1.Execute thenbeginwfilename:=savedialog1.FileName;assignfile(writefile,wfilename);rewrite(writefile);form1.Caption:=wfilename;lastline:=memo1.Lines.Add('')-1;for i:=0 to lastline dobeginwriteln(writefile,memo1.lines[i]);end;closefile(writefile);end;end;procedure TForm1.Button6Click(Sender: TObject);vari:integer;beginif not flag theni:=messagedlg('save file ?',mtConfirmation,mbYesNoCancel,0); if i <> 2 thenbeginif i=6 thenButton4Click(sender);memo1.Clear;form1.Caption:='';memo1.Enabled:=false;button1.Enabled:=true;button2.Enabled:=true;button3.Enabled:=true;button4.Enabled:=false;button5.Enabled:=false;button6.Enabled:=false;button7.Enabled:=true;end;flag:=false;end;procedure TForm1.Button7Click(Sender: TObject);beginclose;end;end.。
delphi记事本开发
Delphi程序设计第三节模仿制作windows 计算器首先:我们设置FORM1的窗口属性这样窗口就不可以随便拉大拉小了!用选中计算器的图标!把FORM1的caption属性设置为模仿的windows 计算器。
画一个EDIT组建(),并把他的TEXT属性里的字删除!使TEXT里边显示为空!好了,画好了,我们接着来画面板!点一下(panel)该组件只是为了好看没有多大的实际用途!拉到适当的大小的时候我们来设置他的属性!首先,我们把他的这个属性清空,是面板上不显示字!把设置为使面板下沉我们再画18个快速按钮,分别把CAPTION属性设置成如图好了,到此,我们的界面就画完了!我们先来讲讲本节用到的组件属性!Form1.caption (代表是FORM1的标题文本),我们可以在属性页里设置也可以用语句设置;Delphi里所有的组件属性都可以用语句设置,以后我就不重复了!例:form1.caption:=’哈哈,测试一下!’; //这时候则form1的标题为哈哈,测试一下!Edit1.text:=’看看你聪明不!’;//这时候edit1里边显示的文本为看看你聪明不!Panel 面板组件,无实际用途,主要用于界面美观·!Speedbutton 快速按钮!主要属性:checked 设置是否被选中flat 设置是否为OFFICE 2000风格Down 设置是否按下好了,接下来我们写代码!重点难点解析:edit1.text:=edit1.text + '1'; 此句话的意思是代表在原来的字符串后面加个字符串1例:假如说原来的edit1.text 为字符串123456 在执行上面的语句的话,edit1.text 的值则为1234561speedbutton12.caption = '关闭' 注意:等于号在Delphi中代表比较的意思,就是说此句的意思是比较speedbutton12.caption 的值和字符串关闭是不是相等的!case flag of1:temp2:=strtofloat(edit1.text)+ strtofloat(temp1);2:temp2:=strtofloat(temp1) - strtofloat(edit1.text);3:temp2:=strtofloat(edit1.text)* strtofloat(temp1);4:temp2:strtofloat(temp1) / strtofloat(edit1.text);end;edit1,text:=floattostr(temp2);strtofloat 把字符串转换为浮点数floattostr 和上面的相反case flag of 当FLAG的值和下面的符合时运行后面的表达式…..。
用delphi编写的记事本代码.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls, ComCtrls, ExtCtrls, ToolWin, ImgList; type TForm1 = class(TForm MainMenu1: TMainMenu; RichEdit1: TRichEdit; OpenDialog1: TOpenDialog; SaveDialog1: TSaveDialog; N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; FontDialog1: TFontDialog; N4: TMenuItem; N5: TMenuItem; N6: TMenuItem; N7: TMenuItem; N8: TMenuItem; N9: TMenuItem; N10: TMenuItem; N11: TMenuItem; N12: TMenuItem; N13: TMenuItem; N14: TMenuItem; N15: TMenuItem; N16: TMenuItem; N17: TMenuItem; N18: TMenuItem; N19: TMenuItem; N20: TMenuItem; PopupMenu1: TPopupMenu; FindDialog1: TFindDialog; N21: TMenuItem; PrintDialog1: TPrintDialog; N22: TMenuItem; N23: TMenuItem; N24: TMenuItem; N25: TMenuItem; N26: TMenuItem; N27: TMenuItem; N28: TMenuItem; N31: TMenuItem; N32: TMenuItem; S1: TMenuItem; Timer1: TTimer; StatusBar2: TStatusBar; ToolBar1: TToolBar; ToolButton1: TToolButton; ToolButton2: TToolButton; ToolButton3: TToolButton; ToolButton4: TToolButton; ToolButton5: TToolButton; ToolButton6: TToolButton; ToolButton7: TToolButton; ToolButton8: TToolButton; ToolButton9: TToolButton; ToolButton10: TToolButton; ToolButton11: TToolButton; ToolButton12: TToolButton; ToolButton13: TToolButton; ImageList1: TImageList; procedureN2Click(Sender: TObject; procedure N3Click(Sender: TObject; procedureN7Click(Sender: TObject; procedure N8Click(Sender: TObject; procedureN9Click(Sender: TObject; procedure N14Click(Sender: TObject; procedureN21Click(Sender: TObject; procedure N16Click(Sender: TObject; procedureN17Click(Sender: TObject; procedure N18Click(Sender: TObject; procedureN12Click(Sender: TObject; procedure N20Click(Sender: TObject; procedureN6Click(Sender: TObject; procedure S1Click(Sender: TObject; procedureFindDialog1Find(Sender: TObject; procedure Timer1Timer(Sender: TObject; procedure N24Click(Sender: TObject; procedure N25Click(Sender: TObject; procedureN26Click(Sender: TObject; procedure N27Click(Sender: TObject; procedureN28Click(Sender: TObject; procedure RichEdit1MouseMove(Sender: TObject; Shift:TShiftState; X, Y: Integer; procedure RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState; procedure RichEdit1Enter(Sender: TObject; private { Private declarations } public { Public declarations } end; var Form1: TForm1; filename: string; temp:integer; implementation {$R *.dfm} procedure TForm1.N2Click(Sender: TObject; begin if opendialog1.Execute then begin Form1.Caption:=ExtractFileName(OpenDialog1.FileName;richedit1.lines.LoadFromFile(opendialog1.FileName; end end; procedureTForm1.N3Click(Sender: TObject; begin if filename='' then begin if savedialog1.Execute then begin richedit1.Lines.SaveToFile(savedialog1.FileName;filename:=savedialog1.filename; end end elserichedit1.Lines.SaveToFile(savedialog1.FileName; end; procedureTForm1.N7Click(Sender: TObject; begin fontdialog1.Execute;richedit1.SelAttributes.Assign(fontdialog1.Font; end; procedure TForm1.N8Click(Sender: TObject; begin if RichEdit1.Modified then begin temp:=MessageDlg('内容发生改变,是否要保存',mtwarning,mbyesnocancel,0; if temp=6 then begin Form1.N3Click(Sender ; RichEdit1.Lines.Clear; end; if temp=7 then RichEdit1.Lines.Clear; end elseRichEdit1.Lines.Clear; end; procedure TForm1.N9Click(Sender: TObject; begin with savedialog1 do if execute then begin richedit1.Lines.SaveToFile (filename;opendialog1.FileName := filename; end; end; procedure TForm1.N14Click(Sender: TObject; begin richedit1.Perform(EM_UNDO,0,0; end; procedureTForm1.N21Click(Sender: TObject; begin richedit1.SelectAll; end; procedureTForm1.N16Click(Sender: TObject; begin richedit1.cuttoclipboard; end; procedure TForm1.N17Click(Sender: TObject; begin richedit1.copyToClipboard; end; procedure TForm1.N18Click(Sender: TObject; begin richedit1.PasteFromClipboard; end; procedure TForm1.N12Click(Sender: TObject; begin PrintDialog1.Execute; end; procedureTForm1.N20Click(Sender: TObject; begin finddialog1.Execute; end; procedureTForm1.N6Click(Sender: TObject; begin if N6.Checked then begin N6.Checked:=false; RichEdit1.WordWrap:=False; end else begin n6.Checked:=True;RichEdit1.WordWrap:=True; end end; procedure TForm1.S1Click(Sender: TObject; begin if s1.Checked then begin s1.Checked:=false; statusbar2.visible:=False; end else begin s1.Checked:=True; statusbar2.visible:=True; end end; procedureTForm1.FindDialog1Find(Sender: TObject; Var findat:longint; startpos,toend:integer; begin with richedit1 do begin if sellength<>0 then startpos:=selstart+sellength else startpos:=0; toend:=length(text-startpos;findat:=findtext(FindDialog1.FindText,startpos,toend,[stMatchCase]; if findat<>-1 then begin setfocus ; SelStart:=findat; SelLength:=Length(FindDialog1.FindText end; end; end; procedure TForm1.Timer1Timer(Sender: TObject; beginstatusbar2.Panels[1].Text:=timetostr(time(; end; procedure TForm1.N24Click(Sender: TObject; begin richedit1.Perform(EM_UNDO,0,0; end; procedureTForm1.N25Click(Sender: TObject; begin richedit1.cuttoclipboard; end; procedure TForm1.N26Click(Sender: TObject; begin richedit1.copyToClipboard; end; procedure TForm1.N27Click(Sender: TObject; begin richedit1.PasteFromClipboard; end; procedure TForm1.N28Click(Sender: TObject; begin richedit1.SelectAll; end; procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer; begin StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+1+'行'+inttostr(RichEdit1.CaretPos.X+1+'列' end; procedureTForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState; begin if Key=13 then StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+2+'行'+'1列' else StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+1+ '行'+inttostr(RichEdit1.CaretPos.X+2+'列' end; procedure TForm1.RichEdit1Enter(Sender: TObject; begin StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+1+'行'+inttostr(RichEdit1.CaretPos.X+1+'列' end; end.。
Delphi常用代码(个人自用整理2)
Delphi常⽤代码(个⼈⾃⽤整理2)3.Delphi 实现⽂件加密源码unit main;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,ShellAPI,inifiles,registry,Des ,FileCtrl, jpeg, ExtCtrls;typeTBruce = class(TForm)dlgOpen1: TOpenDialog;Image1: TImage;Button2: TButton;Edit1: TEdit;Label1: TLabel;Button1: TButton;Label2: TLabel;Edit2: TEdit;Button3: TButton;Label3: TLabel;Edit3: TEdit;Button5: TButton;procedure FormCreate(Sender: TObject);procedure Button5Click(Sender: TObject);procedure JiaMI(var FilePath,MiMa:string);procedure JieMi(var DianFile:string);procedure ZhuCeCaiDan;procedure DelZhuCe;procedure Button1Click(Sender: TObject);procedure Edit2Exit(Sender: TObject);procedure FormShow(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varBruce: TBruce;FilePahtName :string;sRoot:WideString;sCaption:string;implementationuses Unit2;{$R *.dfm}DianFile,IniPath:string;inifile: TInifile;beginDianFile:=FilePath+'..\';iniPath:='dat.ini';inifile:=TIniFile.Create(IniPath);inifile.WriteString('111','key',DES.EncryStrHex(MiMa,'FEIGEW')); inifile.Free;renamefile(FilePath,DianFile);end;procedure TBruce.FormCreate(Sender: TObject);vari: Integer;beginZhuCeCaiDan;for i := 1 to ParamCount dobeginif LowerCase(ParamStr(i)) <> '' then beginFilePahtName:= ParamStr(i);end;end;end;procedure TBruce.JieMi(var DianFile:string);varFileName,FileJD:string;i:Integer;beginFileName:=DianFile+'.\';I:=Length(FileName);FileJD:=Copy(FileName,1,i-3);renamefile(FileName,FileJD);end;procedure TBruce.ZhuCeCaiDan;vara:TRegistry;begina:=TRegistry.create;a.rootkey:=HKEY_CLASSES_ROOT;if a.openkey('Folder\Shell\cutbig',true) thena.closekey;end;if a.openkey('Folder\Shell\cutbig\command',true) thenbegin//command⼦键的内容是点击右键后选择相应项后要运⾏的程序;//%1是在单击右键时选中的⽂件名a.writestring('','"'+Application.exeName+'" "%1"');a.closekey;end;a.free;end;procedure TBruce.DelZhuCe;vara:TRegistry;begina:=TRegistry.create;a.rootkey:=HKEY_CLASSES_ROOT;//⽤deletekey删除⼀个主键,其所包含的⼦键也被删除,如果已⽆此主键,运⾏删除操作不会带来别的危害a.deletekey('Folder\Shell\cutbig');a.free;Application.MessageBox('成功删除系统右键菜单!', '成功', MB_OK +MB_ICONINFORMATION);end;procedure TBruce.Button5Click(Sender: TObject);beginapplication.Terminate;end;procedure TBruce.Button1Click(Sender: TObject);varpassword:string;beginif FilePahtName='' then beginApplication.MessageBox('没有可加密的⽂件夹,请从⽂件夹上右键启动加密程序!','系统', MB_OK + MB_ICONWARNING);//Application.Terminate;end;if (Edit1.Text='') or (Edit2.text='') then beginApplication.MessageBox('密码不能为空,请输⼊你的密码!', '系统', MB_OK +MB_ICONWARNING);Exit;end;if Edit2.Text=Edit1.Text then beginpassword:=Edit2.Text;JiaMI(FilePahtName,password);Application.MessageBox('⽂件夹添加密码成功,请牢记您的密码!', '系统', MB_OK+ MB_ICONINFORMATION);//Application.Terminate;end else beginend;end;procedure TBruce.Edit2Exit(Sender: TObject);beginif Edit2.Text<>Edit1.Text then beginApplication.MessageBox('两次输⼊的密码不⼀致,请重新输⼊!', '系统', MB_OK + MB_ICONWARNING);Edit2.SetFocus;end;end;procedure TBruce.FormShow(Sender: TObject);beginEdit1.SetFocus;end;procedure TBruce.Button2Click(Sender: TObject);begin{ FilePahtName:='';if not dlgOpen1.Execute then exit;FilePahtName:=dlgOpen1.FileName;ShowMessage(FilePahtName);}sCaption := '⽂件夹'; //弹出框标题名(⾮弹出框窗体名)sRoot := ''; //初始⽂件夹(如'C:\','D:\DownLoad'等, 不存在则从桌⾯)beginif SelectDirectory(sCaption, sRoot, FilePahtName) then//已返回所选⽂件夹路径给FilePahtName,⾃⾏处理end;ShowMessage(FilePahtName);end;procedure TBruce.Button3Click(Sender: TObject);varlen:Integer;inifile: TInifile;IniPath,password,sstemp:string;beginBruce.JieMi(FilePahtName);len:=Length(FilePahtName);sstemp:=Copy(FilePahtName,1,len-1);iniPath:='dat.ini';inifile:=TIniFile.Create(IniPath);password:=inifile.ReadString('111','key','');password:=des.DecryStrhex(password,'FEIGEW');inifile.Free;Bruce.JiaMI(sstemp,password);if Edit3.Text=password then beginBruce.JieMi(FilePahtName);DeleteFile(IniPath) ;Application.MessageBox('您输⼊的密码错误,请重新输⼊密码!', '系统', MB_OK +MB_ICONWARNING);Edit3.SetFocus;end;end;end.⼀、“软件限时注册”控件(TShareRegist)功能说明:简介:⼀、1、只要将该控件加⼊主窗体,使Action属性设为true,再设置其它属性,你的软件就有限时注册的功能了,只要你牢记三个密钥,并加上给你的算号软件(RegObject_Admin.exe)你就可以⽆敌天下了。
记事本代码
记事本代码#include<iostream.h>#include<string.h>#include<ctype.h> //为了以下使用isdigit(string)函数作铺垫typedef struct node{char a[100]; //每行100字符node * next; //关于此处next的作用还不清楚,但不可去掉}node;class notepad{public:notepad(){i=1;line=0;}~notepad(){}void operator_interface();void input();void ct_input();void delete1();void copy();void paste();void open();void save();char * find();void print();char store[100]; //储存需复制内容private:char * ptr_array[100]; //指针数组,记录100行行指针int linelen[100]; //最大100行int line; //当前总行数char d[30]; //记录操作数据int k,l; //记录当前查找行ilint i; //文档录入初始标记};void notepad::operator_interface(){cout<<"********************************************************"<<en dl;cout<<"***0.继续录入文档"<<endl;cout<<"***1.输入文档内容"<<endl;cout<<"***2.删除某些内容"<<endl;cout<<"***3.复制某些内容"<<endl;cout<<"***4.粘贴某些内容"<<endl;cout<<"***5.打开文档内容"<<endl;cout<<"***6.是否保存文档"<<endl;cout<<"***7.获取操作帮助"<<endl;cout<<"***8.我要结束操作"<<endl;cout<<"********************************************************"<<en dl;}void notepad::input(){-99"<<endl; cout<<"输入总行数,格式:01char e[10];cin>>e;char *lin=e;if(*(lin+2)=='\0'&&isdigit(*lin)&&isdigit(*(lin+1))){line=(*lin-'0')*10+(*(lin+1)-'0');if(line!=0){cout<<"请输入各行内容"<<endl;while(i<=line){cout<<"第"<<i<<"行 ";node *p=new node;cin>>p->a;ptr_array[i]=p->a;linelen[i]=strlen(p->a);i++;}}else cout<<"你输入的行数有误"<<endl;}else cout<<"你输入的行数有误"<<endl; }void notepad::ct_input(){if(line!=0){int i=line+1;cout<<"输入要录入的总行数,格式:01-99"<<endl;char e[10];cin>>e;char *lin=e;if(*(lin+2)=='\0'&&isdigit(*lin)&&isdigit(*(lin+1))){ line=line+(*lin-'0')*10+(*(lin+1)-'0');if(line!=0){cout<<"请输入各行内容"<<endl;while(i<=line){cout<<"第"<<i<<"行 ";node *p=new node;cin>>p->a;ptr_array[i]=p->a;linelen[i]=strlen(p->a);i++;}}else cout<<"你输入的行数有误"<<endl;}else cout<<"你输入的行数有误"<<endl;}else cout<<"当前文档并无内容,请先输入1录入文档"<<endl; }void notepad::print(){cout<<endl<<endl;int j=1;cout<<"当前文档内容为:"<<endl;while(j<=line){cout<<"第"<<j<<"行 ";char *q=ptr_array[j];while(*q!='\0'){cout<<*q;q++;}cout<<endl;j++;}cout<<endl;}char * notepad::find(){ //暂未解决跨行查找问题k=1;cin>>d;l=strlen(d);char *n=d;int c=1;char *m=ptr_array[k];while(k<=line){if(*m=='\0'){k=k+1;if(k<=line)m=ptr_array[k];}if(*m!='\0'&&*m!=*n)m++;while(*n!='\0'&&*m!='\0'&&*m==*n){ m=m+1;n=n+1;c=c+1;}if(*n=='\0'){return m-c+1;}else {n=d;c=1;}}return NULL;}void notepad::delete1(){char * dp1;char * dp2;cout<<"请输入要删除的文本前几位字符,注意区分"<<endl;dp1=find();int l1=k;cout<<"请输入要删除的文本末几位字符,注意区分"<<endl;dp2=find();int l2=k;if(dp1==NULL||dp2==NULL||l1>l2)cout<<"输入错误"<<endl;else{dp2=dp2+l;if(l1==l2){while(*dp2!='\0'){*dp1=*dp2;dp1++;dp2++;}*dp1='\0';linelen[l1]=strlen(ptr_array[l1]);}else {if(l1+1<l2){for(intt1=l1+1,t2=l2;t2<=line;t1++,t2++)ptr_array[t1]=ptr_array[t2]; line=line-l2+l1+1;l2=l1+1;}*dp1='\0';char *dp21=ptr_array[l2];while(*(dp2-1)!='\0'){*dp21=*dp2;dp21++;dp2++;}linelen[l1]=strlen(ptr_array[l1]);linelen[l2]=strlen(ptr_array[l2]);}if(linelen[l1]==0){for(int v=l1;v<=line;v++)ptr_array[v]=ptr_array[v+1]; line--;}if(linelen[l2]==0){for(int v=l2;v<=line;v++)ptr_array[v]=ptr_array[v+1]; line--;}}}void notepad::copy(){char * cp1;char * cp2;cout<<"请输入要复制的文本前几位字符,注意区分"<<endl; cp1=find();int l1=k;cout<<"请输入要复制的文本末几位字符,注意区分"<<endl; cp2=find();int l2=k;char *pt=store;if(cp1!=NULL&&cp2!=NULL&&l1<=l2){cp2=cp2+l;while(cp1!=cp2){if(*cp1=='\0'){l1++;cp1=ptr_array[l1];}else {*pt=*cp1;pt++;cp1++;}}*pt='\0';}else cout<<"输入错误"<<endl; }void notepad::paste(){cout<<"请输入要粘贴位置的前几位字符(在首字符后粘贴)"<<endl; char *pat=find();if(pat!=NULL){int choice2;cout<<"请选择要粘贴内容:1/从内存中0/我自己输入"<<endl; cin>>choice2;if(!choice2)cin>>store;char *ppt=store;for(char *pat1=pat;*(pat1+1)!='\0';pat1++); //定位至末尾int pl=strlen(store);*(pat1+pl+1)='\0';while(pat1!=pat){ //移位*(pat1+pl)=*pat1;pat1--;}pat++;for(int u=1;u<=pl;u++){*pat=*ppt;ppt++;pat++;}linelen[k]=linelen[k]+pl;}else cout<<"输入错误"<<endl;}void notepad::open(){print();}void notepad::save(){cout<<"是否保存文件,1/是0/否"<<endl;char g[10];int choice1;cin>>g;char *choi=g;if(*(choi+1)=='\0'&&isdigit(*choi)){ choice1=*choi-'0';if(choice1==1)cout<<"文件已保存"<<endl; else if(choice1==0){for(int w=1;w<=line;w++){ //相当于析构函数的作用ptr_array[w]=NULL;linelen[w]=0;}line=0;}else cout<<"输入错误"<<endl;}else cout<<"输入错误"<<endl; }void main(){cout<<"欢迎使用本程序,您可以在要输入文档内容时通过切换输入法实现输入汉字,byhk"<<endl;notepad b;b.operator_interface();char f[10];int choice;cin>>f;char *choic=f;if(*(choic+1)=='\0'&&isdigit(*choic)){ //错误输入处理机制choice=*choic-'0';}else choice=9;while(choice!=8){switch(choice){case 0:b.ct_input();break;case 1:b.input();break;case 2:b.delete1();b.print();break;case 3:{b.copy();cout<<endl;char *p_t=b.store;int fzcd=strlen(b.store);cout<<"你所要复制的内容长度为"<<endl<<fzcd<<endl; cout<<"你所要复制的内容为"<<endl;while(*p_t!='\0'){cout<<*p_t;p_t++;}cout<<endl;}break;case 4:b.paste();b.print();break;case 5:b.open();break;case 6:b.save();break;case 7:b.operator_interface();break;case 8:break;default:break;}if(choice==9||(choice>=0&&choice<=7)){ //输入错误时的操作及输入正确时 //的继续操作判断if(choice==9)cout<<"你输入的操作有误,请重新输入,输入 7 获取操作帮助"<<endl;else cout<<"继续你的操作,输入 7 获取操作帮助"<<endl;cin>>f;choic=f;if(*(choic+1)=='\0'&&isdigit(*choic)) //错误输入处理机制choice=*choic-'0';else choice=9;}}cout<<"感谢您的使用"<<endl; }。
分享一个Delphi制作的文档编辑器源代码(仿Word)
分享⼀个Delphi制作的⽂档编辑器源代码(仿Word)
功能挺多的,就是很多地⽅都没有完善。
不过简单使⽤,是没有问题的。
我也忘记是在哪个⽹站下载的,可能是Git国外的⽹站。
仿Word⽂档编辑器,不过其实没有必要的,因为⽤户会直接使⽤微软或者其他软件来制作⾃⼰的⽂档。
但是却具有参考价值,虽然代码需要优化的地⽅还有很多。
⽐如那些添加控件这些可以去除,把代码结构处理得紧凑⼀些,⽽且增加更多可以⾃定义的地⽅就更理想了。
其实⼤家最最讨厌都是⼀些滚动条和控件背景颜⾊都⽆法⾃定义,都被写得死死的,就像原来Vcl那些玩意⼀样。
想弄个⾃定义界⾯都办不到。
这套代码也是这样,所以需要⼤家⾃⼰改。
下⾯这个是我改的,多页功能,我去掉了,改成单页,跟RichEdit这种类型⼀样。
不过完全⽀持表格、图⽚、GIF图⽚这些添加,⽽且可以⾃定义编辑框背景图⽚的添加和更换。
这样⼀来,也算是解决了⼀直以来的⼼愿。
⾄少收集和管理各种⽂档资料更加⽅便了。
在这⾥提供原来代码的下载地址,原来的忘记了:
有兴趣的,⾃⼰下载看看,⾄于我改的就不提供了。
它⾥⾯带有作者的通讯⽅式,有兴趣可以联系交流。
Eclipse开发记事本程序
Eclipse开发记事本程序1、新建项目,新建Visual Class,名称输入TextEditorApp,Style选Swing 下的Frame,选中public static void main(String[] args),完成。
2、进入JFrame中心面板,右击鼠标,设置中心面板的Layout为null布局。
3、在palette的Swing Containers下选择jScrollPane,建立滚动面板,选择JtextArea建立多行文本,重命名为textArea。
4、建立菜单栏jJMenuBar,插入四个菜单JMenu,分别改名为文件、编辑、工具、帮助;在“文件”下插入菜单项新建、打开、保存、退出;在“编辑”下插入菜单项复制、剪切、粘贴、全选、颜色;在“工具”下插入菜单项计算器、记事本;在“帮助”插入菜单项关于。
5、给“新建”菜单添加actionPerformed事件,在对应的代码处写入:textArea.setText(null);TextEditorApp.this.setTitle("无标题 - 记事本");6、给“打开”菜单添加actionPerformed事件,在对应的代码处写入:File f1;JFileChooser jfc1 = new JFileChooser();int num1 = jfc1.showOpenDialog(TextEditorApp.this);if(num1==JFileChooser.APPROVE_OPTION){try{f1 = jfc1.getSelectedFile();TextEditorApp.this.setTitle(f1.getName());FileReader fr = new FileReader(f1);BufferedReader br = new BufferedReader(fr);String str;while((str = br.readLine())!=null){textArea.setText(str);}fr.close();br.close();}catch(FileNotFoundException e1){e1.printStackTrace();}catch(IOException e2){e2.printStackTrace();}}7、给“保存”菜单添加actionPerformed事件,在对应的代码处写入:File f2 = null;JFileChooser jfc2 = new JFileChooser();int num2 = jfc2.showSaveDialog(TextEditorApp.this);if(num2==JFileChooser.APPROVE_OPTION){f2=jfc2.getSelectedFile();TextEditorApp.this.setTitle(f2.getName());try{FileWriter fw = new FileWriter(f2);BufferedWriter bw = new BufferedWriter(fw);bw.write(textArea.getText());bw.close();fw.close();}catch(IOException e2){e2.printStackTrace();}}8、给“退出”菜单添加actionPerformed事件,在对应的代码处写入:int a = JOptionPane.showConfirmDialog(TextEditorApp.this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);if(a==1){TextEditorApp.this.dispose();}else if(a==0){File f2 = null;JFileChooser jfc2 = new JFileChooser();int num2 = jfc2.showSaveDialog(TextEditorApp.this);if(num2==JFileChooser.APPROVE_OPTION){f2=jfc2.getSelectedFile();TextEditorApp.this.setTitle(f2.getName());try{FileWriter fw = new FileWriter(f2);BufferedWriter bw = new BufferedWriter(fw);bw.write(textArea.getText());bw.close();fw.close();}catch(IOException e2){e2.printStackTrace();}TextEditorApp.this.dispose();}}9、给“编辑”菜单中的复制、剪切、粘贴、全选添加actionPerformed事件,在对应的代码处分别写入:textArea.copy();textArea.cut();textArea.paste();textArea.selectAll();10、给“颜色”菜单添加actionPerformed事件,在对应的代码处写入:Color color=JColorChooser.showDialog(textArea, "选择颜色", Color.BLACK);textArea.setForeground(color);11、给“记事本”菜单添加actionPerformed事件,在对应的代码处写入:try{String command = "notepad.exe";Process child = Runtime.getRuntime().exec(command);}catch (IOException ex){}12、给“计算器”菜单添加actionPerformed事件,在对应的代码处写入:try{String command = "calc.exe";Process child = Runtime.getRuntime().exec(command);}catch (IOException ex){}13、给“关于”菜单添加actionPerformed事件,在对应的代码处写入:String message = "--------\n版本:1.0\n作者:刘艳慧" + "\n\n感谢您的使用!";JOptionPane.showMessageDialog(TextEditorApp.this, message, "关于", JOptionPane.PLAIN_MESSAGE);注意:不能识别的文件需导入包,在错误处单击,选择需导入的包文件,双击引入。
Delphi实例代码
( Word Reader - 未滨册 ) www.wordLeabharlann
program Project2_7; {$APPTYPE CONSOLE} var n:integer; c:string; begin readln(n); case n div 10 of 0..5:c:='E'; 6:c:='D'; 7:c:='C'; 8:c:='B'; 9..10:c:='A'; else c:='Error'; end; writeln(c); readln; end. 5.计算1+2+3+...+n。 program Project2_8; {$APPTYPE CONSOLE} var i,n,sum:Integer; begin readln(n); sum:=0; i:=0; repeat //repeat语句开始行 i:=i+1; sum:=sum+i; until i>=n; //repeat语句结束行 writeln(sum); readln; end. 6.当和大于1000时,执行停止。 program Project2_9; {$APPTYPE CONSOLE} var i,n,sum:Integer; begin readln(n); sum:=0; i:=0; repeat //repeat语句开始行
( Word Reader - 未滨册 )
1.在幏幕上显示9*9乘滕表。 program Project2_1eg; {$APPTYPE CONSOLE} var i,n:integer; begin for i:=1 to 9 do begin for n:=1 to i do write(i*n:3); writeln; end; readln; end. 2.帆整数值为0~127的字符在幏幕上输出。 program Project2_2n; {$APPTYPE CONSOLE} Var I:Integer; begin for I:=0 to 127 do Write(Chr(I)); //帆0~255的整数以ASCII码的形式输出 Writeln; Writeln('按下回车键<Enter>退出。'); Readln; end. 3.湂圆柱体体积。 program Project2_5; {$APPTYPE CONSOLE} const pi=3.14159; var h:integer; r,v:real; begin readln(r); //输入半径 readln(h); //输入高度 v:=pi*sqr(r)*h; //湂体积,sqr(r)为r的平方 writeln(v); readln; end. 4.百分制转五分制。
delphi中记事本功能实现
应用公共对话框、菜单组件、工具栏组件、其他可视化组件,完成菜单设计,工具栏设计,完成学生基本信息管理系统,实现学生信息的建立、增加、修改、删除、浏览、查询、保存等功能。
要求:使用记录类型处理学生信息,使用记录型文件保存学生信息(文件扩展名.dat)。
参考界面unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, Menus, FileCtrl, Mask, jpeg;typeTForm1 = class(TForm)MainMenu1: TMainMenu;N1: TMenuItem;Y1: TMenuItem;Z1: TMenuItem;X1: TMenuItem;Y2: TMenuItem;Z2: TMenuItem;V1: TMenuItem;W1: TMenuItem;X01: TMenuItem;Y3: TMenuItem;Z3: TMenuItem;PageControl1: TPageControl;TabSheet1: TTabSheet;TabSheet2: TTabSheet;GroupBox1: TGroupBox;Panel1: TPanel;Label1: TLabel;Label2: TLabel;Label3: TLabel;Label4: TLabel;Label5: TLabel; RadioButton1: TRadioButton; RadioButton2: TRadioButton; Edit1: TEdit;GroupBox2: TGroupBox; RadioButton3: TRadioButton; RadioButton4: TRadioButton; GroupBox3: TGroupBox; CheckBox1: TCheckBox; CheckBox2: TCheckBox; CheckBox3: TCheckBox; CheckBox4: TCheckBox; GroupBox4: TGroupBox; Button1: TButton;Button2: TButton;Button3: TButton;Button4: TButton;Button5: TButton; GroupBox5: TGroupBox; Button6: TButton;Button7: TButton;Button8: TButton;Button9: TButton;Button10: TButton; GroupBox6: TGroupBox; GroupBox7: TGroupBox; GroupBox8: TGroupBox; Label6: TLabel;Label7: TLabel;Edit4: TEdit;Button11: TButton;Button12: TButton; MaskEdit1: TMaskEdit; MaskEdit2: TMaskEdit; Shape1: TShape;Label8: TLabel; ComboBox1: TComboBox; ListBox1: TListBox;Image1: TImage;Edit2: TEdit;procedure FormCreate(Sender: TObject);procedure ListBox1Click(Sender: TObject);procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button4Click(Sender: TObject);procedure Button5Click(Sender: TObject);procedure Button6Click(Sender: TObject);procedure Button7Click(Sender: TObject);procedure Button8Click(Sender: TObject);procedure Button9Click(Sender: TObject);procedure Button10Click(Sender: TObject);procedure Button11Click(Sender: TObject);procedure X1Click(Sender: TObject);procedure Y2Click(Sender: TObject);procedure Z2Click(Sender: TObject);procedure V1Click(Sender: TObject);procedure W1Click(Sender: TObject);procedure X01Click(Sender: TObject);procedure Y3Click(Sender: TObject);procedure Z3Click(Sender: TObject);procedure Button12Click(Sender: TObject); private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;typestudent=recordno:string[4];xm,sr,dh,mz:string[20];xb:string[2];ah:string[30];end;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);vart:student;f:file of student;beginassignfile(f,'student.dat');if fileexists('student.dat') thenreset(f)elserewrite(f);while not eof(f) dobeginread(f,t);listbox1.Items.add(t.no+' '+t.xm+' '+t.xb+' '+t.mz+' '+t.sr+' '+t.dh+' '+t.ah); end;closefile(f);button2.Enabled:=false;button3.Enabled:=false;button4.Enabled:=false;end;procedure TForm1.ListBox1Click(Sender: TObject); //是否选中记录beginif listbox1.ItemIndex>-1 thenbeginbutton2.Enabled:=true;button3.Enabled:=true;button4.Enabled:=true;endelsebeginbutton2.Enabled:=false;button3.Enabled:=false;button4.Enabled:=false;end;end;procedure TForm1.Button1Click(Sender: TObject); //添加记录var t:student;f:file of student;size:integer;s:string;begins:='';t.no:=edit2.Text;t.xm:=edit1.Text ;t.sr:=maskedit1.Text ;t.dh:=maskedit2.Text ;if radiobutton1.Checked then t.xb:='男' else t.xb:='女';if radiobutton3.Checked then t.mz:='汉族' else t.mz:='少数民族';if checkbox1.Checked then s:=s+'钓鱼、';if checkbox2.Checked then s:=s+'读书、';if checkbox3.Checked then s:=s+'下棋、';if checkbox4.Checked then s:=s+'打球、';t.ah:=copy(s,0,length(s)-2);assignfile(f,'student.dat');reset(f);size:=filesize(f);seek(f,size);write(f,t);listbox1.Items.Clear ;seek(f,0);while not eof(f) dobeginread(f,t);listbox1.Items.add(t.no+' '+t.xm+' '+t.xb+' '+t.mz+' '+t.sr+' '+t.dh+' '+t.ah); end;closefile(f);edit1.text:='';edit2.text:='';maskedit1.Text:='';maskedit2.Text:='';radiobutton1.Checked:=false;radiobutton2.Checked:=false;radiobutton3.Checked:=false;radiobutton4.Checked:=false;checkbox1.Checked:=false;checkbox2.Checked:=false;checkbox3.Checked:=false;checkbox4.Checked:=false;end;procedure TForm1.Button2Click(Sender: TObject); //删除记录var pos:integer;t:student;f:file of student;beginpos:=listbox1.ItemIndex;assignfile(f,'student.dat');reset(f);seek(f,pos+1);while not eof(f) dobeginread(f,t);seek(f,pos);pos:=pos+1;write(f,t);seek(f,pos+1);end;seek(f,pos);truncate(f);seek(f,0);listbox1.Items.Clear;while not eof(f) dobeginread(f,t);listbox1.Items.add(t.no+' '+t.xm+' '+t.xb+' '+t.mz+' '+t.sr+' '+t.dh+' '+t.ah); end;closefile(f);end;procedure TForm1.Button3Click(Sender: TObject); //修改记录var t:student;f:file of student;pos:integer;s:string;beginpos:=listbox1.ItemIndex;assignfile(f,'student.dat');reset(f);seek(f,pos);read(f,t);edit2.Text:=t.no;edit1.Text:=t.xm;maskedit1.Text:=t.sr;maskedit2.Text:=t.dh;s:=t.ah;if t.xb='男' thenradiobutton1.Checked:=trueelseradiobutton2.Checked:=true;if t.mz='汉族' thenradiobutton3.Checked:=trueelseradiobutton4.Checked:=true;if(copy(s,0,4)='钓鱼') thencheckbox1.Checked:=true;if( copy(s,0,4)='读书') or (copy(s,7,4)='读书' ) thencheckbox2.Checked:=true;if( copy(s,0,4)='下棋') or (copy(s,7,4)='下棋') or (copy(s,13,4)='下棋') thencheckbox3.Checked:=true;if( copy(s,0,4)='打球') or (copy(s,7,4)='打球') or (copy(s,13,4)='打球') or (copy(s,19,4)='打球') thencheckbox3.Checked:=true;closefile(f);end;procedure TForm1.Button4Click(Sender: TObject); //保存修改var t:student;f:file of student;pos:integer;s:string;beginpos:=listBox1.ItemIndex;assignfile(f,'student.dat');reset(f);seek(f,pos-1);read(f,t);s:='';t.no:=edit2.Text;t.xm:=edit1.Text;t.sr:=maskedit1.Text;t.dh:=maskedit2.Text;if radiobutton1.Checked then t.xb:='男' else t.xb:='女';if radiobutton3.Checked then t.mz:='汉族' else t.mz:='少数民族';if checkbox1.Checked then s:=s+'钓鱼、';if checkbox2.Checked then s:=s+'读书、';if checkbox3.Checked then s:=s+'下棋、';if checkbox4.Checked then s:=s+'打球、';t.ah:=copy(s,0,length(s)-2);write(f,t);listbox1.Items.Clear ;seek(f,0);while not eof(f) dobeginread(f,t);listbox1.Items.add(t.no+' '+t.xm+' '+t.xb+' '+t.mz+' '+t.sr+' '+t.dh+' '+t.ah); end;closefile(f);end;procedure TForm1.Button5Click(Sender: TObject); //显示所有var t:student;f:file of student;beginassignfile(f,'student.dat');if fileexists('student.dat') thenreset(f)elserewrite(f);listbox1.Items.Clear;seek(f,0);while not eof(f) dobeginread(f,t);listbox1.Items.add(t.no+' '+t.xm+' '+t.xb+' '+t.mz+' '+t.sr+' '+t.dh+' '+t.ah); end;closefile(f);tabsheet1.Show;end;procedure TForm1.Button6Click(Sender: TObject); //第一条var t:student;f:file of student;s:string;beginassignfile(f,'student.dat');reset(f);seek(f,0);read(f,t);edit2.Text:=t.no;edit1.Text:=t.xm;maskedit1.Text:=t.sr;maskedit2.Text:=t.dh;if t.xb='男' thenradiobutton1.Checked:=trueelseradiobutton2.Checked:=true;if t.mz='汉族' thenradiobutton3.Checked:=trueelseradiobutton4.Checked:=true;if(copy(s,0,4)='钓鱼') thencheckbox1.Checked:=true;if( copy(s,0,4)='读书') or (copy(s,7,4)='读书' ) thencheckbox2.Checked:=true;if( copy(s,0,4)='下棋') or (copy(s,7,4)='下棋') or (copy(s,13,4)='下棋') thencheckbox3.Checked:=true;if( copy(s,0,4)='打球') or (copy(s,7,4)='打球') or (copy(s,13,4)='打球') or (copy(s,19,4)='打球') thencheckbox3.Checked:=true;closefile(f);end;procedure TForm1.Button7Click(Sender: TObject); //上一条var t:student;f:file of student;pos:integer;s:string;beginpos:=listbox1.ItemIndex;assignfile(f,'student.dat');reset(f);//seek(f,pos-1);//read(f,t);if pos-1=-1 thenshowmessage('已经是第一条记录,无法再查找上一条记录!')elsebeginseek(f,pos-1);read(f,t);edit2.Text:=t.no;edit1.Text:=t.xm;maskedit1.Text:=t.sr;maskedit2.Text:=t.dh;if t.xb='男' thenradiobutton1.Checked:=trueelseradiobutton2.Checked:=true;if t.mz='汉族' thenradiobutton3.Checked:=trueelseradiobutton4.Checked:=true;if(copy(s,0,4)='钓鱼') thencheckbox1.Checked:=true;if( copy(s,0,4)='读书') or (copy(s,7,4)='读书' ) thencheckbox2.Checked:=true;if( copy(s,0,4)='下棋') or (copy(s,7,4)='下棋') or (copy(s,13,4)='下棋') thencheckbox3.Checked:=true;if( copy(s,0,4)='打球') or (copy(s,7,4)='打球') or (copy(s,13,4)='打球') or (copy(s,19,4)='打球') thencheckbox3.Checked:=true;end;closefile(f);end;procedure TForm1.Button8Click(Sender: TObject); //下一条var t:student;f:file of student;pos:integer;s:string;beginpos:=listbox1.ItemIndex;assignfile(f,'student.dat');reset(f);seek(f,pos+1);read(f,t);if pos+1=filesize(f) thenshowmessage('已经是最后一条记录,无法再查找下一条记录!')elsebeginseek(f,pos+1);read(f,t);edit2.Text:=t.no;edit1.Text:=t.xm;maskedit1.Text:=t.sr;maskedit2.Text:=t.dh;if t.xb='男' thenradiobutton1.Checked:=trueelseradiobutton2.Checked:=true;if t.mz='汉族' thenradiobutton3.Checked:=trueelseradiobutton4.Checked:=true;if(copy(s,0,4)='钓鱼') thencheckbox1.Checked:=true;if( copy(s,0,4)='读书') or (copy(s,7,4)='读书' ) thencheckbox2.Checked:=true;if( copy(s,0,4)='下棋') or (copy(s,7,4)='下棋') or (copy(s,13,4)='下棋') thencheckbox3.Checked:=true;if( copy(s,0,4)='打球') or (copy(s,7,4)='打球') or (copy(s,13,4)='打球') or (copy(s,19,4)='打球') thencheckbox3.Checked:=true;end;closefile(f);end;procedure TForm1.Button9Click(Sender: TObject); //最后一条var t:student;f:file of student;size:integer;s:string;beginassignfile(f,'student.dat');reset(f);size:=filesize(f);seek(f,size-1);read(f,t);edit2.Text:=t.no;edit1.Text:=t.xm;maskedit1.Text:=t.sr;maskedit2.Text:=t.dh;if t.xb='男' thenradiobutton1.Checked:=trueelseradiobutton2.Checked:=true;if t.mz='汉族' thenradiobutton3.Checked:=trueelseradiobutton4.Checked:=true;if(copy(s,0,4)='钓鱼') thencheckbox1.Checked:=true;if( copy(s,0,4)='读书') or (copy(s,7,4)='读书' ) thencheckbox2.Checked:=true;if( copy(s,0,4)='下棋') or (copy(s,7,4)='下棋') or (copy(s,13,4)='下棋') thencheckbox3.Checked:=true;if( copy(s,0,4)='打球') or (copy(s,7,4)='打球') or (copy(s,13,4)='打球') or (copy(s,19,4)='打球') thencheckbox3.Checked:=true;closefile(f);end;procedure TForm1.Button10Click(Sender: TObject); //退出系统beginform1.Close;end;procedure TForm1.Button11Click(Sender: TObject); //查询var t:student;f:file of student;i,j,pos:integer;begini:=0;j:=listbox1.Items.Count;pos:=0;assignfile(f,'student.dat');reset(f);listbox1.Items.Clear;while not eof(f) dobeginseek(f,pos);read(f,t);if ((edit4.text=t.no)or(edit4.text=t.xm)or(edit4.Text=t.mz)or(edit4.Text=t.xb)) then beginlistbox1.Items.add(t.no+' '+t.xm+' '+t.xb+' '+t.mz+' '+t.sr+' '+t.dh+' '+t.ah); i:=i+1;end;pos:=pos+1;end;closefile(f);label8.Caption:='共有记录条数'+#13+inttostr(j)+#13+'查询到记录'+#13+inttostr(i)+'条'; end;procedure TForm1.Button12Click(Sender: TObject); //显示所有beginButton5.Click;end;procedure TForm1.X1Click(Sender: TObject); //记录编辑(主菜单)begintabsheet2.Show;end;procedure TForm1.Y2Click(Sender: TObject); //记录查询(主菜单)begintabsheet1.Show;end;procedure TForm1.Z2Click(Sender: TObject); //退出系统(主菜单)beginform1.Close;end;procedure TForm1.V1Click(Sender: TObject); //添加记录(主菜单)begintabsheet2.Show;Button1.Click;end;procedure TForm1.W1Click(Sender: TObject); //删除记录beginButton2.Click;end;procedure TForm1.X01Click(Sender: TObject); //修改记录(主菜单)begintabsheet2.Show;Button3.Click;end;procedure TForm1.Y3Click(Sender: TObject); //保存记录(主菜单)beginButton4.Click;end;procedure TForm1.Z3Click(Sender: TObject);beginButton12.Click;end;end.。
Delphi控制Word编程手记
Delphi控制Word编程手记前几日对软件“文件整理助手”进行了完善。
该软件有文本文件合并,文本文件内容的替换、插入、删除、为特定行首/尾添加字符、清理空行等,以及文件批量替换、改名等功能。
一同事见后,希望能对Word文件进行合并。
尽管Word的“插入文件”可以实现这个功能,但不能在插入文件时调整插入的顺序,也不能控制插入的新文件是否另起一页。
Word虽然功能强大,但还是有一定的局限性。
当然,通过VBA录入脚本、编写宏代码也许可以实现这些复杂的功能。
但囿于其缺乏通用性和移植性,对于不善于编程的人来说,还是存在诸多不便。
因此,打算做一个“Word文档合并器”。
刚做出这个决定时,以为很简单,因为Delphi的Servers组件页有WordApplication、WordDocument等控件,通过它们来控制全不是那么回事!以前做过涉及到Excel的小程序,没觉得有多难。
首次跟Word打交道,竟给我来了个大大的下马威。
以前用过函数,用过过程,也用过带参数的函数、带参数的过程。
见过参数多的,但没见过打开、保存Word时尽然要用多达15个、16个参数的函数、过程。
而且这些参数青一色地被定义为OleVariant类型,哪些应该是字符串,哪些应该是布尔型,或者专门为Word程序和文档定义的变量类型,没有详细的、系统的资料,只好摸着石头过河,慢慢研究了。
经过几翻碰壁、几翻查证、几翻试验,把要实现的功能一步步拆解,逐一进行调试,通过后再重新组合起来试验。
经过拆解、调试、组装三步曲之后,总算是完成了“Word文档合并器”这样一个小小的软件。
为避免下次还重复这种繁琐的基础工作,现将有关技术要点总结于下:(本程序在Word2003中调试通过,其他版本未进行测试。
网上找的一些资料在过程调用、函数语句及参数个数上有出入,可能与Word版本不一样有关。
)说明:主窗体中放置以下三个与Word有关的控件:Word: TWordApplication; //Word应用程序接口Document1: TWordDocument; //Word文档ole_ShowDoc: TOleContainer; //用以显示Word文档【一】相关Word组件这里仅整理Delphi通过自身所提供的Server组件连接Office (Word)的有关资料,其他方法暂不研究。
Delphi 文件操作
Delphi 文件操作var file_name:string;file_name:='c:\luther.gliethttp.txt';FileWrite(com.log_fd, PChar(file_name)^, length(file_name));文件是同一种类型元素的有序集合,是内存与外设之间传输数据的渠道。
文件的本质是一个数据流,所有的文件实际上是一串二进制序列。
文件管理包括:1、文件操作。
2、目录操作。
3、驱动器操作。
三部分。
1、常见文件操作函数和过程。
1.1、AssignFile过程作用:将一个外部文件的文件名与一个File类型的变量关联,并对变量进行初始化。
原型:procedure AssignFile(var F; FileName: String);F:一个File类型的变量,可以是任何数据类型。
FileName:外部文件的文件名。
1.2、FileExists函数作用:用于测试指定文件名的文件是否存在。
原型:function FileExists(const FileName: String): Boolean;FileName:文件名。
字符串类型。
注意:如果文件存在,返回True;否则,返回False。
1.3、Append过程作用:为在一个已经存在的文件末尾添加文本做好准备工作。
文件是只写方式打开的。
原型:procedure Append(var F: Text);F: 一个TextFile类型变量。
注意:这是一个对文本类型文件操作的过程。
其中变量F必须和外部文件关联。
如果F 没有与外部文件关联,将抛出异常。
如果F已经处于打开状态,则过程会关闭F并重新打开。
文件指针位置将被设置在文件末尾。
1.4、Reset过程作用:对于文本文件,Reset过程将以只读方式打开文件。
对于类型文件和无类型文件,Reset过程将以可读写方式打开文件。
原型:procedure Reset(var F [: File; RecSize: Word ] );F:File类型变量,可以是任何文件类型。
记事本程序源代码汇总
记事本程序源代码汇总下面是一个简单的记事本程序的源代码:```#include <iostream>#include <fstream>#include <string>using namespace std;void showMencout << "**********************" << endl; cout << " 记事本程序 " << endl; cout << "**********************" << endl; cout << "请选择以下操作:" << endl;cout << "1. 新建记事本文件" << endl;cout << "2. 打开已有记事本文件" << endl; cout << "3. 查看记事本文件内容" << endl; cout << "4. 添加文本到记事本文件" << endl; cout << "5. 退出程序" << endl;cout << "**********************" << endl; void createFilstring filename;cout << "请输入新建记事本文件的文件名:";cin >> filename;//在当前目录创建一个新文件ofstream outFile(filename);outFile.close(;cout << "成功创建记事本文件:" << filename << endl; void openFilstring filename;cout << "请输入要打开的记事本文件的文件名:";cin >> filename;//尝试打开已存在的文件ifstream inFile(filename);if (!inFile)cout << "无法打开文件:" << filename << endl;} elsestring content;getline(inFile, content, '\0');cout << "记事本文件内容:" << endl;cout << content << endl;inFile.close(;}void viewFilstring filename;cout << "请输入要查看的记事本文件的文件名:"; cin >> filename;//尝试打开已存在的文件ifstream inFile(filename);if (!inFile)cout << "无法打开文件:" << filename << endl; } elsestring line;cout << "记事本文件内容:" << endl;while (getline(inFile, line))cout << line << endl;}inFile.close(;}void appendToFilstring filename;cout << "请输入要添加文本的记事本文件的文件名:";cin >> filename;//尝试打开已存在的文件ofstream outFile(filename, ios::app);if (!outFile)cout << "无法打开文件:" << filename << endl;} elsestring content;cout << "请输入要添加的文本内容(以#结束):" << endl; while (true)getline(cin, content);if (content == "#")break;}outFile << content << endl;}outFile.close(;cout << "成功添加文本到记事本文件:" << filename << endl; }int maiint choice;doshowMenu(;cout << "请输入您的选择:";cin >> choice;switch (choice)case 1:createFile(;break;case 2:openFile(;break;case 3:viewFile(;break;case 4:appendToFile(;break;case 5:cout << "感谢使用记事本程序,再见!" << endl;break;default:cout << "无效的选择,请重新输入!" << endl;}} while (choice != 5);return 0;```这个记事本程序通过命令行界面提供了以下操作:1.新建记事本文件:用户输入一个文件名后,在当前目录下创建一个新文件作为记事本。
delphi文本记录流式读写文件
delphi⽂本记录流式读写⽂件unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;typeTForm1 = class(TForm)btn1: TButton;edt1: TEdit;edt2: TEdit;btn2: TButton;btn3: TButton;btn4: TButton;btn5: TButton;procedure btn1Click(Sender: TObject);procedure btn2Click(Sender: TObject);procedure btn3Click(Sender: TObject);procedure btn4Click(Sender: TObject);procedure btn5Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}//⽂本⽂件写和读procedure TForm1.btn1Click(Sender: TObject);varf:TextFile;s:string;beginAssignFile(f,'a.txt');try//if FileExists('a.txt') then// Append(f)// elseRewrite(f);Writeln(f,edt1.Text,'---',edt2.Text);////////////Reset(f);Readln(f,s);edt2.Text:= s;finallyCloseFile(f);end;end;//记录类型⽂件写procedure TForm1.btn2Click(Sender: TObject);typestudent=recordsNo:string[10];sName:string[10];end;varf:File of student;stu:student;isize:Integer;beginAssignFile(f,'a.dat');tryif not FileExists('a.dat') thenRewrite(f)elseReset(f);isize:= FileSize(f);Seek(f,isize);stu.sNo:= edt1.Text;stu.sName:= edt2.Text;Write(f,stu);finallyCloseFile(f);end;end;//记录类型⽂件读procedure TForm1.btn3Click(Sender: TObject);typestudent=recordsNo:string[10];sName:string[10];end;varf:File of student;stu:student;isize:Integer;beginAssignFile(f,'a.dat');tryif not FileExists('a.dat') thenRewrite(f)elseReset(f);Seek(f,1);//读取第⼆条记录,直接⽤数字表⽰read(f,stu);edt1.Text:=stu.sNo;edt2.Text:=stu.sName;finallyCloseFile(f);end;end;//流写procedure TForm1.btn4Click(Sender: TObject);typeTBuffer = array [0..160]of char;varFileStream: TFileStream;tempBuffer:TBuffer;beginStrPcopy(tempBuffer,edt1.Text);Filestream:=TFileStream.Create('a.file',fmCreate);FileStream.Position:=0;FileStream.Write(tempBuffer,length(tempBuffer)); FreeAndNil(FileStream);end;//流读procedure TForm1.btn5Click(Sender: TObject);typeTBuffer = array [0..160]of char;varFileStream: TFileStream;ABuffer: TBuffer;beginFilestream:=TFileStream.Create('a.file',fmOpenRead); Filestream.Position:=0;FileStream.Read(ABuffer,sizeof(ABuffer));edt2.Text:= abuffer;FreeAndNil(FileStream);end;end.。
delphi packedrecord使用示例
delphi packedrecord使用示例在Delphi 中,`packed record` 是一种特殊的记录类型,它可以用于在固定大小的数据结构中存储可变大小的数据。
`packed record` 类型的字段会自动进行压缩,以适应其父数据结构的固定大小。
下面是一个使用`packed record` 的示例:```delphitypeTMyPackedRecord = packed recordName: array[0..255] of AnsiChar;Age: Integer;end;varMyPackedRecord: TMyPackedRecord;MyOtherPackedRecord: packed recordID: Integer;OtherData: array[0..10] of Byte;end;begin// 填充TMyPackedRecord 类型的实例 := 'John Doe';MyPackedRecord.Age := 30;// 填充TMyOtherPackedRecord 类型的实例MyOtherPackedRecord.ID := 123;MyOtherPackedRecord.OtherData := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];end.```在这个示例中,我们定义了两个`packed record` 类型:`TMyPackedRecord` 和`TMyOtherPackedRecord`。
`TMyPackedRecord` 包含一个变长字符串和一个整数,而`TMyOtherPackedRecord` 包含一个整数和一个长度为11 的字节数组。
注意,由于使用了`packed` 关键字,每个字段都会根据父数据结构的长度进行压缩。
因此,实际分配的内存空间将根据字段的大小和顺序进行调整。
在使用`packed record` 时,需要注意以下几点:1. `packed record` 的字段不会进行内存对齐,而是紧密相邻排列。
基于delphi的电子日记本软件的实现
基于delphi的电子日记本软件的实现
申玉亮
【期刊名称】《福建电脑》
【年(卷),期】2014(000)005
【摘要】本文介绍了基于delphi编程语言的windows环境下一些编程原理和方法,结合软件工程的理念、数据库、加密与面向对象编程等主要技术,详细讲述了电子日记本软件的内容设计及背景、delphi编程语言、信息加密、数据库设计和程序设计。
【总页数】3页(P113-114,128)
【作者】申玉亮
【作者单位】淮阴区人力资源和社会保障局江苏淮安 223300
【正文语种】中文
【相关文献】
1.基于Delphi的激光雕刻上位机软件设计与实现 [J], 张天;王新政;唐道华
2.基于Delphi的通用比赛倒计时软件的设计与实现 [J], 陈进;
3.基于Delphi的地毯簇绒机横动补偿系统软件设计与实现 [J], 赵斌良;陈广锋;孙以泽
4.基于Delphi的计算机电源定时管理软件的实现 [J], 朱毅
5.基于Delphi的称重控制器上位机软件设计与实现 [J], 张颖超;马艳娜;胡凯
因版权原因,仅展示原文概要,查看原文内容请购买。
delphi stringgrid setedittext
delphi stringgrid setedittext在Delphi 中,`TStringGrid` 是一个用于显示和编辑表格数据的控件。
如果你想设置特定单元格的编辑文本,可以使用`Cells` 属性来访问单元格的文本,并使用`SetEditText` 事件来设置编辑文本。
以下是一个示例,演示如何在`TStringGrid` 中设置单元格的编辑文本:```delphiunit MainForm;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, Grids;typeTForm1 = class(TForm)StringGrid1: TStringGrid;procedure FormCreate(Sender: TObject);procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);begin// 设置表格的行列数StringGrid1.RowCount := 5;StringGrid1.ColCount := 3;// 设置初始单元格文本StringGrid1.Cells[0, 0] := 'Cell (0, 0)';StringGrid1.Cells[1, 1] := 'Cell (1, 1)';StringGrid1.Cells[2, 2] := 'Cell (2, 2)';end;procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);begin// 在这里可以设置编辑后的文本ShowMessage(Format('Cell (%d, %d) edited. New value: %s', [ACol, ARow, Value]));end;end.```在上述代码中,`FormCreate` 事件用于初始化表格,并设置了一些初始文本。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls, ComCtrls, ExtCtrls, ToolWin, ImgList; type TForm1 = class(TForm MainMenu1: TMainMenu; RichEdit1: TRichEdit; OpenDialog1: TOpenDialog; SaveDialog1: TSaveDialog; N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; FontDialog1: TFontDialog; N4: TMenuItem; N5: TMenuItem; N6: TMenuItem; N7: TMenuItem; N8: TMenuItem; N9: TMenuItem; N10: TMenuItem; N11: TMenuItem; N12: TMenuItem; N13: TMenuItem; N14: TMenuItem; N15: TMenuItem; N16: TMenuItem; N17: TMenuItem; N18: TMenuItem; N19: TMenuItem; N20: TMenuItem; PopupMenu1: TPopupMenu; FindDialog1: TFindDialog; N21: TMenuItem; PrintDialog1: TPrintDialog; N22: TMenuItem; N23: TMenuItem; N24: TMenuItem; N25: TMenuItem; N26: TMenuItem; N27: TMenuItem; N28: TMenuItem; N31: TMenuItem; N32: TMenuItem; S1: TMenuItem; Timer1: TTimer; StatusBar2: TStatusBar; ToolBar1: TToolBar; ToolButton1: TToolButton; ToolButton2: TToolButton; ToolButton3: TToolButton; ToolButton4: TToolButton; ToolButton5: TToolButton; ToolButton6: TToolButton; ToolButton7: TToolButton; ToolButton8: TToolButton; ToolButton9: TToolButton; ToolButton10: TToolButton; ToolButton11: TToolButton; ToolButton12: TToolButton; ToolButton13: TToolButton; ImageList1: TImageList; procedureN2Click(Sender: TObject; procedure N3Click(Sender: TObject; procedureN7Click(Sender: TObject; procedure N8Click(Sender: TObject; procedureN9Click(Sender: TObject; procedure N14Click(Sender: TObject; procedureN21Click(Sender: TObject; procedure N16Click(Sender: TObject; procedureN17Click(Sender: TObject; procedure N18Click(Sender: TObject; procedureN12Click(Sender: TObject; procedure N20Click(Sender: TObject; procedureN6Click(Sender: TObject; procedure S1Click(Sender: TObject; procedureFindDialog1Find(Sender: TObject; procedure Timer1Timer(Sender: TObject; procedure N24Click(Sender: TObject; procedure N25Click(Sender: TObject; procedureN26Click(Sender: TObject; procedure N27Click(Sender: TObject; procedureN28Click(Sender: TObject; procedure RichEdit1MouseMove(Sender: TObject; Shift:TShiftState; X, Y: Integer; procedure RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState; procedure RichEdit1Enter(Sender: TObject; private { Private declarations } public { Public declarations } end; var Form1: TForm1; filename: string; temp:integer; implementation {$R *.dfm} procedure TForm1.N2Click(Sender: TObject; begin if opendialog1.Execute then begin Form1.Caption:=ExtractFileName(OpenDialog1.FileName;richedit1.lines.LoadFromFile(opendialog1.FileName; end end; procedureTForm1.N3Click(Sender: TObject; begin if filename='' then begin if savedialog1.Execute then begin richedit1.Lines.SaveToFile(savedialog1.FileName;filename:=savedialog1.filename; end end elserichedit1.Lines.SaveToFile(savedialog1.FileName; end; procedureTForm1.N7Click(Sender: TObject; begin fontdialog1.Execute;richedit1.SelAttributes.Assign(fontdialog1.Font; end; procedure TForm1.N8Click(Sender: TObject; begin if RichEdit1.Modified then begin temp:=MessageDlg('内容发生改变,是否要保存',mtwarning,mbyesnocancel,0; if temp=6 then begin Form1.N3Click(Sender ; RichEdit1.Lines.Clear; end; if temp=7 then RichEdit1.Lines.Clear; end elseRichEdit1.Lines.Clear; end; procedure TForm1.N9Click(Sender: TObject; begin with savedialog1 do if execute then begin richedit1.Lines.SaveToFile (filename;opendialog1.FileName := filename; end; end; procedure TForm1.N14Click(Sender: TObject; begin richedit1.Perform(EM_UNDO,0,0; end; procedureTForm1.N21Click(Sender: TObject; begin richedit1.SelectAll; end; procedureTForm1.N16Click(Sender: TObject; begin richedit1.cuttoclipboard; end; procedure TForm1.N17Click(Sender: TObject; begin richedit1.copyToClipboard; end; procedure TForm1.N18Click(Sender: TObject; begin richedit1.PasteFromClipboard; end; procedure TForm1.N12Click(Sender: TObject; begin PrintDialog1.Execute; end; procedureTForm1.N20Click(Sender: TObject; begin finddialog1.Execute; end; procedureTForm1.N6Click(Sender: TObject; begin if N6.Checked then begin N6.Checked:=false; RichEdit1.WordWrap:=False; end else begin n6.Checked:=True;RichEdit1.WordWrap:=True; end end; procedure TForm1.S1Click(Sender: TObject; begin if s1.Checked then begin s1.Checked:=false; statusbar2.visible:=False; end else begin s1.Checked:=True; statusbar2.visible:=True; end end; procedureTForm1.FindDialog1Find(Sender: TObject; Var findat:longint; startpos,toend:integer; begin with richedit1 do begin if sellength<>0 then startpos:=selstart+sellength else startpos:=0; toend:=length(text-startpos;findat:=findtext(FindDialog1.FindText,startpos,toend,[stMatchCase]; if findat<>-1 then begin setfocus ; SelStart:=findat; SelLength:=Length(FindDialog1.FindText end; end; end; procedure TForm1.Timer1Timer(Sender: TObject; beginstatusbar2.Panels[1].Text:=timetostr(time(; end; procedure TForm1.N24Click(Sender: TObject; begin richedit1.Perform(EM_UNDO,0,0; end; procedureTForm1.N25Click(Sender: TObject; begin richedit1.cuttoclipboard; end; procedure TForm1.N26Click(Sender: TObject; begin richedit1.copyToClipboard; end; procedure TForm1.N27Click(Sender: TObject; begin richedit1.PasteFromClipboard; end; procedure TForm1.N28Click(Sender: TObject; begin richedit1.SelectAll; end; procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer; begin StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+1+'行'+inttostr(RichEdit1.CaretPos.X+1+'列' end; procedureTForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState; begin if Key=13 then StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+2+'行'+'1列' else StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+1+ '行'+inttostr(RichEdit1.CaretPos.X+2+'列' end; procedure TForm1.RichEdit1Enter(Sender: TObject; begin StatusBar2.Panels[0].Text:=IntToStr(RichEdit1.CaretPos.Y+1+'行'+inttostr(RichEdit1.CaretPos.X+1+'列' end; end.。