Menus

I am working on writing a Win32 Windows Application, and I am very confused. How would I go about adding a button for new file under the "File" menu?

Thanks for the help.

Cameron
Try GUI application...windows forms
Go here and take the tutorial --

http://www.winprog.org/tutorial/
Here's a demo of how to create menues and dialog boxes using the Windows Api. It is a C++ program. The files are Main.cpp, Main.h and Main.rc. You will need to set up a project in whatever development environment you are using and include these files...

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
//Main.cpp
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <tchar.h>
#include <commdlg.h>
#include <string.h>
#include "Main.h"

typedef struct WindowsEventArguments
{
 HWND hWnd;
 WPARAM wParam;
 LPARAM lParam;
 HINSTANCE hIns;
}WndEventArgs,*lpWndEventArgs;


BOOL CALLBACK ModalDlgProc(HWND hwndDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
 TCHAR szName[64],szText[64];
 HWND hwndEdit;
 UINT nID;

 switch(message)
 {
  case WM_INITDIALOG:
    hwndEdit=GetDlgItem(hwndDlg,IDC_NAME);
    SetFocus(hwndEdit);
    return FALSE;
  case WM_COMMAND:
    nID = LOWORD(wParam);
    switch(nID)
    {
     case IDOK:
       hwndEdit=GetDlgItem(hwndDlg, IDC_NAME);
       GetWindowText(hwndEdit,szText,64);
       _tcscpy(szName,_T("Hello, "));
       _tcscat(szName,szText);
       _tcscat(szName,_T("!"));
       MessageBox(hwndDlg,szName,_T("Simple Demo"),MB_OK);
     case IDCANCEL:
       EndDialog(hwndDlg, nID);
       break;
     default:
       break;
    }
    return TRUE;
  default:
    return FALSE;
 }
}


long WndProc_OnCreate(lpWndEventArgs Wea)
{
 HWND hTextBox,hButton;
 DWORD dwStyle;

 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
 dwStyle=WS_CHILD|WS_VISIBLE|WS_BORDER;
 hTextBox=CreateWindowEx(0,_T("edit"),_T(""),dwStyle,10,30,370,20,Wea->hWnd,(HMENU)IDC_EDITBOX1,Wea->hIns,0);
 hButton=CreateWindow(_T("button"),_T("Exit"),WS_CHILD|WS_VISIBLE,168,75,60,25,Wea->hWnd,(HMENU)IDC_BUTTON,Wea->hIns,0);

 return 0;
}


void WndProc_OnCommand_OnOpen(lpWndEventArgs Wea)
{
 static TCHAR szFilter[]=_T("C Files (*.C),CPP Files (*.cpp)\0*.c;*.cpp\0\0");
 static TCHAR szTitleName[_MAX_FNAME+_MAX_EXT];
 static TCHAR szFileName[_MAX_PATH];
 TCHAR lpszBuffer[128];
 OPENFILENAME ofn;

 GetCurrentDirectory(128,lpszBuffer);
 memset(&ofn,'\0',sizeof(OPENFILENAME));
 ofn.lStructSize=sizeof(OPENFILENAME);
 ofn.lpstrFilter = szFilter;
 ofn.nMaxFile=_MAX_PATH;
 ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;
 ofn.lpstrInitialDir = lpszBuffer;
 ofn.lpstrDefExt = _T("cpp");
 ofn.hInstance=Wea->hIns;
 ofn.hwndOwner = Wea->hWnd;
 ofn.Flags=OFN_HIDEREADONLY | OFN_CREATEPROMPT;
 ofn.lpstrFile=szFileName;
 ofn.lpstrFileTitle=szTitleName;
 GetOpenFileName(&ofn);
 SetWindowText(GetDlgItem(Wea->hWnd,IDC_EDITBOX1),ofn.lpstrFile);

 return;
}


void WndProc_OnCommand_OnSave(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,_T("You Chose File >> Save"),_T("Form8"),MB_OK);
}


void WndProc_OnCommand_OnExit(lpWndEventArgs Wea)
{
 SendMessage(Wea->hWnd,WM_DESTROY,0,0L);
}


