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
|
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#define IDC_MAIN_MDI 300
#define IDC_MAIN_STATUS 301
#define IDC_CHILD_STATIC1 101
#define ID_MDI_FIRSTCHILD 50000
const char MainClass[] = "myWindowClass";
const char ChildClass[] = "myMDIChildWindowClass";
HWND hMDIClientWindow = NULL;
HWND hMainWindow = NULL;
HWND CreateNewMDIChild(HWND hMDIClient)
{
MDICREATESTRUCT mcs;
HWND hChild;
mcs.szTitle = "Act 1";
mcs.szClass = ChildClass;
mcs.hOwner = GetModuleHandle(NULL);
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = MDIS_ALLCHILDSTYLES;
hChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
if(!hChild)
{
MessageBox(hMDIClient, "Act 1 MDI child creation failed !", "Error", MB_ICONERROR | MB_OK);
}
return hChild;
}
LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
// Create Static control
HFONT hfDefault;
HWND hStatic1;
hStatic1 = CreateWindowEx(WS_EX_CLIENTEDGE, "static", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_CHILD_STATIC1,
GetModuleHandle(NULL), NULL);
if(hStatic1 == NULL)
{
MessageBox(hwnd, "Could not create Static control !", "Error",
MB_ICONERROR | MB_OK);
}
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hStatic1, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
break;
case WM_MDIACTIVATE:
{
HMENU hMenu, hFileMenu;
UINT EnableFlag;
hMenu = GetMenu(hMainWindow);
if(hwnd == (HWND)lParam)
{ //being activated, enable the menus
EnableFlag = MF_ENABLED;
}
else
{ //being de-activated, gray the menus
EnableFlag = MF_GRAYED;
}
EnableMenuItem(hMenu, 1, MF_BYPOSITION | EnableFlag);
EnableMenuItem(hMenu, 2, MF_BYPOSITION | EnableFlag);
hFileMenu = GetSubMenu(hMenu, 0);
EnableMenuItem(hFileMenu, ID_FILE_CLOSE, MF_BYCOMMAND | EnableFlag);
DrawMenuBar(hMainWindow);
}
break;
case WM_SIZE:
{
// Calculate remaining height and size edit
HWND hStatic1;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hStatic1 = GetDlgItem(hwnd, IDC_CHILD_STATIC1);
SetWindowPos(hStatic1, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
return DefMDIChildProc(hwnd, msg, wParam, lParam);
default:
return DefMDIChildProc(hwnd, msg, wParam, lParam);
}
return 0;
}
BOOL SetUpMDIChildWindowClass(HINSTANCE hInst)
{
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 3);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 32, 32, 0);
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 32, 32, 0);
wc.hInstance = hInst;
wc.lpfnWndProc = MDIChildWndProc;
wc.lpszClassName = ChildClass;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClassEx(&wc))
{
MessageBox(0, "Could not register child window !", "Error",
MB_ICONERROR | MB_OK);
return FALSE;
}
else
return TRUE;
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
break;
case WM_CREATE:
{
/// Create MDI client ///////////////////////////////////
////////// Find window menu where children will be listed
CLIENTCREATESTRUCT ccs;
ccs.hWindowMenu = GetSubMenu(GetMenu(hwnd), 2);
ccs.idFirstChild = ID_MDI_FIRSTCHILD;
hMDIClientWindow = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient", NULL,
WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, (HMENU)IDC_MAIN_MDI, GetModuleHandle(NULL), (LPVOID)&ccs);
if(hMDIClientWindow == NULL)
{
MessageBox(hwnd, "Could not create MDI client !", "Error", MB_ICONERROR | MB_OK);
}
// Create Status bar
HWND hStatus;
int statwidths[] = {140, 300, -1};
hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
WS_VISIBLE | WS_CHILD | SBARS_SIZEGRIP,
0, 0, 0, 0, hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);
SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)" Author: Me");
SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM)" - MDI2 - ");
SendMessage(hStatus, SB_SETTEXT, 2, (LPARAM)" May 2020");
}
break;
case WM_SIZE:
{
// Size status bar and get height
HWND hStatus;
RECT rcStatus;
int iStatusHeight;
hStatus = GetDlgItem(hwnd, IDC_MAIN_STATUS);
SendMessage(hStatus, WM_SIZE, 0, 0);
GetWindowRect(hStatus, &rcStatus);
iStatusHeight = rcStatus.bottom - rcStatus.top;
// Calculate remaining height and size static
HWND hMDI;
RECT rcClient;
int iMDIHeight;
GetClientRect(hwnd, &rcClient);
iMDIHeight = rcClient.bottom - iStatusHeight;
hMDI = GetDlgItem(hwnd, IDC_MAIN_MDI);
SetWindowPos(hMDI, NULL, 0, 0, rcClient.right, iMDIHeight, SWP_NOZORDER);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_CLOSE:
{
HWND hChild = (HWND)SendMessage(hMDIClientWindow, WM_MDIGETACTIVE, 0, 0);
if(hChild)
{
SendMessage(hChild, WM_CLOSE, 0, 0);
}
}
break;
case ID_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_FILE_ACT1:
CreateNewMDIChild(hMDIClientWindow);
break;
case ID_WINDOW_TILE:
SendMessage(hMDIClientWindow, WM_MDITILE, 0, 0);
break;
case ID_WINDOW_CASCADE:
SendMessage(hMDIClientWindow, WM_MDICASCADE, 0, 0);
break;
default:
{
if(LOWORD(wParam) >= ID_MDI_FIRSTCHILD)
{
DefFrameProc(hwnd, hMDIClientWindow, WM_COMMAND, wParam, lParam);
}
else
{
HWND hChild = (HWND)SendMessage(hMDIClientWindow, WM_MDIGETACTIVE, 0, 0);
if(hChild)
{
SendMessage(hChild, WM_COMMAND, wParam, lParam);
}
}
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefFrameProc(hwnd, hMDIClientWindow, msg, wParam, lParam);
}
return 0;
}
|