MDI (Multiple Document Interface) Demo
#include <windows.h>
#include <commctrl.h>
#define IDI_ICON 101
#define IDC_CHILD_EDIT 200
#define IDM_FILE_EXIT 1000
#define IDM_FILE_NEW 1001
#define ID_MDI_CLIENT 1002
#define ID_MDI_FIRSTCHILD 1003
const TCHAR MainClassName[] = TEXT("MainWindowClass");
const TCHAR ChildClassName[] = TEXT("ChildWindowClass");
HWND hWndClient = NULL;
// Forward declarations
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildProc(HWND, UINT, WPARAM, LPARAM);
//------------------------------------------------------
// MDI CHILD WINDOW PROCEDURE
//------------------------------------------------------
LRESULT CALLBACK ChildProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CREATE:
{
HWND hEdit;
hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("EDIT"),
TEXT(""),
WS_CHILD |
WS_VISIBLE |
WS_VSCROLL |
WS_HSCROLL |
ES_MULTILINE |
ES_AUTOVSCROLL |
ES_AUTOHSCROLL,
0,
0,
100,
100,
hWnd,
(HMENU)IDC_CHILD_EDIT,
GetModuleHandle(NULL),
NULL
);
if(hEdit == NULL)
{
MessageBox(
hWnd,
TEXT("Could not create edit control"),
TEXT("Error"),
MB_OK
);
}
}
break;
case WM_SIZE:
{
RECT rc;
HWND hEdit;
GetClientRect(hWnd,&rc);
hEdit = GetDlgItem(hWnd,IDC_CHILD_EDIT);
if(hEdit)
{
SetWindowPos(
hEdit,
NULL,
0,
0,
rc.right,
rc.bottom,
SWP_NOZORDER
);
}
}
break;
case WM_MDIACTIVATE:
{
// Code here runs when child activates
}
break;
}
return DefMDIChildProc(hWnd,Msg,wParam,lParam);
}
//------------------------------------------------------
// MAIN FRAME WINDOW PROCEDURE
//------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CREATE:
{
HMENU hMenuBar;
HMENU hFileMenu;
hMenuBar = CreateMenu();
hFileMenu = CreateMenu();
AppendMenu(
hFileMenu,
MF_STRING,
IDM_FILE_NEW,
TEXT("&New")
);
AppendMenu(
hFileMenu,
MF_STRING,
IDM_FILE_EXIT,
TEXT("&Exit")
);
AppendMenu(
hMenuBar,
MF_POPUP,
(UINT_PTR)hFileMenu,
TEXT("&File")
);
SetMenu(hWnd,hMenuBar);
CLIENTCREATESTRUCT ccs;
ccs.hWindowMenu =
GetSubMenu(hMenuBar,0);
ccs.idFirstChild =
ID_MDI_FIRSTCHILD;
hWndClient = CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("MDICLIENT"),
NULL,
WS_CHILD |
WS_VISIBLE |
WS_CLIPCHILDREN |
WS_VSCROLL |
WS_HSCROLL,
0,
0,
0,
0,
hWnd,
(HMENU)ID_MDI_CLIENT,
GetModuleHandle(NULL),
(LPVOID)&ccs
);
if(hWndClient == NULL)
{
MessageBox(
hWnd,
TEXT("Cannot create MDI client"),
TEXT("Error"),
MB_OK
);
}
}
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDM_FILE_NEW:
{
HWND hChild;
hChild = CreateWindowEx(
WS_EX_MDICHILD,
ChildClassName,
TEXT("Document"),
WS_CHILD |
WS_VISIBLE |
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
300,
200,
hWndClient,
NULL,
GetModuleHandle(NULL),
NULL
);
if(hChild == NULL)
{
MessageBox(
hWnd,
TEXT("Cannot create child window"),
TEXT("Error"),
MB_OK
);
}
}
break;
case IDM_FILE_EXIT:
SendMessage(
hWnd,
WM_CLOSE,
0,
0
);
break;
default:
if(LOWORD(wParam)>=ID_MDI_FIRSTCHILD)
{
DefFrameProc(
hWnd,
hWndClient,
Msg,
wParam,
lParam
);
}
break;
}
}
break;
case WM_SIZE:
if(hWndClient)
{
MoveWindow(
hWndClient,
0,
0,
LOWORD(lParam),
HIWORD(lParam),
TRUE
);
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefFrameProc(
hWnd,
hWndClient,
Msg,
wParam,
lParam
);
}
return 0;
}
//------------------------------------------------------
// PROGRAM ENTRY
//------------------------------------------------------
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wc;
MSG Msg;
HWND hWnd;
ZeroMemory(&wc,sizeof(wc));
//--------------------------------------------------
// Register MAIN frame window
//--------------------------------------------------
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon =
LoadIcon(NULL,IDI_APPLICATION);
wc.hIconSm =
LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor =
LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =
(HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName =
MainClassName;
if(!RegisterClassEx(&wc))
{
return 0;
}
//--------------------------------------------------
// Register MDI CHILD window
//--------------------------------------------------
ZeroMemory(&wc,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = ChildProc;
wc.hInstance = hInstance;
wc.hIcon =
LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor =
LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =
(HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName =
ChildClassName;
if(!RegisterClassEx(&wc))
{
return 0;
}
//--------------------------------------------------
// Create main frame window
//--------------------------------------------------
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
MainClassName,
TEXT("MDI Application"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
700,
500,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return 0;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
//--------------------------------------------------
// Message loop
//--------------------------------------------------
while(GetMessage(&Msg,NULL,0,0))
{
if(!TranslateMDISysAccel(hWndClient,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}