void WndProc_OnCommand_OnHelp(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,_T("There Is No Help."),_T("Form8"),MB_OK);
}


void WndProc_OnCommand_OnHelpAbout(lpWndEventArgs Wea)
{
 DialogBox(Wea->hIns,MAKEINTRESOURCE(IDD_ABOUT),Wea->hWnd,ModalDlgProc);
}


void WndProc_OnCommand_OnButton_Click(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,_T("You Clicked The Button And Want To Exit!"),_T("Form8"),MB_OK);
 SendMessage(Wea->hWnd,WM_DESTROY,0,0L);
}


long WndProc_OnCommand(lpWndEventArgs Wea)
{
 switch (LOWORD(Wea->wParam))
 {
  case IDM_OPEN:
    WndProc_OnCommand_OnOpen(Wea);
    break;
  case IDM_SAVE:
    WndProc_OnCommand_OnSave(Wea);
    break;
  case IDM_EXIT:
    WndProc_OnCommand_OnExit(Wea);
    break;
  case IDM_HELP:
    WndProc_OnCommand_OnHelp(Wea);
    break;
  case IDM_ABOUT:
    WndProc_OnCommand_OnHelpAbout(Wea);
    break;
  case IDC_BUTTON:
    WndProc_OnCommand_OnButton_Click(Wea);
    break;
 }

 return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
 static WndEventArgs wea;

 switch (iMsg)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return WndProc_OnCreate(&wea);
  case WM_COMMAND:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return WndProc_OnCommand(&wea);
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
 }

 return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrIns,PSTR szCmdLn,int iCmdShow)
{
 TCHAR szAppName[]=_T("SimpleDemo");
 WNDCLASSEX wc;
 HWND hwnd;
 MSG msg;

 wc.lpszClassName=szAppName,                     wc.lpfnWndProc=WndProc;
 wc.cbClsExtra=0,                                wc.cbWndExtra=0;
 wc.cbSize=sizeof(wc),                           wc.style=CS_HREDRAW|CS_VREDRAW;
 wc.hInstance=hIns,                              wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wc.hCursor=LoadCursor(NULL,IDC_ARROW),          wc.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
 wc.lpszMenuName=MAKEINTRESOURCE(IDR_MAIN_MENU), wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
 RegisterClassEx(&wc);
 hwnd=CreateWindow(szAppName,_T("Simple Demo"),WS_OVERLAPPEDWINDOW,200,120,400,175,NULL,0,hIns,NULL);
 ShowWindow(hwnd,iCmdShow);
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return msg.wParam;
}


1
2
3
4
5
6
7
8
9
10
11
12
//Main.h
#define IDC_STATIC      -1
#define IDR_MAIN_MENU   1240
#define IDM_OPEN        1250
#define IDM_SAVE        1252
#define IDM_EXIT        1254
#define IDM_HELP        1260
#define IDM_ABOUT       1262
#define IDC_NAME        1300
#define IDD_ABOUT       1400
#define IDC_EDITBOX1    1500
#define IDC_BUTTON      1510 


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
//Main.rc
#include <windows.h>
#include "Main.h"

IDR_MAIN_MENU MENU
{
 POPUP "&File"
 {
  MENUITEM "&Open...",          IDM_OPEN
  MENUITEM "&Save...",          IDM_SAVE
  MENUITEM SEPARATOR
  MENUITEM "E&xit",             IDM_EXIT
 }
 POPUP "&Help"
 {
  MENUITEM "&Help...",          IDM_HELP
  MENUITEM "&About Form8...",   IDM_ABOUT
 }
}

IDD_ABOUT DIALOG DISCARDABLE 30,60,180,36
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Identify Your Sorry Self!"
FONT 8, "MS Sans Serif"
BEGIN
  DEFPUSHBUTTON  "OK",IDOK,120,20,40,14
  PUSHBUTTON     "Cancel",IDCANCEL,75,20,40,14
  LTEXT          "Enter Your Name",IDC_STATIC,7,4,60,8
  EDITTEXT       IDC_NAME,65,4,100,12,ES_AUTOHSCROLL
END


The program also shows how to use an 'OpenFile Dialog Box'.
Last edited on
Topic archived. No new replies allowed.