#include<windows.h>

#include"resource.h"

 

#define IDM_FIRSTCHILD 0x00001000

#define MIN          0

#define RES          1

#define CLOSE 2

 

LRESULT CALLBACK FrameWndProc(HWND,UINT,WPARAM,LPARAM);

LRESULT CALLBACK DocProc(HWND,UINT,WPARAM,LPARAM);

BOOL CALLBACK EnumChildProc(HWND,LPARAM);

int MyRegisterWC(WNDPROC,LPCTSTR,HBRUSH);

 

char szFrameClassName[]="frame";

char szChildDoc[]="document";

 

HMENU hMenuFirst,hMenuDoc;

HMENU hMenuFirstWnd,hMenuDocWnd;

HINSTANCE hInst;

 

int doc_no;

 

int WINAPI WinMain(HINSTANCE hCurInst,HINSTANCE hPrevInst,

                   LPSTR lpsCmdLine,int nCmdShow)

{

    MSG msg;

    HWND hFrame,hClient;

 

    hInst=hCurInst;

   

    if(!MyRegisterWC(FrameWndProc,szFrameClassName,(HBRUSH)(COLOR_APPWORKSPACE+1)))

        return FALSE;

    if(!MyRegisterWC(DocProc,szChildDoc,(HBRUSH)GetStockObject(WHITE_BRUSH)))

        return FALSE;

 

    hMenuFirst=LoadMenu(hInst,"MYMENU");

    hMenuFirstWnd=GetSubMenu(hMenuFirst,0);

    hMenuDoc=LoadMenu(hInst,"MYDOCUMENT");

    hMenuDocWnd=GetSubMenu(hMenuDoc,1);

 

    hFrame=CreateWindow(

        szFrameClassName,

        "MDI—Œ`",

        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,

        CW_USEDEFAULT,

        CW_USEDEFAULT,

        CW_USEDEFAULT,

        CW_USEDEFAULT,

        NULL,

        hMenuFirst,

        hInst,

        NULL);

    hClient=GetWindow(hFrame,GW_CHILD);

    ShowWindow(hFrame,nCmdShow);

    UpdateWindow(hFrame);

 

    while (GetMessage(&msg,NULL,0,0)){

        if (!TranslateMDISysAccel(hClient,&msg)){

            TranslateMessage(&msg);

            DispatchMessage(&msg);

        }

    }

    DestroyMenu(hMenuFirst);

    DestroyMenu(hMenuDoc);

    return (int)msg.wParam;

}

 

LRESULT CALLBACK FrameWndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)

{

    static HWND hClient;

    HWND hChild;

    CLIENTCREATESTRUCT ccs;

    MDICREATESTRUCT mdic;

    char str[64],*str_org="ƒhƒLƒ…ƒƒ“ƒg No. = %d";

 

    switch(msg){

        case WM_CREATE:

            ccs.hWindowMenu=hMenuFirstWnd;

            ccs.idFirstChild=IDM_FIRSTCHILD;

            hClient=CreateWindow(

                            "MDICLIENT",

                NULL,

                WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,

                0,0,0,0,

                hWnd,

                (HMENU)1,

                hInst,

                (LPVOID)&ccs);

            return 0;

        case WM_COMMAND:

            switch(LOWORD(wp)){

                            case IDM_NEW:

                    doc_no++;

                    wsprintf(str,str_org,doc_no);

                    mdic.szClass=szChildDoc;

                    mdic.szTitle=str;

                    mdic.hOwner=hInst;

                    mdic.x=CW_USEDEFAULT;

                    mdic.y=CW_USEDEFAULT;

                    mdic.cx=CW_USEDEFAULT;

                    mdic.cy=CW_USEDEFAULT;

                    mdic.style=0;

                    mdic.lParam=0;

                    hChild=(HWND)SendMessage(hClient,WM_MDICREATE,0,(LPARAM)&mdic);

                    return 0;

                case IDM_CLOSE:

                    hChild=(HWND)SendMessage(hClient,WM_MDIGETACTIVE,0,0);

                    if (hChild)

                        SendMessage(hClient,WM_MDIDESTROY,(WPARAM)hChild,0);

                    return 0;

                case IDM_EXIT:

                    SendMessage(hWnd,WM_CLOSE,0,0);

                    return 0;

                case IDM_CASCADE:

                    SendMessage(hClient,WM_MDICASCADE,0,0);

                    return 0;

                case IDM_TILE:

                    SendMessage(hClient,WM_MDITILE,0,0);

                    return 0;

                case IDM_HTILE:

                    SendMessage(hClient,WM_MDITILE,MDITILE_HORIZONTAL,0);

                    return 0;

                case IDM_VTILE:

                    SendMessage(hClient,WM_MDITILE,MDITILE_VERTICAL,0);

                    return 0;

                case IDM_ARRANGE:

                    SendMessage(hClient,WM_MDIICONARRANGE,0,0);

                    return 0;

                            case IDM_MINIMIZEALL:

                                   EnumChildWindows(hClient,EnumChildProc,MIN);

                    return 0;

                            case IDM_RESTOREALL:

                                   EnumChildWindows(hClient,EnumChildProc,RES);

                    return 0;

                case IDM_CLOSEALL:

                    EnumChildWindows(hClient,EnumChildProc,CLOSE);

                    return 0;

                default:

                    hChild=(HWND)SendMessage(hClient,WM_MDIGETACTIVE,0,0);

                    if(IsWindow(hChild))

                        SendMessage(hChild,WM_COMMAND,wp,lp);

                    break;

            }

            break;

        case WM_QUERYENDSESSION:

        case WM_CLOSE:

            SendMessage(hWnd,WM_COMMAND,IDM_CLOSEALL,0);

            if (GetWindow(hClient,GW_CHILD))

                return 0;

            break;

        case WM_DESTROY:

            PostQuitMessage(0);

            return 0;

    }

    return DefFrameProc(hWnd,hClient,msg,wp,lp);

}

 

LRESULT CALLBACK DocProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)

{

       HDC hdc;

       PAINTSTRUCT ps;

 

    static HWND hClient,hFrame;

 

    switch(msg){

        case WM_CREATE:

            hClient=GetParent(hWnd);

            hFrame=GetParent(hClient);

            return 0;

              case WM_PAINT:

                     hdc=BeginPaint(hWnd,&ps);

                     EndPaint(hWnd,&ps);

                     return 0;

        case WM_MDIACTIVATE:

            if(lp==(LPARAM)hWnd)

                SendMessage(hClient,WM_MDISETMENU,(WPARAM)hMenuDoc,(LPARAM)hMenuDocWnd);

            else

                SendMessage(hClient,WM_MDISETMENU,(WPARAM)hMenuFirst,(LPARAM)hMenuFirstWnd);

            DrawMenuBar(hFrame);

            return 0;

    }

    return DefMDIChildProc(hWnd,msg,wp,lp);

}

 

BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lp)

{

       switch(lp){

              case MIN:

                     ShowWindow(hWnd,SW_MINIMIZE);

                     break;

              case RES:

                     SendMessage(GetParent(hWnd),WM_MDIRESTORE,(WPARAM)hWnd,0);

                     break;

              case CLOSE:

            SendMessage(GetParent(hWnd),WM_MDIDESTROY,(WPARAM)hWnd,0);

                     break;

       }

       return TRUE;

}

 

int MyRegisterWC(WNDPROC lpfnWndProc,LPCTSTR lpszClassName,HBRUSH hbrBack)

{

    WNDCLASSEX wc;

 

    wc.cbSize=sizeof(WNDCLASSEX);

    wc.style=CS_HREDRAW | CS_VREDRAW;

    wc.lpfnWndProc=lpfnWndProc;

    wc.cbClsExtra=0;

    wc.cbWndExtra=0;

    wc.hInstance=hInst;

    wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);

    wc.hCursor=LoadCursor(NULL,IDC_ARROW);

    wc.hbrBackground=hbrBack;

    wc.lpszMenuName=NULL;

    wc.lpszClassName=lpszClassName;

    wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);

 

    return RegisterClassEx(&wc);

}