Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <windows.h>
#include <commctrl.h>
#include "mdi_unit.h"
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT Message, WPARAM wParam,
LPARAM lParam);
ZeroMemory(&ofn, sizeof(ofn));
pszFileName[0] = 0;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
ofn.lpstrFile = pszFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
if(bSave)
{
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT;
if(!GetSaveFileName(&ofn))
return FALSE;
}
else
{
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(!GetOpenFileName(&ofn))
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
MSG Msg;
WNDCLASSEX WndClassEx;
InitCommonControls();
g_hInst = hInstance;
WndClassEx.cbSize = sizeof(WNDCLASSEX);
WndClassEx.style = CS_HREDRAW | CS_VREDRAW;
WndClassEx.lpfnWndProc = WndProc;
WndClassEx.cbClsExtra = 0;
WndClassEx.cbWndExtra = 0;
WndClassEx.hInstance = hInstance;
WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClassEx.hbrBackground = (HBRUSH)(COLOR_3DSHADOW+1);
WndClassEx.lpszMenuName = "MAIN";
WndClassEx.lpszClassName = g_szAppName;
WndClassEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClassEx))
{
MessageBox(0, "Could Not Register Window", "Oh Oh...",
MB_ICONEXCLAMATION | MB_OK);
return -1;
}
WndClassEx.lpfnWndProc = MDIChildWndProc;
WndClassEx.lpszMenuName = NULL;
WndClassEx.lpszClassName = g_szChild;
WndClassEx.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
if(!RegisterClassEx(&WndClassEx))
{
MessageBox(0, "Could Not Register Child Window", "Oh Oh...",
MB_ICONEXCLAMATION | MB_OK);
return -1;
}
if (g_hMainWindow == NULL){
MessageBox(0, "No Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
return -1;
}
ShowWindow(g_hMainWindow, nCmdShow);
UpdateWindow(g_hMainWindow);
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
{
CLIENTCREATESTRUCT ccs;
int iStatusWidths[] = {200, 300, -1};
TBADDBITMAP tbab;
TBBUTTON tbb[9];
tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(g_hToolBar, TB_ADDBITMAP, 0, (LPARAM)&tbab);
ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = CM_FILE_NEW;
tbb[1].iBitmap = STD_FILEOPEN;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = CM_FILE_OPEN;
tbb[2].iBitmap = STD_FILESAVE;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].idCommand = CM_FILE_SAVE;
tbb[3].fsStyle = TBSTYLE_SEP;
tbb[4].iBitmap = STD_CUT;
tbb[4].fsStyle = TBSTYLE_BUTTON;
tbb[4].idCommand = CM_EDIT_CUT;
tbb[5].iBitmap = STD_COPY;
tbb[5].fsStyle = TBSTYLE_BUTTON;
tbb[5].idCommand = CM_EDIT_COPY;
tbb[6].iBitmap = STD_PASTE;
tbb[6].fsStyle = TBSTYLE_BUTTON;
tbb[6].idCommand = CM_EDIT_PASTE;
tbb[7].fsStyle = TBSTYLE_SEP;
tbb[8].iBitmap = STD_UNDO;
tbb[8].fsStyle = TBSTYLE_BUTTON;
tbb[8].idCommand = CM_EDIT_UNDO;
return 0;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case CM_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case CM_FILE_NEW:
{
MDICREATESTRUCT mcs;
HWND hChild;
mcs.szTitle = "[Untitled]";
mcs.szClass = g_szChild;
mcs.hOwner = g_hInst;
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = MDIS_ALLCHILDSTYLES;
mcs.szTitle = szFileName;
mcs.szClass = g_szChild;
mcs.hOwner = g_hInst;
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = MDIS_ALLCHILDSTYLES;
if(!hChild)
{
MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...",
MB_ICONEXCLAMATION | MB_OK);
}
}
break;
case CM_WINDOW_TILEHORZ:
PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
break;
case CM_WINDOW_TILEVERT:
PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
break;
case CM_WINDOW_CASCADE:
PostMessage(g_hMDIClient, WM_MDICASCADE, 0, 0);
break;
case CM_WINDOW_ARRANGE:
PostMessage(g_hMDIClient, WM_MDIICONARRANGE, 0, 0);
break;
default:
{
if(LOWORD(wParam) >= ID_MDI_FIRSTCHILD){
DefFrameProc(hwnd, g_hMDIClient, Message, wParam, lParam);
}
else {
HWND hChild;
hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
if(hChild){
SendMessage(hChild, WM_COMMAND, wParam, lParam);
}
}
}
}
}
break;
case WM_SIZE:
{
RECT rectClient, rectStatus, rectTool;
UINT uToolHeight, uStatusHeight, uClientAlreaHeight;
SendMessage(hEdit, WM_SETFONT,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
hMenu = GetMenu(g_hMainWindow);
if(hwnd == (HWND)lParam){ //being activated
EnableFlag = TRUE;
}
else{
EnableFlag = FALSE; //being de-activated
}
EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED :
MF_GRAYED));
EnableMenuItem(hMenu, 2, MF_BYPOSITION | (EnableFlag ? MF_ENABLED :
MF_GRAYED));
DrawMenuBar(g_hMainWindow);