Be Friendly
Search This Site for Tutorials About...

How to Create a Window Menu Programmatically (C++)

Just for fun: How to Make French Toast

I'll base this tutorial on the previous one in which I showed you how to draw red pixels in the window. In this tutorial I'll show you how you can create a drop-down(or whatever those called) menu with 3 options to change the color of the pixels being drawn in the window. First declare some variables in the beginning of the code. This will store the current color of pixels.


int r = 255;
int g = 0;
int b = 0;

MVC lets you create resources such as icons, cursors and menus but we're not going to do it the MVC way. Coding it yourself is always better because you know what's actually going on. Unless you already know it. You will need to create a resource file yourself. And if you follow this tutorial you'll have no problems doing it. First, let's define the menu id's in a header(.h) file and then I'll talk about the resource(.rc) file contents. They(menu ids) will take care of getting the value from the menu when you click on an option so the menu knows what you've selected. And you will also need to define the menu's id itself to refer to menu from your code. Plus I wanna add an exit option. I only want to use 3 main options to change the color. After a little 2nd grade math you come up with 5 id's.


// resource.h

#define ID_Menu		100
#define ID_Red		0
#define ID_Green		1
#define ID_Blue		2
#define ID_Exit		3

Now that you have your defines down, take a look at the menu resource file. The .rc file can be written in Notepad and then added to your project. You will need to know a special menu "script" format to write menus. It is really straight-forward as you can see from the code below. It shouldn't be too hard to understand and I'm sure you'll be able to add as many options you want on your own.


// resource.rc

#include "resource.h"

// Menu script
ID_Menu MENU DISCARDABLE
{
    POPUP "File"
    {
        MENUITEM "Exit", ID_Exit
    }
    POPUP "Color"
    {
        MENUITEM "Red", ID_Red
        MENUITEM "Green", ID_Green
        MENUITEM "Blue", ID_Blue
    }
}

Add this file to your MVC project and the resource tab will pop up underneath the workspace window. Open it up, then open the menu resource and find the id of the menu(which should be ID_Menu). If you double click it the menu bar will show up and you can edit it the easy way. But at least now you know how to write little and clean menu scripts. Our next concern is getting the menu attached to our window and make it able to change the color of the pixels being drawn by selecting the color options from it. Declare a handle to your menu:



HMENU hmenu;

Now you have to load the menu from the resouces you've created. There's a function LoadMenu that lets you do that.



hmenu = LoadMenu(hinstance, MAKEINTRESOURCE(ID_Menu));

The function LoadMenu takes two parameters, handle to the instance of the window and the menu id. The MAKEINTRESOURCE macro converts an integer value to a resource type compatible with Win32 resource-management functions. This macro is used in place of a string containing the name of the resource. Now, to attach this menu to the window you have to pass hmenu to the CreateWindowEx function as the hMenu parameter. In previous windows tutorials I simply passed NULL to it because we didn't need a menu. Here's the code:



// create the window
hwnd = CreateWindowEx(NULL,
		      WNDCLASSNAME,
		      "Window",
		      WS_OVERLAPPEDWINDOW,
		      0, 0,
		      swidth, sheight,
		      NULL,
	      	      hmenu,
		      hinstance,
		      NULL);

At this point, if you run the program you should see a menu attached to your window. However if you click on its options - nothing happens. You have to let the menu know what actions to perform once you click an option. Remember we defined the options as ID_Red, ID_Green, ID_Blue and ID_Exit in the resource.h and resource.rc files? Now it's time to use them. To let the menu know what to do when you select its options you have to add a few lines to the event handler function, which will identify the option and perform actions based on it. Enough of that, and here's how it's done:


LRESULT CALLBACK WinProc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{

	case WM_CLOSE:
	    {
		PostQuitMessage(0);
	    } break;

	case WM_COMMAND:
	    {
		switch(LOWORD(wparam))
		{
		    case ID_Red:
		    {
			r = 250;
			g = 0;
			b = 0;
		    } break;

		    case ID_Green:
		    {
		      r = 0;
			g = 250;
			b = 0;
		    } break;

		    case ID_Blue:
		    {
		      r = 0;
			g = 0;
			b = 250;
		    } break;

		    case ID_Exit:
		    {
			quit = true;
		    } break;
		}
	    } break;
	}
	return DefWindowProc(hwnd, msg, wparam, lparam);
}

The point of interest here is everything inside WM_COMMAND switch statement. The menu messages are stored in the low word of wparam. And you just branch each option with a case statement. The case/switch statement is well explained in your C book. In the source code I added one more option for black and white colors, just for fun.

Did this article help you learn something new?

I enjoy writing for the Internet audiences because my thoughts are instantly published to the entire world. My work consists of writing educational articles about making websites to help people learn. If you enjoyed reading this article, or learned something new, then I have succeeded.

If the content on this website somehow helped you to learn something new, please let others know about it by sharing it on your website or on your Facebook page with your friends. In addition you can help by making a donation or bookmarking this site.