⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdidemo.c

📁 Programming Windows 程序开发设计指南
💻 C
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------
   MDIDEMO.C -- Multiple Document Interface Demonstration
                (c) Charles Petzold, 1996
  --------------------------------------------------------*/

#include <windows.h>
#include <stdlib.h>
#include "mdidemo.h"

LRESULT CALLBACK FrameWndProc  (HWND, UINT, WPARAM, LPARAM) ;
BOOL    CALLBACK CloseEnumProc (HWND, LPARAM) ;
LRESULT CALLBACK HelloWndProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK RectWndProc   (HWND, UINT, WPARAM, LPARAM) ;

          // structure for storing data unique to each Hello child window

typedef struct tagHELLODATA
     {
     UINT     iColor ;
     COLORREF clrText ;
     }
     HELLODATA, *LPHELLODATA ;

          // structure for storing data unique to each Rect child window

typedef struct tagRECTDATA
     {
     short cxClient ;
     short cyClient ;
     }
     RECTDATA, *LPRECTDATA ;

          // global variables

char      szFrameClass[] = "MdiFrame" ;
char      szHelloClass[] = "MdiHelloChild" ;
char      szRectClass[]  = "MdiRectChild" ;
HINSTANCE hInst ;
HMENU     hMenuInit, hMenuHello, hMenuRect ;
HMENU     hMenuInitWindow, hMenuHelloWindow, hMenuRectWindow ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     {
     HACCEL      hAccel ;
     HWND        hwndFrame, hwndClient ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;

     hInst = hInstance ;

     if (!hPrevInstance) 
          {
                    // Register the frame window class

          wndclass.cbSize        = sizeof (wndclass) ;
          wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
          wndclass.lpfnWndProc   = FrameWndProc ;
          wndclass.cbClsExtra    = 0 ;
          wndclass.cbWndExtra    = 0 ;
          wndclass.hInstance     = hInstance ;
          wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
          wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
          wndclass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1) ;
          wndclass.lpszMenuName  = NULL ;
          wndclass.lpszClassName = szFrameClass ;
          wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

          RegisterClassEx (&wndclass) ;

                    // Register the Hello child window class

          wndclass.cbSize        = sizeof (wndclass) ;
          wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
          wndclass.lpfnWndProc   = HelloWndProc ;
          wndclass.cbClsExtra    = 0 ;
          wndclass.cbWndExtra    = sizeof (HANDLE) ;
          wndclass.hInstance     = hInstance ;
          wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
          wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
          wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
          wndclass.lpszMenuName  = NULL ;
          wndclass.lpszClassName = szHelloClass ;
          wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

          RegisterClassEx (&wndclass) ;

                    // Register the Rect child window class

          wndclass.cbSize        = sizeof (wndclass) ;
          wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
          wndclass.lpfnWndProc   = RectWndProc ;
          wndclass.cbClsExtra    = 0 ;
          wndclass.cbWndExtra    = sizeof (HANDLE) ;
          wndclass.hInstance     = hInstance ;
          wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
          wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
          wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
          wndclass.lpszMenuName  = NULL ;
          wndclass.lpszClassName = szRectClass ;
          wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

          RegisterClassEx (&wndclass) ;
          }
               // Obtain handles to three possible menus & submenus

     hMenuInit  = LoadMenu (hInst, "MdiMenuInit") ;
     hMenuHello = LoadMenu (hInst, "MdiMenuHello") ;
     hMenuRect  = LoadMenu (hInst, "MdiMenuRect") ;

     hMenuInitWindow  = GetSubMenu (hMenuInit,   INIT_MENU_POS) ;
     hMenuHelloWindow = GetSubMenu (hMenuHello, HELLO_MENU_POS) ;
     hMenuRectWindow  = GetSubMenu (hMenuRect,   RECT_MENU_POS) ;

               // Load accelerator table

     hAccel = LoadAccelerators (hInst, "MdiAccel") ;

               // Create the frame window

     hwndFrame = CreateWindow (szFrameClass, "MDI Demonstration",
                               WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                               CW_USEDEFAULT, CW_USEDEFAULT,
                               CW_USEDEFAULT, CW_USEDEFAULT,
                               NULL, hMenuInit, hInstance, NULL) ;

     hwndClient = GetWindow (hwndFrame, GW_CHILD) ;

     ShowWindow (hwndFrame, iCmdShow) ;
     UpdateWindow (hwndFrame) ;

               // Enter the modified message loop

     while (GetMessage (&msg, NULL, 0, 0))
          {
          if (!TranslateMDISysAccel (hwndClient, &msg) &&
              !TranslateAccelerator (hwndFrame, hAccel, &msg))
               {
               TranslateMessage (&msg) ;
               DispatchMessage (&msg) ;
               }
          }
               // Clean up by deleting unattached menus

     DestroyMenu (hMenuHello) ;
     DestroyMenu (hMenuRect) ;

     return msg.wParam ;
     }

