12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

260 Day 7<br />

Pop-up Menus (Speed Menus)<br />

We’re not quite done with our discussion of menus. In <strong>C++</strong>Builder, you can create pop-up<br />

menus as easily as you can create a ma<strong>in</strong> menu. A nice feature of <strong>C++</strong>Builder is that you can<br />

assign a particular pop-up menu to a component via the PopupMenu property of the<br />

component. When the cursor is placed over the component and the secondary mouse button<br />

is clicked, that pop-up will automatically be displayed. Writ<strong>in</strong>g event handlers for pop-up<br />

menus is exactly the same as writ<strong>in</strong>g event handlers for ma<strong>in</strong> menus.<br />

A common feature of text-edit<strong>in</strong>g programs is that the Cut, Copy, and Paste operations are<br />

on a speed menu. We’ll add that capability to ScratchPad. To create the pop-up, we’ll cheat<br />

and copy part of the ma<strong>in</strong> menu. Here we go:<br />

1. Choose a PopupMenu component from the Component Palette and place it on the<br />

form.<br />

2. Change the Name property to MemoPopup.<br />

3. Double-click the PopupMenu icon to run the Menu Designer.<br />

4. Click the secondary mouse button to br<strong>in</strong>g up the Menu Designer speed menu.<br />

Choose Select Menu from the speed menu. A dialog box is displayed that shows<br />

the menus available for your application. Choose Ma<strong>in</strong>Menu and click OK.<br />

5. Click on the Edit menu. Click on the Cut menu item, hold down the Shift key,<br />

and click on the Paste menu item. Cut, Copy, and Paste are all now highlighted.<br />

6. To copy the selected items to the Clipboard, choose Edit | Copy from the<br />

<strong>C++</strong>Builder ma<strong>in</strong> menu (don’t choose Edit | Copy from the menu you are creat<strong>in</strong>g<br />

<strong>in</strong> the Menu Designer) or press Ctrl+C.<br />

7. Aga<strong>in</strong>, choose Select Menu from the Menu Designer speed menu. This time, choose<br />

MemoPopup and click OK. The Menu Designer shows a blank pop-up menu.<br />

8. Choose Edit | Paste from the ma<strong>in</strong> menu or type Ctrl+V on the keyboard. The<br />

Cut, Copy, and Paste menu items are <strong>in</strong>serted <strong>in</strong>to the pop-up.<br />

Okay, just a few more th<strong>in</strong>gs and we’ll be done. We need to change the Name property for the<br />

new menu items:<br />

1. For the Cut menu item, change the Name property to PopupCut.<br />

2. Change the Name property for the Copy menu item to PopupCopy.<br />

3. F<strong>in</strong>ally, change the Name property for the Paste menu item to PopupPaste.<br />

The f<strong>in</strong>al step is to write event handlers for the pop-up menu items. Hmmm…we already have<br />

code written for the ma<strong>in</strong> menu’s Cut, Copy, and Paste items. It would be a shame to duplicate<br />

that code (even if it is just a s<strong>in</strong>gle l<strong>in</strong>e <strong>in</strong> each case). It would be nice if we could just use the<br />

same event handlers that we created earlier. Can we? Sure we can! Just follow these steps:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!