-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDIChWnd.c
264 lines (218 loc) · 7.01 KB
/
MDIChWnd.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdlib.h>
#include "Globals.h"
#include "MainWnd.h"
#include "MDIChWnd.h"
#include "Resource.h"
/* MDI child window class and title */
static const char MDIChildWndClass[] = "Win16 MDI Example Application - Child";
/* Window procedure for our MDI child window */
LRESULT CALLBACK MDIChildWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
{
WORD id = wParam;
switch (id)
{
case ID_FILE_SAVE:
{
MDIChildSave(hWnd);
return 0;
}
case ID_FILE_SAVEAS:
{
MDIChildSaveAs(hWnd);
return 0;
}
}
break;
}
/* An MDI child window is being activated */
case WM_MDIACTIVATE:
{
HMENU hParentMenu, hParentFileMenu;
UINT enableMenu;
HWND hActivatedChild = (HWND)lParam;
/* If this window is the one being activated, enable its menus */
if (hWnd == hActivatedChild)
{
enableMenu = MF_ENABLED;
}
else
{
enableMenu = MF_GRAYED;
}
/* Get menu of MDI frame window */
hParentMenu = GetMenu(g_hMainWindow);
/* Enable / disable the "window" menu */
EnableMenuItem(hParentMenu, 1, MF_BYPOSITION | enableMenu);
/* Enable / disable the save and close menu items */
hParentFileMenu = GetSubMenu(hParentMenu, 0);
EnableMenuItem(hParentFileMenu, ID_FILE_SAVE, MF_BYCOMMAND | enableMenu);
EnableMenuItem(hParentFileMenu, ID_FILE_SAVEAS, MF_BYCOMMAND | enableMenu);
EnableMenuItem(hParentFileMenu, ID_FILE_CLOSE, MF_BYCOMMAND | enableMenu);
EnableMenuItem(hParentFileMenu, ID_FILE_CLOSEALL, MF_BYCOMMAND | enableMenu);
/* Redraw the updated menu */
DrawMenuBar(g_hMainWindow);
return 0;
}
case WM_CREATE:
{
/* Allocate child window data */
HLOCAL hChildData = LocalAlloc(LHND, sizeof(MdiChildData));
/* Fail window creation if allocation failed */
if (!hChildData)
return -1;
/* Associate child window data with window */
SetWindowLong(hWnd, DWL_USER, (UINT)hChildData);
return 0;
}
case WM_DESTROY:
{
/* Free child window data */
HLOCAL hChildData = (HLOCAL)GetWindowLong(hWnd, DWL_USER);
if (hChildData)
LocalFree(hChildData);
return 0;
}
}
return DefMDIChildProc(hWnd, msg, wParam, lParam);
}
/* Register a class for our main window */
BOOL RegisterMDIChildWindowClass()
{
WNDCLASS wc;
/* Class for our MDI child window */
wc.style = 0;
wc.lpfnWndProc = &MDIChildWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hInstance;
wc.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_APPICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
wc.lpszClassName = MDIChildWndClass;
return (RegisterClass(&wc)) ? TRUE : FALSE;
}
/* Create a new MDI child window */
void MDIChildNew(HWND hMDIClient)
{
MDICREATESTRUCT mcs;
static unsigned int counter = 0;
char title[16];
HWND hWndChild;
/* Increment counter, but ensure it doesn't become longer than the buffer */
counter = (counter % 9999) + 1;
/* Create the window title */
wsprintf(title, "Untitled - %u", counter);
/* Set MDI child properties */
mcs.szTitle = title;
mcs.szClass = MDIChildWndClass;
mcs.hOwner = g_hInstance;
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = 0;
/* Create the MDI child */
hWndChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LPARAM)(LPMDICREATESTRUCT)&mcs);
if (hWndChild)
{
HLOCAL hChildData;
MdiChildData* childData;
/* Mark document as unsaved */
hChildData = (HLOCAL)GetWindowLong(hWndChild, DWL_USER);
childData = (MdiChildData*)LocalLock(hChildData);
childData->IsUnSaved = TRUE;
LocalUnlock(hChildData);
}
else
MessageBox(NULL, "Error creating new document.", "Error", MB_ICONHAND | MB_OK);
}
/* Open a document in a new MDI child window */
void MDIChildOpen(HWND hMDIClient)
{
OPENFILENAME ofn = { 0 };
char fileName[_MAX_PATH] = "";
char fileTitle[_MAX_FNAME + _MAX_EXT] = "";
MDICREATESTRUCT mcs;
/* Set open file dialog properties */
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hMDIClient;
ofn.lpstrFilter = "Text Documents (*.txt)\0.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = fileName;
ofn.lpstrFileTitle = fileTitle;
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
ofn.nMaxFile = _MAX_PATH;
ofn.Flags = OFN_NOTESTFILECREATE | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
/* Show open file dialog */
if (GetOpenFileName(&ofn))
{
HWND hWndChild;
/* Set MDI child properties */
mcs.szTitle = fileTitle;
mcs.szClass = MDIChildWndClass;
mcs.hOwner = g_hInstance;
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = 0;
/* Create the MDI child */
hWndChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LPARAM)(LPMDICREATESTRUCT)&mcs);
if (hWndChild)
{
/* TODO: Add file opening code */
}
else
MessageBox(NULL, "Error opening document.", "Error", MB_ICONHAND | MB_OK);
}
}
/* Save a document in an MDI child window */
void MDIChildSave(HWND hMDIChild)
{
HLOCAL hChildData;
MdiChildData* childData;
/* Mark document as unsaved */
hChildData = (HLOCAL)GetWindowLong(hMDIChild, DWL_USER);
childData = (MdiChildData*)LocalLock(hChildData);
/* If this is an unsaved document, do a save as */
if (childData->IsUnSaved)
{
MDIChildSaveAs(hMDIChild);
}
else
{
/* TODO: Add file saving code */
}
LocalUnlock(hChildData);
}
/* Save a document in an MDI child window with a filename */
void MDIChildSaveAs(HWND hMDIChild)
{
OPENFILENAME ofn = { 0 };
char fileName[_MAX_PATH] = "";
char fileTitle[_MAX_FNAME + _MAX_EXT] = "";
/* Set save file dialog properties */
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hMDIChild;
ofn.lpstrFilter = "Text Documents (*.txt)\0.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = fileName;
ofn.lpstrFileTitle = fileTitle;
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
ofn.nMaxFile = _MAX_PATH;
ofn.Flags = OFN_NOTESTFILECREATE | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = "txt";
if (GetSaveFileName(&ofn))
{
HLOCAL hChildData;
MdiChildData* childData;
/* TODO: Add file saving code */
/* Update window title with the filename */
SetWindowText(hMDIChild, ofn.lpstrFileTitle);
/* Mark document as saved */
hChildData = (HLOCAL)GetWindowLong(hMDIChild, DWL_USER);
childData = (MdiChildData*)LocalLock(hChildData);
childData->IsUnSaved = FALSE;
LocalUnlock(hChildData);
}
}