Next, let's continue writing our scratchpad program.
Write code
With all these menu items, there is no code to make it work. It is actually easy to have a lot of work to accomplish this. Most of the code is required to make the existing part of the tmemo class. All you need to do is call the tmemo method in the menu handler. We also need to do some other things, but most of the added code is previously seen.
1. Add components to the form
Before writing code, you need to add the opendialog and savedialog components to the form:
(1) Place the opendialog component on the form and change its name attributeOpendialog.
(2) Place the savedialog component on the form and change its name attributeSavedialog.
(3) line the mainmenu, opendialog, and savedialog icons on the form.
2. Write the code for the main menu item
Adding components above is very simple. Continue to write code for the menu item. First, write the code for the [file | exit] menu item (this is the easiest !). Make sure that Menu Designer is disabled, so that Menu Designer and Form Designer are not confused.
(1) Select File | exit from the Form Designer Main Menu. Code Editor appears on the front end and displays the fileexitclick event handler.
(2) The cursor has been positioned in the fileexitclick event handler, and type the code at the cursor:
procedure TMainForm.FileExitClick(Sender: TObject);begin Close;end;
Note
In the above two steps, close is used to close the form. Because this is the main form of the application, it is valid. Use:
Application.Terminate;
This Code ensures that the program is interrupted, regardless of the current form.
This is easy. Let the reader complete the rest.
(1) select 【Edit | cutThe Code Editor appears at the top and displays the editcutclick event handler.
(2) enter the following code:
procedure TMainForm.EditCutClick(Sender: TObject);begin Memo.CutToClipboard;end;
The cut menu is done in this way. Maybe not all of them are aware of this. VCL has done a lot of work behind the scenes. The entire idea of the framework allows programmers to avoid low levels of detailed burdens.
3. end work
One interesting aspect of a program like Delphi is that the whole program is rarely seen. Delphi provides code segments that take care of specific events. Therefore, we usually see small pieces of programs. The following list shows the main form unit of the scratchpad program so far. Class declarations are completely generated by Delphi. Follow the example to write the remaining menu item code.
4. Detailed explanation and Description: [file | new] menu item code
Procedure tmainform. filenewclick (Sender: tobject); var Res: integer; begin {create a file and check whether the current file is saved. If not saved, how can I prompt whether the message is saved, if yes is selected, the current file is saved, and no is selected. The current file is ignored.} If Memo. modified then begin Res: = application. messageBox ('the current file has changed. save changes? ', 'Scratchpad message', mb_yesnocancel); {if you click "yes", save the current file first} If res = idyes then filesaveclick (sender ); {if you click "cancel", nothing will be done.} If res = idcancel then exit; end; {deleting memo's existing characters} If Memo. lines. count> 0 then memo. clear; {set the filename attribute of savedialog to an empty string to let us know that the file is not stored} savedialog. filename: = ''; end;
[File | open ...] Menu item code
Procedure tmainform. fileopenclick (Sender: tobject); var Res: integer; begin {open a file and check whether the current file is saved. If not saved, how can I prompt whether the message is saved, if yes is selected, the current file is saved, and no is selected. The current file is ignored.} If Memo. modified then begin Res: = application. messageBox ('the current file has changed. save changes? ', 'Scratchpad message', mb_yesnocancel); If res = idyes then filesaveclick (sender); If res = idcancel then exit; end; {execute the open file dialog box, select the file to be opened, click "OK", and use the loadfromfile method to open the file. First, check the filename attribute} opendialog. filename: = ''; If opendialog. execute then begin if memo. lines. count> 0 then memo. clear; memo. lines. loadfromfile (opendialog. filename); savedialog. filename: = opendialog. filename; end;
[File | save ...]
Procedure tmainform. filesaveclick (Sender: tobject); begin {First Judge savedialog. whether the filename attribute is empty. If it is not empty, the savetofile method is called directly. Otherwise, the savedialog dialog box is called to save.} If savedialog. filename <> ''then begin memo. lines. savetofile (savedialog. filename); {set memo. modified is false. If it is saved,} memo. modified: = false; end else {if the filename attribute is empty, call the Save As dialog box} filesaveasclick (sender); end;
[File | save ...]
Procedure tmainform. filesaveasclick (Sender: tobject); begin {The savedialog dialog box is displayed. Save the file and set memo. modified is false. Once saved,} savedialog. title: = 'Save... '; If savedialog. execute then begin memo. lines. savetofile (savedialog. filename); memo. modified: = false; end;
[Edit | undo]
Procedure tmainform. editundoclick (Sender: tobject); begin {tmemo component does not provide the Undo revocation method. Therefore, you can only send the wm_undo message to implement} sendmessage (memo. handle, wm_undo, 0, 0); end;
[Edit | select all]
Procedure tmainform. editselectallclick (Sender: tobject); begin {call the memo. selectall method to implement the full selection function} memo. selectall; end;
[Edit | copy]
Procedure tmainform. editcopyclick (Sender: tobject); begin {call memo. copytoclipboard method} memo. copytoclipboard; end;
[Edit | paste]
Procedure tmainform. editpasteclick (Sender: tobject); begin {call memo. pastefromclipboard method} memo. pastefromclipboard; end;
[Edit | word wrap]
Procedure tmainform. editwordwrapclick (Sender: tobject); begin {set the memo. wordwrap attribute to implement the automatic line feed function .} Memo. wordwrap: = Not memo. wordwrap; editwordwrap. Checked: = memo. wordwrap; If Memo. wordwrap then memo. Memory: = ssvertical else memo. scrollbars: = ssboth; end;
It's a long wait.
After creating an event handler for the menu item, prepare to run the program. Click the run button to compile and run the program. If a compilation error occurs, make a careful comparison of the above Code and modify it several times so that it can run successfully.
When running the program, you will find that the program is not very exquisite, most of them are like Windows notepad. Although there are some things to add before the completion, there is a good start. Displays the running scratchpad program.
The above code passes the test in Delphi7. Download the sample code in this section:Scratchpad(the second character of the Menu Designer. rar