LRESULT CALLBACK FrameWndProc (HWND hwnd, UINT iMsg, WPARAM wParam,
                                                     LPARAM lParam)
     {
     static HWND         hwndClient ;
     CLIENTCREATESTRUCT  clientcreate ;
     HWND                hwndChild ;
     MDICREATESTRUCT     mdicreate ;

     switch (iMsg)
          {
          case WM_CREATE :          // Create the client window

               clientcreate.hWindowMenu  = hMenuInitWindow ;
               clientcreate.idFirstChild = IDM_FIRSTCHILD ;

               hwndClient = CreateWindow ("MDICLIENT", NULL,
                              WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
                              0, 0, 0, 0, hwnd, (HMENU) 1, hInst,
                              (LPSTR) &clientcreate) ;
               return 0 ;

          case WM_COMMAND :
               switch (wParam)
                    {
                    case IDM_NEWHELLO :       // Create a Hello child window

                         mdicreate.szClass = szHelloClass ;
                         mdicreate.szTitle = "Hello" ;
                         mdicreate.hOwner  = hInst ;
                         mdicreate.x       = CW_USEDEFAULT ;
                         mdicreate.y       = CW_USEDEFAULT ;
                         mdicreate.cx      = CW_USEDEFAULT ;
                         mdicreate.cy      = CW_USEDEFAULT ;
                         mdicreate.style   = 0 ;
                         mdicreate.lParam  = 0 ;

                         hwndChild = (HWND) SendMessage (hwndClient,
                                        WM_MDICREATE, 0,
                                        (LPARAM) (LPMDICREATESTRUCT) &mdicreate) ;
                         return 0 ;

                    case IDM_NEWRECT :        // Create a Rect child window

                         mdicreate.szClass = szRectClass ;
                         mdicreate.szTitle = "Rectangles" ;
                         mdicreate.hOwner  = hInst ;
                         mdicreate.x       = CW_USEDEFAULT ;
                         mdicreate.y       = CW_USEDEFAULT ;
                         mdicreate.cx      = CW_USEDEFAULT ;
                         mdicreate.cy      = CW_USEDEFAULT ;
                         mdicreate.style   = 0 ;
                         mdicreate.lParam  = 0 ;

                         hwndChild = (HWND) SendMessage (hwndClient,
                                        WM_MDICREATE, 0,
                                        (LPARAM) (LPMDICREATESTRUCT) &mdicreate) ;
                         return 0 ;

                    case IDM_CLOSE :          // Close the active window

                         hwndChild = (HWND) SendMessage (hwndClient,
                                                  WM_MDIGETACTIVE, 0, 0) ;

                         if (SendMessage (hwndChild, WM_QUERYENDSESSION, 0, 0))
                              SendMessage (hwndClient, WM_MDIDESTROY,
                                           (WPARAM) hwndChild, 0) ;
                         return 0 ;

                    case IDM_EXIT :           // Exit the program

                         SendMessage (hwnd, WM_CLOSE, 0, 0) ;
                         return 0 ;

                                   // messages for arranging windows
                    case IDM_TILE :
                         SendMessage (hwndClient, WM_MDITILE, 0, 0) ;
                         return 0 ;

                    case IDM_CASCADE :
                         SendMessage (hwndClient, WM_MDICASCADE, 0, 0) ;
                         return 0 ;

                    case IDM_ARRANGE :
                         SendMessage (hwndClient, WM_MDIICONARRANGE, 0, 0) ;
                         return 0 ;

                    case IDM_CLOSEALL :       // Attempt to close all children

                         EnumChildWindows (hwndClient, &CloseEnumProc, 0) ;
                         return 0 ;

                    default :            // Pass to active child...

                         hwndChild = (HWND) SendMessage (hwndClient,
                                                  WM_MDIGETACTIVE, 0, 0) ;

                        if (IsWindow (hwndChild))
                             SendMessage (hwndChild, WM_COMMAND,
                                          wParam, lParam) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -