menu

hi,
i am trying to develop a program in C++, to bring up a dialog box, with drop down menu.
please can anyone help point me in the right direction on where and how to start
thanks
You create a dialog with CreateDialog(). This dialog must be defined in your resource file (.rc), as must your menu.

If your dialog is your main window, then in the RegisterClass() function you must set the cWndExtra size like this --

wc.cWndExtra=DLGWINDOWEXTRA

Have you started any code at all? If you don't know how to do this, I'd suggest you go to this tutorial --

http://www.winprog.org/tutorial/
Check the below code.. It is a tutorial that give you a good idea of creating menus, edit boxes, buttons, combo boxes, labels and lists..
The menu must be defined in the resource file (.rc) and then linked to your window in lpszMenuName..

The tutorial code is:
resource.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by Script1.rc
//
#define IDR_MENU1                       101
#define IDB_BITMAP1                     102
#define IDI_ICON1                       103
#define ID_FILE_MSGABOUT                40001
#define ID_FILE_WINDOWABOUT             40002
#define ID_FILE_EXIT                    40003

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        104
#define _APS_NEXT_COMMAND_VALUE         40004
#define _APS_NEXT_CONTROL_VALUE         1000
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif 


WinMain.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// windows controls platform sdk
// jared bruni
// http://www.lostsidedead.com/gameprog
// email me: jared@lostsidedead.com

// win32 standard controls, via platform SDK :)

#include <windows.h> // windows header file
#include "resource.h"

// enumerated constants for the child handles
enum { ID_LABEL = 1,ID_IMAGE,ID_EDIT,ID_LIST,ID_BUTTON,ID_COMBO, ID_BUTTON2 };



// function prototypes
LRESULT APIENTRY TestWindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
LRESULT APIENTRY AboutWindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
void onmenu(WPARAM);


// Application Instance Handle
HINSTANCE g_hInst;

// window handles (unsigned long)'s
HWND testwin;
HWND aboutwin;
HWND static_label;
HWND static_image;
HWND edit;
HWND list;
HWND button;
HWND combo;

// using _stdcall the winmain entry
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR line,int CmdShow)
{
	g_hInst = hInst;
	MSG msg;
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON1));
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.lpfnWndProc = (WNDPROC) TestWindowProc;
	wc.lpszClassName = "Test:)";
	wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
	wc.style = CS_HREDRAW | CS_VREDRAW;

	RegisterClass(&wc);

	wc.lpszMenuName = NULL;
	wc.lpszClassName = "About:)";
	wc.lpfnWndProc = (WNDPROC) AboutWindowProc;

	RegisterClass(&wc);
// this is standard example two is ex :)

	testwin = CreateWindow("Test:)","Test Window",WS_OVERLAPPEDWINDOW,0,0,640,480,0,0,hInst,0);
	aboutwin = CreateWindow("About:)","About Test", WS_OVERLAPPED | WS_SYSMENU |WS_MINIMIZEBOX,0,0,250,250,0,0,hInst,0);

	ShowWindow(testwin,SW_SHOW);
	UpdateWindow(testwin);

	while(GetMessage(&msg,0,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

// function def's
LRESULT APIENTRY TestWindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);// destroy code out of memory
		break;
	case WM_CREATE:// CREATING ALL THE CHILDREN OF THIS WINDOW
		{
			HWND stx;
			stx = CreateWindow("Static","A Random Static Label",WS_CHILD | WS_VISIBLE ,0,0,200,25,hwnd,0,g_hInst,0);
			static_label = CreateWindow("Static","This static label",WS_CHILD | WS_VISIBLE,205,0,200,25,hwnd,0,g_hInst,0);
			edit = CreateWindow("Edit","A Windows Edit Box",WS_BORDER | WS_VSCROLL | WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL,5,35,200,200,hwnd,(HMENU)ID_EDIT,g_hInst,0);
			list = CreateWindow("ListBox",NULL, LBS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_VSCROLL|WS_BORDER,5,250,200,200,hwnd,(HMENU)ID_LIST,g_hInst,0);
			button = CreateWindow("Button","About",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,350,5,100,25,hwnd,(HMENU)ID_BUTTON,g_hInst,0);
			static_image = CreateWindow("Static",NULL,WS_CHILD | WS_VISIBLE | SS_BITMAP,350,40,0,0,hwnd,(HMENU)ID_IMAGE,g_hInst,0);
			SendMessage(static_image,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)(HBITMAP)LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_BITMAP1)));
			combo = CreateWindow("ComboBox",NULL,WS_BORDER | CBS_DISABLENOSCROLL | WS_CHILD | CBS_DROPDOWNLIST | WS_VISIBLE,350,400,200,200,hwnd,(HMENU)ID_COMBO,g_hInst,0);
			SendMessage(combo,CB_ADDSTRING,255,(LPARAM)(LPCSTR)"addstuff");
		}
		break;
	case WM_COMMAND:
		{
			onmenu(wParam);

			// child notification messages
			switch(HIWORD(wParam))
			{
			case BN_CLICKED:
				switch(LOWORD(wParam))
				{
				case ID_BUTTON:
					{
						// code
						SendMessage(list,LB_ADDSTRING,255,(LPARAM)(LPCSTR)"You clicked the button");
						SendMessage(combo,CB_ADDSTRING,255,(LPARAM)(LPCSTR)"You clicked the button ");
						ShowWindow(aboutwin,SW_SHOW);
					}
					break;

				}
				break;

				case LBN_DBLCLK:
					{
						switch(LOWORD(wParam))
						{
						case ID_LIST:
							{

								MessageBox(hwnd,"Double Clicked the List","HAHA",MB_OK |MB_ICONINFORMATION);
							}
							break;
						}

					}
					break;
				case EN_CHANGE:
					{
						switch(LOWORD(wParam))
						{
						case ID_EDIT:
							{
								MessageBox(hwnd,"Change","Hehe",MB_OK | MB_ICONINFORMATION);
							}
							break;
						}

					}
					break;
		}

		}
		break;
	default: return DefWindowProc(hwnd,msg,wParam,lParam);
	}
	return 0;
}

// on the menu command

void onmenu(WPARAM wParam)
{
	switch(wParam)
	{
	case ID_FILE_EXIT:
		{
			SendMessage(testwin,WM_CLOSE,0,0);
		}
		break;
	case ID_FILE_MSGABOUT:
		{
			MessageBox(0,"About this test program","test",MB_OK|MB_ICONINFORMATION);
		}
		break;
	case ID_FILE_WINDOWABOUT:
		{
			ShowWindow(aboutwin,SW_SHOW);
		}
		break;
	}


}



// about window process
LRESULT APIENTRY AboutWindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		ShowWindow(hwnd,SW_HIDE);
		break;
	case WM_CREATE:
		{
			HWND buttonx;
			buttonx = CreateWindow("Button","Ok!",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,200-55,200,100,25,hwnd,(HMENU)ID_BUTTON2,g_hInst,0);
			HWND stx;
			stx = CreateWindow("Static","Example for my friends, you can do what you need with this. :)",WS_CHILD|WS_VISIBLE,5,5,200,150,hwnd,0,g_hInst,0);

		}
	    	break;
	case WM_COMMAND:
		{
			switch(HIWORD(wParam))
			{
			case BN_CLICKED:
				{
					switch(LOWORD(wParam))
					{
					case ID_BUTTON2:
						{
							SendMessage(hwnd,WM_CLOSE,0,0);
						}
						break;
					}
				}
				break;
			}

		}
		break;
	default: return DefWindowProc(hwnd,msg,wParam,lParam);
	}
	return 0;
}


Hope this will help
Here is about the very simplest way possible to create a dialog box with a functioning menu. There is no extraneous code here. You can compile and run it as is, and you can use it as a base template for future dialog creation applications.

First the header file "win_skel.h"

1
2
3
4
5
6
7
8
9
10
11
#include <Windows.h>
#include <tchar.h>

#define szClassName _T("IDD_VANILLA_DIALOG")
#define szAppName _T("Simple Dialog Window")

#define IDM_DIALOG_MENU 101

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
ATOM DialogRegisterClass(HINSTANCE);
LRESULT CALLBACK DialogWndProc(HWND,UINT,WPARAM,LPARAM);


Now for the resource file "win_skel.rc"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "win_skel.h"

IDD_VANILLA_DIALOG DIALOGEX 0, 0, 186, 105
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_VISIBLE |
		WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_GROUP | WS_SYSMENU
EXSTYLE WS_EX_OVERLAPPEDWINDOW | WS_EX_APPWINDOW
CLASS szClassName
CAPTION "Vanilla Dialog"
FONT 8, "Ms Shell Dlg 2", 400, 0, 1
BEGIN
    DEFPUSHBUTTON   "OK", IDOK, 78, 73, 50, 14
    PUSHBUTTON      "Cancel", IDCANCEL, 132, 73, 50, 14
END

IDM_DIALOG_MENU MENU DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
	MENUITEM "Exit Application"	IDOK
    END
    POPUP "&Edit"
    BEGIN
	MENUITEM "Click On Cancel"	IDCANCEL
    END
END


And now for the final CPP file "win_skel..cpp"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "win_skel.h"

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int iCmdShow)
{
     MSG msg;
     HWND hWinMainWnd;
     TCHAR szNoRegister[]=_T("Unable To Register Window Class! Operation Cancelled.");
	
    if(!DialogRegisterClass(hInstance))
    {
         MessageBox(GetDesktopWindow(),szNoRegister,szAppName,MB_ICONERROR);
         return FALSE;
    }

     hWinMainWnd=CreateDialog(hInstance, szClassName, 0, NULL);

     ShowWindow(hWinMainWnd, iCmdShow);
     UpdateWindow(hWinMainWnd);

     while(GetMessage(&msg, NULL, 0, 0))
     {
	TranslateMessage(&msg);
	DispatchMessage(&msg);
     }

     return (int)msg.wParam;
}

ATOM DialogRegisterClass(HINSTANCE hInst)
{
     WNDCLASSEX wcex;

     wcex.cbSize = sizeof(WNDCLASSEX);
     wcex.style = CS_HREDRAW | CS_VREDRAW;
     wcex.lpfnWndProc = DialogWndProc;
     wcex.cbClsExtra	 = 0;
     wcex.cbWndExtra = DLGWINDOWEXTRA; // this required if dialog box is main window
     wcex.hInstance	= hInst;
     wcex.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
     wcex.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
     wcex.lpszMenuName = MAKEINTRESOURCE(IDM_DIALOG_MENU);
     wcex.lpszClassName = szClassName;
     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	
     return RegisterClassEx(&wcex);
	
}

LRESULT CALLBACK DialogWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_CREATE:
		return FALSE;

	case WM_COMMAND:
	case WM_SYSCOMMAND:
	switch(LOWORD(wParam))
	{
		case IDOK:
		case IDCANCEL:
		     PostQuitMessage(0);
		     return 0;
	}
	break;

	case WM_CLOSE:
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	
	return DefWindowProc(hwnd, message, wParam, lParam);
}


It is as simple as that, and as I said, you can use this as a main dialog template and modify it to your heart is content.

Last edited on
Topic archived. No new replies allowed.