Menus


There are a number of resources listed in this window, including the dialog itself. Some of these we will return to later in this book. For now, our focus will be on how to add menus to the resource tab. Fortunately, this is a relatively simple task. If you will right-click on the top of the resource list, you will be presented with an opportunity to insert new resources, including menus. This is shown in Figure 16.14.

click to expand
Figure 16.14: New menus.

click to expand
Figure 16.15: A new menu item.

The next step is to right-click on the menu and choose properties, as is shown in Figure 16.16.

click to expand
Figure 16.16: Menu item properties.

You will then be able to assign a caption to the menu, decide whether it is a checked menu, if it is grayed out, and so on, as shown in Figure 16.17.

click to expand
Figure 16.17: Selecting menu item properties.

click to expand
Figure 16.18: Associating a menu with a dialog.

Hint!

Make sure that you are right-clicking on the dialog and not on an individual component on the dialog.

You can see that you can now associate any given menu with any dialog. Creating menus and associating them with dialogs is a relatively straightforward operation that you can accomplish in just a few steps. You will find that adding drop-down menus to your applications makes them much more user-friendly. Let’s look at an example that shows the use of the drop-down menu, coupled with math functions that we saw earlier in this book.

Example 16.3

Step 1: Create a standard dialog, just as you have been doing with the previous examples.

click to expand
Figure 16.19: Example 16.3 component layout.

Step 3: Now use the class wizard to associate a float variable with each of the Edit boxes, as is shown in Figure 16.20.

click to expand
Figure 16.20: Naming the Edit boxes.

Step 4: We will now add a series of menus. You should recall from the previous discussion that you will need to select the resource tab, and right-click on it to add a menu. If you continue to right-click on the menu, just below the previous item, you can add several items in one menu, as shown in Figure 16.21.

click to expand
Figure 16.21: Adding multiple menu items.

Step 5: Next, we will associate that menu with the dialog. Remember that you accomplish this by right-clicking on the dialog itself and choosing properties.

click to expand
Figure 6.22: Menu items for Example 16.3.

click to expand
Figure 16.23: Launching the class wizard.

click to expand
Figure 16.24: Class wizard continued.

Step 7: Now we can add functions to our menu items. To do this, you use tab one of the class wizard (Message Maps), select the menu item, then select the word command in the right pane. This is shown in Figure 16.25. You can then choose add function, and add an OnMenuItem function as shown in Figure 16.26.

click to expand
Figure 16.25: Selecting a menu item.

click to expand
Figure 16.26: Adding a function.

You will need to add a function for each of these menu items. Thus, you will have an OnSquare, OnCube, OnMathOperations, and onSQRT function. If you then choose to edit any of these functions (and you should!), a window will pop-up that displays the code for all these functions. Your first order of business is to scroll to the top of that source file and add the include statement for the cmath header file.

#include <cmath>

Step 8: Scrolling back down to the functions, we are going to add just a few lines of code as you see here.

void CExample16_03Dlg::OnCube()  {      UpdateData(TRUE);      m_output = pow(m_input,3);      UpdateData(FALSE);       } void CExample16_03Dlg::OnSqrt()  {      UpdateData(TRUE);      m_output = sqrt(m_input);      UpdateData(FALSE);      } void CExample16_03Dlg::OnMathoperations()  {      AfxMessageBox("Please select one of the math   operations"); } void CExample16_03Dlg::OnSquare()  {       UpdateData(TRUE);       m_output = pow(m_input,2);       UpdateData(FALSE);      } 

Step 9: Now it’s time to execute this program. You should see something much like what is depicted in Figure 16.27.

click to expand
Figure 16.27: Running menu programs.

When you enter a number into the first Edit box, and select one of the menu options, a mathematical operation is performed on that number, and the answer is placed in the second Edit box. This application, while simple, should illustrate to you how to utilize the menus, and how to incorporate standard C++, such as the functions in cmath, with Visual C++.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net