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.
POPUP "&Draw"
BEGIN
MENUITEM "Line" IDM_LINE
MENUITEM "Ellipse" IDM_ELLIPSE
END
#define IDM_LINE 110 #define IDM_ELLIPSE 111
bool gbDrawLine = false; bool gbDrawEllipse = false;
case IDM_LINE:
gbDrawLine = !gbDrawLine;
InvalidateRect(hWnd, 0, TRUE);
break;
case IDM_ELLIPSE:
gbDrawEllipse = !gbDrawEllipse;
InvalidateRect(hWnd, 0, TRUE);
break;
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);
}
© 20072025 XoaX.net LLC. All rights reserved.