[ Pobierz całość w formacie PDF ]
.FileOpenClick(Sender: TObject);varRes : Integer;begin{ Open a file.First check to see if the current file needs }{ to be saved.Same logic as in FileNewClick above.}if Memo.Modified then beginRes := Application.MessageBox(`THE CURRENT FILE HAS CHANGED.SAVE CHANGES?',`ScratchPad Message', MB_YESNOCANCEL);if Res = IDYES thenFileSaveClick(Sender);if Res = IDCANCEL thenExit;end;{ Execute the File Open dialog.If OK was pressed then }{ open the file using the LoadFromFile method.First }{ clear the FileName property.}OpenDialog.FileName := `';if OpenDialog.Execute then beginif Memo.Lines.Count > 0 thenMemo.Clear;Memo.Lines.LoadFromFile(OpenDialog.FileName);SaveDialog.FileName := OpenDialog.FileName;end;end;procedure TMainForm.FileSaveClick(Sender: TObject);begin{ If a filename has already been provided then there is }{ no need to bring up the File Save dialog.Just save the }{ file using SaveToFile.}if SaveDialog.FileName <> `' then beginMemo.Lines.SaveToFile(SaveDialog.FileName);{ Set Modified to False since we've just saved.}Memo.Modified := False;{ If no filename was set then do a SaveAs.}end else FileSaveAsClick(Sender);end;procedure TMainForm.FileSaveAsClick(Sender: TObject);begin{ Display the File Save dialog to save the file.}{ Set Modified to False since we just saved.}SaveDialog.Title := `Save As';if SaveDialog.Execute then beginMemo.Lines.SaveToFile(SaveDialog.FileName);Memo.Modified := False;end;end;procedure TMainForm.EditUndoClick(Sender: TObject);begin{ TMemo doesn't have an Undo method so we have to send }{ a Windows WM_UNDO message to the memo component.}SendMessage(Memo.Handle, WM_UNDO, 0, 0);end;procedure TMainForm.EditSelectAllClick(Sender: TObject);begin{ Just call TMemo.SelectAll.}Memo.SelectAll;end;procedure TMainForm.EditWordWrapClick(Sender: TObject);begin{ Toggle the TMemo.WordWrap property.Set the Checked }{ property of the menu item to the same value as WordWrap.}Memo.WordWrap := not Memo.WordWrap;EditWordWrap.Checked := Memo.WordWrap;{ If WordWrap is on then we only need the vertical scroll }{ bar.If it's off, then we need both scroll bars.}if Memo.WordWrap thenMemo.ScrollBars := ssVerticalelseMemo.ScrollBars := ssBoth;end;end.And Now, the Moment You've All Been Waiting For.After you create the event handlers for the menu items, you are ready to run theprogram.Click the Run button and the program should compile and run.If you getcompiler errors, carefully compare your source code with the code in Listing 6.1.Make any changes and click the Run button again.You might have to go through thisprocess a few times before the program will compile and run.Eventually, though,it will run (I promise!).When the program runs, you will find a program that, although not yet 100 percentfeature-complete, acts a lot like Windows Notepad.Even though you have a few thingsto add before you're finished, you have a fairly good start--especially when youconsider the actual time involved up to this point.Figure 6.22 shows the ScratchPadprogram running.FIGURE 6.22.TheScratchPad program in action.Pop-Up Menus (Context Menus)I am not quite done with the discussion of menus.In Delphi, you can create pop-upmenus as easily as you can a main menu.A nice feature of Delphi is that you canassign a particular pop-up menu to a component via the component's PopupMenu property.When the cursor is placed over the component and the secondary mouse button is clicked,that pop-up will automatically be displayed.Writing event handlers for pop-up menusis exactly the same as writing event handlers for main menus.A common feature of text-editing programs is to place the Cut, Copy, and Pasteoperations on a context menu.You'll add that capability to ScratchPad.To createthe pop-up, you'll cheat and copy part of the main menu.Follow these steps:1.Choose a PopupMenu component from the Component palette and place iton the form.2.Change the Name property to MemoPopup.3.Double-click the PopupMenu icon to run the Menu Designer.4.Click the secondary mouse button to bring up the Menu Designer contextmenu.Choose Select Menu from the context menu.A dialog box is displayed that showsthe menus available for your application.Choose MainMenu and click OK.5.Click on the Edit menu.Click on the Cut menu item, hold down the Shiftkey, and click on the Paste menu item.Cut, Copy, and Paste are all now highlighted.6.To copy the selected items to the Clipboard, choose Edit | Copy fromthe Delphi main menu (don't choose Edit | Copy from the menu you are creating inthe Menu Designer) or press Ctrl+C.7.Again, choose Select Menu from the Menu Designer context menu.Thistime, choose MemoPopup and click OK.The Menu Designer shows a blank pop-up menu.8.Choose Edit | Paste from the main menu or type Ctrl+V on the keyboard.The Cut, Copy, and Paste menu items are inserted into the pop-up.Okay, just a few more things and you'll be done.You need to change the Name propertyfor the new menu items:1.For the Cut menu item, change the Name property to PopupCut.2.For the Copy menu item, change the Name property to PopupCopy.3.For the Paste menu item, change the Name property to PopupPaste.The final step is to write event handlers for the pop-up menu items.Hmmm.youhave already written code for the main menu's Cut, Copy, and Paste items.It wouldbe a shame to duplicate that effort (even if it is just a single line in each case).Could you just use the same event handlers that you created earlier? Sure you can.Just follow these steps:1.Click on the Cut popup menu item.2.Click on the Events tab in the Object Inspector.3.Click the drop-down arrow button in the Value column next to the OnClickevent (the only event in the list).A list of event handlers created so far is displayed.4.Choose the EditCutClick event handler from the list.Now, when theCut pop-up menu item is clicked, the Edit | Cut handler will be called.No code duplicationis required.5.Repeat steps 1 through 4 for the Copy and Paste items on the pop-upmenu.When you are done, close the Menu Designer.6.On the main form, click on the Memo component.Change the PopupMenuproperty to MemoPopup (by choosing it from the list).You can attach just about any event to any event handler by using this method.Now run the program again to test the new context menu.Of course it works!Creating and Saving Menu TemplatesDelphi provides you with several menu templates that you can insert into yourmain menus and pop-ups.You can also create and save your own templates for futureuse in your programs.First, start the Menu Designer and create the menu.NOTE: When creating menus to use as templates, you first must have a mainmenu or a pop-up menu on a form in order to start the Menu Designer
[ Pobierz całość w formacie PDF ]