Win32 C++

Lesson 5 : Adding Menu Items

Overview

In this C++ video, we demostrate how to add menus and menu items to the menubar. The application for this tutorial adds few menus to our lesson 1 project, which toggle drawing the line and ellipse from lesson 4. We begin by opening the project from C++ Win32 Lesson 1.

Download Code

  1. With the project open, we first open the "Win32Lesson1.rc" resource file. We do this by expanding the "Resources Files" folder in the "Solution Explorer" pane, right-clicking on "Win32Lesson1.rc," and left-clicking "View Code" in the submenu.
  2. Scroll down the page and add following code to "Win32Lesson1.rc" at line 45:
  3. POPUP "&Draw"
        BEGIN
            MENUITEM "Line"     IDM_LINE
            MENUITEM "Ellipse"  IDM_ELLIPSE
        END
    

  4. Next open the file "Resource.h," which contains the various ID values. We can open it by expanding the "Header Files" folder in the "Solution Explorer" pane, right-clicking on "Resource.h," and left-clicking "Open" in the submenu.
  5. In the "Resource.h," we add the following two lines of code at line 13:
  6. #define IDM_LINE        110
    #define IDM_ELLIPSE     111
    

  7. Lastly, we open the code file "Win32Lesson1.cpp." To do this, we expand the "Source Files" folder in the "Solution Explorer" pane, right-click on "Win32Lesson1.cpp," and left-click "Open" in the submenu.
  8. In "Win32Lesson1.cpp," add the following two lines of code at line 8 to define our boolean variables to toggling drawing:
  9. bool gbDrawLine     = false;
    bool gbDrawEllipse  = false;
    
  10. In "Win32Lesson1.cpp," add the following eight lines of code at line 156 to handle the menu clicks and toggle drawing our line and ellipse:
  11. case IDM_LINE:
        gbDrawLine = !gbDrawLine;
        InvalidateRect(hWnd, 0, TRUE);
        break;
    case IDM_ELLIPSE:
        gbDrawEllipse = !gbDrawEllipse;
        InvalidateRect(hWnd, 0, TRUE);
        break;
    
  12. Finally, we add the following code to "Win32Lesson1.cpp" at line 171:
  13. HPEN hPenOld;
    
    if (gbDrawLine) {
        // Draw a red line
        HPEN hLinePen;
        COLORREF qLineColor;
        qLineColor = RGB(255, 0, 0);
        hLinePen = CreatePen(PS_SOLID, 7, qLineColor);
        hPenOld = (HPEN)SelectObject(hdc, hLinePen);
    
        MoveToEx(hdc, 100, 100, NULL);
        LineTo(hdc, 500, 250);
    
        SelectObject(hdc, hPenOld);
        DeleteObject(hLinePen);
    }
    
    if (gbDrawEllipse) {
        // Draw a blue ellipse
        HPEN hEllipsePen;
        COLORREF qEllipseColor;
        qEllipseColor = RGB(0, 0, 255);
        hEllipsePen = CreatePen(PS_SOLID, 3, qEllipseColor);
        hPenOld = (HPEN)SelectObject(hdc, hEllipsePen);
    
        Arc(hdc, 100, 100, 500, 250, 0, 0, 0, 0);
    
        SelectObject(hdc, hPenOld);
        DeleteObject(hEllipsePen);
    }
    

  14. Now we can compile and execute the program. Selecting Draw->Line or Draw->Ellipse should toggle drawing the line or ellipse, respectively.
 

© 2007–2025 XoaX.net LLC. All rights reserved.