MEMORY.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:39k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /******************************************************************************
  2. *       This is a part of the Microsoft Source Code Samples.
  3. *       Copyright (C) 1993-1997 Microsoft Corporation.
  4. *       All rights reserved.
  5. *       This source code is only intended as a supplement to
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the
  8. *       Microsoft samples programs.
  9. ******************************************************************************/
  10. /****************************** Module Header ******************************
  11. * Module Name: memory.c
  12. ***************************************************************************/
  13. #include <windows.h>
  14. #include "memory.h"
  15. HANDLE ghModule;
  16. HWND   ghwndMain = NULL;
  17. HWND   ghwndClient;
  18. HMENU  hMenu,       hMenuWindow;
  19. HMENU  hServerMenu, hServerMenuWindow;
  20. HMENU  hClientMenu, hClientMenuWindow;
  21. CHAR   gszFile[20];
  22. CHAR   gszMapName[20];
  23. typedef struct _PerWndInfo
  24. {
  25.     HWND    hParent;
  26.     HWND    hThisWnd;           // Used in locating the node
  27.     RECT    rcClient;
  28.     char    CaptionBarText[SIZEOFCAPTIONTEXT];
  29. } PERWNDINFO, *PPERWNDINFO;
  30. typedef struct _node
  31. {
  32.    PERWNDINFO      ChildWnd;
  33.    HANDLE          hNext;
  34. } NODE, *PNODE;
  35. /*
  36.  * Forward declarations.
  37.  */
  38. BOOL InitializeApp   (void);
  39. LONG APIENTRY MainWndProc     (HWND, UINT, DWORD, LONG);
  40. LONG APIENTRY ServerWndProc   (HWND, UINT, DWORD, LONG);
  41. LONG APIENTRY ClientWndProc   (HWND, UINT, DWORD, LONG);
  42. BOOL CALLBACK About          (HWND, UINT, DWORD, LONG);
  43. BOOL CALLBACK FileType       (HWND, UINT, DWORD, LONG);
  44. BOOL CALLBACK MapFileName     (HWND, UINT, DWORD, LONG);
  45. LONG APIENTRY TextWndProc     (HWND, UINT, DWORD, LONG);
  46. /***************************************************************************
  47. * main
  48. *
  49. ***************************************************************************/
  50. int WINAPI WinMain(
  51.     HANDLE hInstance,
  52.     HANDLE hPrevInstance,
  53.     LPSTR lpCmdLine,
  54.     int nShowCmd)
  55. {
  56.     MSG msg;
  57.     // this will change to something more reasonable
  58.     ghModule = GetModuleHandle(NULL);
  59.     if (!InitializeApp())
  60.     {
  61.         MessageBox(ghwndMain,
  62.                    GetStringRes(IDS_ERR_INITAPP_FAILED),
  63.                    NULL, MB_OK);
  64.         return 0;
  65.     }
  66.     while (GetMessage(&msg, NULL, 0, 0))
  67.     {
  68.         TranslateMessage(&msg);
  69.         DispatchMessage(&msg);
  70.     }
  71.     if (hMenuWindow && IsWindow(hMenuWindow))
  72.         DestroyMenu(hMenuWindow);
  73.     if (hClientMenuWindow && IsWindow(hClientMenuWindow))
  74.         DestroyMenu(hClientMenuWindow);
  75.     if (hServerMenuWindow && IsWindow(hServerMenuWindow))
  76.         DestroyMenu(hServerMenuWindow);
  77.     if (hMenu && IsWindow(hMenu))
  78.         DestroyMenu(hMenu);
  79.     if (hClientMenu && IsWindow(hClientMenu))
  80.         DestroyMenu(hClientMenu);
  81.     if (hServerMenu && IsWindow(hServerMenu))
  82.         DestroyMenu(hServerMenu);
  83.     return 1;
  84.     UNREFERENCED_PARAMETER(lpCmdLine);
  85.     UNREFERENCED_PARAMETER(nShowCmd);
  86.     UNREFERENCED_PARAMETER(hInstance);
  87.     UNREFERENCED_PARAMETER(hPrevInstance);
  88. }
  89. /***************************************************************************
  90. * InitializeApp
  91. *
  92. ***************************************************************************/
  93. BOOL InitializeApp(void)
  94. {
  95.     WNDCLASS wc;
  96.     wc.style            = CS_OWNDC;
  97.     wc.lpfnWndProc      = (WNDPROC)MainWndProc;
  98.     wc.cbClsExtra       = 0;
  99.     wc.cbWndExtra       = sizeof(LONG);
  100.     wc.hInstance        = ghModule;
  101.     wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  102.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  103.     wc.hbrBackground    = (HBRUSH)(COLOR_APPWORKSPACE);
  104.     wc.lpszMenuName     = "MainMenu";
  105.     wc.lpszClassName    = "MemoryClass";
  106.     if (!RegisterClass(&wc))
  107.         return FALSE;
  108.     wc.style            = CS_OWNDC;
  109.     wc.lpfnWndProc      = (WNDPROC)ServerWndProc;
  110.     wc.cbClsExtra       = 0;
  111.     wc.cbWndExtra       = 0;                      // LATER sizeof(LONG);
  112.     wc.hInstance        = ghModule;
  113.     wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  114.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  115.     wc.hbrBackground    = (HBRUSH)(COLOR_APPWORKSPACE);
  116.     wc.lpszMenuName     = NULL;
  117.     wc.lpszClassName    = "ServerClass";
  118.     if (!RegisterClass(&wc))
  119.         return FALSE;
  120.     wc.style            = CS_OWNDC;
  121.     wc.lpfnWndProc      = (WNDPROC)ClientWndProc;
  122.     wc.cbClsExtra       = 0;
  123.     wc.cbWndExtra       = 0;                      // LATER sizeof(LONG);
  124.     wc.hInstance        = ghModule;
  125.     wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  126.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  127.     wc.hbrBackground    = (HBRUSH)(COLOR_APPWORKSPACE);
  128.     wc.lpszMenuName     = NULL;
  129.     wc.lpszClassName    = "ClientClass";
  130.     if (!RegisterClass(&wc))
  131.         return FALSE;
  132.     wc.style            = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  133.     wc.lpfnWndProc      = (WNDPROC)TextWndProc;
  134.     wc.cbClsExtra       = 0;
  135.     wc.cbWndExtra       = 0;
  136.     wc.hInstance        = ghModule;
  137.     wc.hIcon            = NULL;
  138.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  139.     wc.hbrBackground    = (HBRUSH)(COLOR_BTNSHADOW);
  140.     wc.lpszMenuName     = NULL;
  141.     wc.lpszClassName    = "Text";
  142.     if (!RegisterClass(&wc))
  143.             return FALSE;
  144.     hMenu       = LoadMenu(ghModule, "MainMenu");
  145.     hServerMenu = LoadMenu(ghModule, "ServerMenu");
  146.     hClientMenu = LoadMenu(ghModule, "ClientMenu");
  147.     hMenuWindow       = GetSubMenu(hMenu, 1);
  148.     hServerMenuWindow = GetSubMenu(hServerMenu, 2);
  149.     hClientMenuWindow = GetSubMenu(hClientMenu, 2);
  150.     ghwndMain = CreateWindowEx(0L, "MemoryClass", "Memory",
  151.             WS_OVERLAPPED   | WS_CAPTION     | WS_BORDER       |
  152.             WS_THICKFRAME   | WS_MAXIMIZEBOX | WS_MINIMIZEBOX  |
  153.             WS_CLIPCHILDREN | WS_VISIBLE     | WS_SYSMENU,
  154.             80, 70, 592, 300,
  155.             NULL, hMenu, ghModule, NULL);
  156.     if (ghwndMain == NULL)
  157.         return FALSE;
  158.     SetWindowLong(ghwndMain, GWL_USERDATA, 0L);
  159.     return TRUE;
  160. }
  161. /***************************************************************************
  162. * MainWndProc
  163. *
  164. ***************************************************************************/
  165. long APIENTRY MainWndProc(
  166.                           HWND hwnd,
  167.                           UINT message,
  168.                           DWORD wParam,
  169.                           LONG lParam)
  170. {
  171.     static int         iSvrCount=1;
  172.     static int         iCltCount=1;
  173.     CLIENTCREATESTRUCT clientcreate;
  174.     HWND               hwndChildWindow;
  175.     BOOL               fSuccess;
  176.     switch (message)
  177.     {
  178.       case WM_CREATE:
  179.         SetWindowLong(hwnd, 0, (LONG)NULL);
  180.         clientcreate.hWindowMenu  = hMenuWindow;
  181.         clientcreate.idFirstChild = 1;
  182.         ghwndClient = CreateWindow("MDICLIENT", NULL,
  183.                                     WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
  184.                                     0,0,0,0,
  185.                                     hwnd, NULL, ghModule, (LPVOID)&clientcreate);
  186.         return 0L;
  187.       case WM_DESTROY: {
  188.         PostQuitMessage(0);
  189.         return 0L;
  190.       }
  191.       case WM_COMMAND:
  192.         switch (LOWORD(wParam))
  193.         {
  194.             case IDM_TILE:
  195.                 SendMessage(ghwndClient, WM_MDITILE, 0L, 0L);
  196.                 return 0L;
  197.             case IDM_CASCADE:
  198.                 SendMessage(ghwndClient, WM_MDICASCADE, 0L, 0L);
  199.                 return 0L;
  200.             case IDM_ARRANGE:
  201.                 SendMessage(ghwndClient, WM_MDIICONARRANGE, 0L, 0L);
  202.                 return 0L;
  203.             case MM_SERVER: {
  204.                 BOOL fSucess = FALSE;
  205.                 HANDLE hNode, hHead;
  206.                 PNODE  pNode;
  207.                 MDICREATESTRUCT mdicreate;
  208.                 hNode = LocalAlloc(LHND, (WORD) sizeof(NODE));
  209.                 if (hNode && (pNode = (PNODE)LocalLock(hNode)))
  210.                 {
  211.                     wsprintf((LPSTR) &(pNode->ChildWnd.CaptionBarText),
  212.                              GetStringRes(IDS_SERVER), iSvrCount);
  213.                     mdicreate.szClass = "ServerClass";
  214.                     mdicreate.szTitle = (LPTSTR)&(pNode->ChildWnd.CaptionBarText);
  215.                     mdicreate.hOwner  = ghModule;
  216.                     mdicreate.x       =
  217.                     mdicreate.y       =
  218.                     mdicreate.cx      =
  219.                     mdicreate.cy      = CW_USEDEFAULT;
  220.                     mdicreate.style   = 0l;
  221.                     mdicreate.lParam  = 0L;
  222.                     /*Create Child Window*/
  223.                     hwndChildWindow =
  224.                        (HWND) SendMessage(ghwndClient, WM_MDICREATE,
  225.                                           0L,
  226.                                           (LONG)(LPMDICREATESTRUCT)&mdicreate);
  227.                     if (hwndChildWindow)
  228.                     {
  229.                        pNode->ChildWnd.hParent      = ghwndClient;
  230.                        pNode->ChildWnd.hThisWnd     = hwndChildWindow;
  231.                        hHead = (HANDLE)GetWindowLong(hwnd, 0);
  232.                        pNode->hNext = hHead;
  233.                        SetWindowLong(hwnd, 0, (LONG) hNode);
  234.                        iSvrCount++;
  235.                        LocalUnlock(hNode);
  236.                        fSuccess = TRUE;
  237.                     }
  238.                     else
  239.                     {
  240.                        LocalUnlock (hNode);
  241.                        LocalFree (hNode);
  242.                        fSuccess = FALSE;
  243.                     }
  244.                 }
  245.                 if (!fSuccess)
  246.                     MessageBox(ghwndMain,
  247.                                GetStringRes(IDS_ERR_CREATE_CHILD_FAILED),
  248.                                NULL, MB_OK);
  249.                  return 0L;
  250.             }
  251.             case MM_CLIENT:
  252.             {
  253.                 BOOL fResult = FALSE;
  254.                 HANDLE hNode, hHead;
  255.                 PNODE  pNode;
  256.                 MDICREATESTRUCT mdicreate;
  257.                 hNode = LocalAlloc(LHND, (WORD) sizeof(NODE));
  258.                 if (hNode && (pNode = (PNODE)LocalLock(hNode)))
  259.                 {
  260.                     wsprintf((LPSTR) &(pNode->ChildWnd.CaptionBarText),
  261.                              GetStringRes(IDS_CLIENT), iCltCount);
  262.                     mdicreate.szClass = "ClientClass";
  263.                     mdicreate.szTitle = (LPSTR) &(pNode->ChildWnd.CaptionBarText);
  264.                     mdicreate.hOwner  = ghModule;
  265.                     mdicreate.x       =
  266.                     mdicreate.y       =
  267.                     mdicreate.cx      =
  268.                     mdicreate.cy      = CW_USEDEFAULT;
  269.                     mdicreate.style   = 0l;
  270.                     mdicreate.lParam  = 0L;
  271.                     /*Create Child Window*/
  272.                     hwndChildWindow =
  273.                         (HANDLE) SendMessage(ghwndClient, WM_MDICREATE,
  274.                                              0L,
  275.                                              (LONG)(LPMDICREATESTRUCT)&mdicreate);
  276.                     if (hwndChildWindow)
  277.                     {
  278.                        pNode->ChildWnd.hParent      = ghwndClient;
  279.                        pNode->ChildWnd.hThisWnd     = hwndChildWindow;
  280.                        hHead = (HANDLE)GetWindowLong(hwnd, 0);
  281.                        pNode->hNext = hHead;
  282.                        SetWindowLong(hwnd, 0, (LONG) hNode);
  283.                        iCltCount++;
  284.                        LocalUnlock(hNode);
  285.                        fSuccess = TRUE;
  286.                     }
  287.                     else
  288.                     {
  289.                        LocalUnlock (hNode);
  290.                        LocalFree (hNode);
  291.                        fSuccess = FALSE;
  292.                     }
  293.                 }
  294.                 if (!fSuccess)
  295.                    MessageBox(ghwndMain,
  296.                               GetStringRes(IDS_ERR_CREATE_CHILD_FAILED),
  297.                               NULL, MB_OK);
  298.                 return 0L;
  299.             }
  300.             case MM_ABOUT:
  301.                 if (DialogBox(ghModule, "AboutBox", ghwndMain, (DLGPROC)About) == -1)
  302.                    MessageBox(ghwndMain,
  303.                               GetStringRes(IDS_ERR_CREATE_DLG_FAILED), NULL, MB_OK);
  304.                 return 0L;
  305.             case MM_OPT_1:
  306.             case MM_OPT_2:
  307.             case MM_OPT_3:
  308.             case MM_OPT_4:
  309.             case MM_OPT_5:
  310.             case MM_OPT_6:
  311.             case MM_OPT_7:
  312.             case MM_OPT_8:
  313.             {
  314.                 HWND hActiveChild;
  315.                 hActiveChild = (HANDLE) SendMessage(ghwndClient, WM_MDIGETACTIVE, 0L, 0L);
  316.                 if (hActiveChild)
  317.                     SendMessage(hActiveChild, WM_COMMAND, wParam, lParam);
  318.                 return 0L;
  319.             }
  320.             default:
  321.                 return DefFrameProc(hwnd,  ghwndClient, message, wParam, lParam);
  322.         }
  323.     default:
  324.         return DefFrameProc(hwnd,  ghwndClient, message, wParam, lParam);
  325.     }
  326. }
  327. /***************************************************************************
  328. * ServerWndProc
  329. *
  330. ***************************************************************************/
  331. long APIENTRY ServerWndProc( HWND hwnd,
  332.                              UINT message,
  333.                              DWORD wParam,
  334.                              LONG lParam)
  335. {
  336.    static HANDLE MapFileHandle = NULL;
  337.    static HANDLE hMem1         = NULL;
  338.    static LPVOID pShrMem1      = NULL;
  339.    static HANDLE hEdit;
  340.    static BOOL   bDirty        = FALSE;
  341.    static HWND   hTextWnd;
  342.    BOOL          fSuccess;
  343.    switch (message)
  344.    {
  345.       case WM_COMMAND:
  346.       {
  347.          switch (LOWORD(wParam))
  348.          {
  349.             case MM_OPT_1:
  350.             { //Create File
  351.                SetWindowText(hTextWnd, GetStringRes(IDS_CREATE_MAP_FILE));
  352.                switch (DialogBox(ghModule, "FileType", hwnd, (DLGPROC)FileType))
  353.                {
  354.                   case -1:
  355.                      fSuccess = FALSE;
  356.                      break;
  357.                   case IDBTN_MAP:
  358.                      if (MapFileHandle = CreateMapFile(gszFile))
  359.                         fSuccess = TRUE;
  360.                      else
  361.                         fSuccess = FALSE;
  362.                      break;
  363.                   default:
  364.                      MapFileHandle = (HANDLE) 0xFFFFFFFF;
  365.                      fSuccess = TRUE;
  366.                      break;
  367.                }
  368.                if (fSuccess)
  369.                {
  370.                   EnableMenuItem(hServerMenu, MM_OPT_1, MF_GRAYED);
  371.                   EnableMenuItem(hServerMenu, MM_OPT_2, MF_ENABLED);
  372.                   SetWindowText(hTextWnd,
  373.                                 GetStringRes(IDS_SEL_CREATE_FILE_MAPPING));
  374.                }
  375.                else
  376.                {
  377.                   SetWindowText(hTextWnd,
  378.                                 GetStringRes (IDS_ERR_MAPFILE_FAILED));
  379.                }
  380.                return 0L;
  381.             }
  382.             case MM_OPT_2:
  383.             { //Create File Mapping
  384.                // This option should be disabled until MM_OPT_1 has been chosen.
  385.                SetWindowText(hTextWnd,
  386.                              GetStringRes(IDS_CREATE_MAPPING));
  387.                if (MapFileHandle)
  388.                   switch (DialogBox(ghModule, "MapName", hwnd, (DLGPROC)MapFileName))
  389.                   {
  390.                      case IDBTN_OK:
  391.                         if (hMem1 = CreateMap(MapFileHandle, gszMapName))
  392.                            fSuccess = TRUE;
  393.                         else
  394.                            fSuccess = FALSE;
  395.                         break;
  396.                      default:
  397.                         fSuccess = FALSE;
  398.                         break;
  399.                   }
  400.                else
  401.                   fSuccess = FALSE;
  402.                if (fSuccess)
  403.                {
  404.                   EnableMenuItem(hServerMenu, MM_OPT_2, MF_GRAYED);
  405.                   EnableMenuItem(hServerMenu, MM_OPT_3, MF_ENABLED);
  406.                   SetWindowText(hTextWnd,
  407.                                 GetStringRes (IDS_SEL_MAP_VIEW));
  408.                }
  409.                else
  410.                {
  411.                   SetWindowText(hTextWnd,
  412.                                 GetStringRes (IDS_ERR_MAPPING_FAILED));
  413.                }
  414.                return 0L;
  415.             }
  416.             case MM_OPT_3:
  417.             { //Map View of File
  418.                SetWindowText(hTextWnd, GetStringRes (IDS_MAPPING_VIEW));
  419.                if (hMem1 && (pShrMem1 = (LPVOID)MapView(hMem1)))
  420.                {
  421.                   EnableMenuItem(hServerMenu, MM_OPT_3, MF_GRAYED);
  422.                   EnableMenuItem(hServerMenu, MM_OPT_4, MF_ENABLED);
  423.                   SetWindowText(hTextWnd, GetStringRes (IDS_SEL_ACCESS));
  424.                }
  425.                else
  426.                {
  427.                   SetWindowText(hTextWnd, GetStringRes(IDS_ERR_MAPVIEW_FAILED));
  428.                }
  429.                return 0L;
  430.             }
  431.             case MM_OPT_4:
  432.             { //Access
  433.                RECT    rcl;
  434.                SetWindowText(hTextWnd,
  435.                              GetStringRes (IDS_ACCESSING_SERVER_WRITE));
  436.                if (pShrMem1)
  437.                {
  438.                   GetClientRect(hwnd, &rcl);
  439.                   hEdit = CreateWindow("edit", (LPSTR) NULL,
  440.                                         WS_CHILD      | WS_VISIBLE     |
  441.                                         WS_HSCROLL    | WS_VSCROLL     |
  442.                                         WS_BORDER     | ES_LEFT |
  443.                                         ES_MULTILINE  | ES_AUTOHSCROLL |
  444.                                         ES_AUTOVSCROLL,
  445.                                         0,0, rcl.right - rcl.left,
  446.                                         rcl.bottom - rcl.top -
  447.                                         GetWindowLong(hTextWnd, GWL_USERDATA),
  448.                                         hwnd, (HMENU)1, ghModule, (LPVOID)NULL);
  449.                   if (hEdit)
  450.                   {
  451.                      EnableMenuItem(hServerMenu, MM_OPT_4, MF_GRAYED);
  452.                      SetFocus(hEdit);
  453.                      fSuccess = TRUE;
  454.                   }
  455.                   else
  456.                      fSuccess = FALSE;
  457.                }
  458.                else
  459.                   fSuccess = FALSE;
  460.                if (!fSuccess)
  461.                {
  462.                   MessageBox(ghwndMain,
  463.                              GetStringRes(IDS_ERR_ACCESS_WRITE_FAILED),
  464.                              NULL, MB_OK);
  465.                   SetWindowText(hTextWnd,
  466.                                 GetStringRes (IDS_ERR_ACCESS_WRITE_FAILED));
  467.                }
  468.                return 0L;
  469.             }
  470.          } // End of switch (LOWORD(wParam))
  471.          switch (HIWORD(wParam))
  472.          {
  473.             case EN_UPDATE:
  474.             {
  475.                if (hEdit && (hEdit == (HWND)lParam))
  476.                {
  477.                   bDirty = TRUE;
  478.                }
  479.                return 0L;
  480.             }
  481.          }
  482.       }
  483.       case WM_TIMER:
  484.          if (bDirty && IsWindow(hEdit))
  485.          {
  486.             int     iCnt;
  487.             iCnt = SendMessage(hEdit, WM_GETTEXT, (WPARAM)4000, (LPARAM)pShrMem1);
  488.             if (iCnt)
  489.             {
  490.                bDirty = FALSE;
  491.             }
  492.          }
  493.          return 0L;
  494.       case WM_MDIACTIVATE:
  495.          if ((HWND) lParam == hwnd)
  496.          {
  497.             SendMessage(GetParent(hwnd), WM_MDISETMENU,
  498.                         (DWORD)  hServerMenu,
  499.                         (LONG)   hServerMenuWindow) ;
  500.             DrawMenuBar(GetParent(GetParent(hwnd))) ;
  501.          }
  502.          return 0;
  503.       case WM_SIZE:
  504.          if (hEdit)
  505.             MoveWindow(hEdit, 0, 0,
  506.                        LOWORD(lParam),
  507.                        HIWORD(lParam)-GetWindowLong(hTextWnd, GWL_USERDATA),
  508.                        TRUE);
  509.          MoveWindow(hTextWnd, 0,
  510.                     HIWORD(lParam) - GetWindowLong(hTextWnd, GWL_USERDATA),
  511.                     LOWORD(lParam),
  512.                     HIWORD(lParam), TRUE);
  513.          return DefMDIChildProc(hwnd, message, wParam, lParam);
  514.       case WM_CREATE:
  515.       {
  516.          PPERWNDINFO      pWndInfo;
  517.          PNODE            pHead;
  518.          HANDLE           hHead, hTmp;
  519.          RECT             rect;
  520.          GetClientRect(hwnd, &rect);
  521.          hTextWnd = CreateWindow("Text", NULL,
  522.                                  WS_BORDER | SS_LEFT | WS_CHILD | WS_VISIBLE,
  523.                                  0, 0, 0, 0,
  524.                                  hwnd,
  525.                                  (HMENU) 2,
  526.                                  ghModule,
  527.                                  NULL);
  528.          EnableMenuItem(hServerMenu, MM_OPT_1, MF_ENABLED);
  529.          SetWindowText(hTextWnd, GetStringRes (IDS_SEL_CREATE_FILE));
  530.          // now find match
  531.          hHead = (HANDLE) GetWindowLong(ghwndMain, 0);
  532.          if (hHead && (pHead = (PNODE)LocalLock(hHead)))
  533.          {
  534.             while ((pHead->ChildWnd.hThisWnd != hwnd) && (pHead->hNext))
  535.             {
  536.                hTmp = hHead;
  537.                hHead = pHead->hNext;
  538.                LocalUnlock(hTmp);
  539.                pHead = (PNODE)LocalLock(hHead);
  540.             }
  541.             if (pHead->ChildWnd.hThisWnd == hwnd)
  542.             {
  543.                pWndInfo = &pHead->ChildWnd;
  544.                GetClientRect(pWndInfo->hThisWnd, &pWndInfo->rcClient);
  545.             }
  546.             LocalUnlock(hHead);
  547.          }
  548.          return DefMDIChildProc(hwnd, message, wParam, lParam);
  549.       }
  550.       case WM_CLOSE:
  551.       {
  552.          PPERWNDINFO      pWndInfo;
  553.          PNODE            pHead, pTrail;
  554.          HANDLE           hHead, hTmp;
  555.          EnableMenuItem(hServerMenu, MM_OPT_2, MF_GRAYED);
  556.          EnableMenuItem(hServerMenu, MM_OPT_3, MF_GRAYED);
  557.          EnableMenuItem(hServerMenu, MM_OPT_4, MF_GRAYED);
  558.          SendMessage(GetParent(hwnd), WM_MDISETMENU, (DWORD)hMenu, (LONG)hMenuWindow);
  559.          DrawMenuBar(GetParent(GetParent(hwnd))) ;
  560.          // Unmap view, close mapping and file!
  561.          if (pShrMem1)
  562.          {
  563.             UnmapViewOfFile (pShrMem1);
  564.             pShrMem1 = NULL;
  565.          }
  566.          if (hMem1)
  567.          {
  568.             CloseHandle (hMem1);
  569.             hMem1 = NULL;
  570.          }
  571.          if (MapFileHandle && MapFileHandle != INVALID_HANDLE_VALUE)
  572.          {
  573.             CloseHandle (MapFileHandle);
  574.             MapFileHandle = NULL;
  575.          }
  576.          // now find match
  577.          hHead = (HANDLE) GetWindowLong(ghwndMain, 0);
  578.          if ((hHead) && (pHead = (PNODE)LocalLock(hHead)))
  579.          {
  580.             pTrail = pHead;
  581.             while ((pHead->ChildWnd.hThisWnd != hwnd) && (pHead->hNext))
  582.             {
  583.                hTmp = hHead;
  584.                pTrail = pHead;
  585.                hHead = pHead->hNext;
  586.                LocalUnlock(hTmp);
  587.                pHead = (PNODE) LocalLock(hHead);
  588.             }
  589.             if (pHead->ChildWnd.hThisWnd == hwnd)
  590.             {
  591.                pWndInfo = &pHead->ChildWnd;
  592.                if (pTrail == pHead)
  593.                   SetWindowLong(ghwndMain, 0, (LONG) pHead->hNext);
  594.                else
  595.                   pTrail->hNext = pHead->hNext;
  596.                LocalUnlock(hHead);
  597.                LocalFree(hHead);
  598.             }
  599.             else
  600.             {
  601.                LocalUnlock(hHead);
  602.             }
  603.          }
  604.          return DefMDIChildProc(hwnd, message, wParam, lParam);
  605.       }
  606.       case WM_DESTROY:
  607.          KillTimer(hwnd, 1);
  608.          return 0L;
  609.       case WM_PAINT:
  610.          return DefMDIChildProc(hwnd, message, wParam, lParam);
  611.       default:
  612.          return DefMDIChildProc(hwnd, message, wParam, lParam);
  613.    }
  614. }
  615. /***************************************************************************
  616. * ClientWndProc
  617. *
  618. ***************************************************************************/
  619. long APIENTRY ClientWndProc( HWND hwnd,
  620.                              UINT message,
  621.                              DWORD wParam,
  622.                              LONG lParam)
  623. {
  624.     static HANDLE hMem1    = NULL;
  625.     static LPVOID pShrMem1 = NULL;
  626.     static HANDLE hEdit;
  627.     static HANDLE hTextWnd;
  628.     BOOL   fSuccess = FALSE;
  629.     switch (message)
  630.     {
  631.         case WM_COMMAND:
  632.         {
  633.           switch (LOWORD(wParam))
  634.           {
  635.              case MM_OPT_5:
  636.              { //Open File Mapping
  637.                  SetWindowText(hTextWnd, GetStringRes (IDS_OPENING_FILE));
  638.                  switch (DialogBox(ghModule, "MapName", hwnd, (DLGPROC)MapFileName))
  639.                  {
  640.                      case IDBTN_OK:
  641.                          if (hMem1 = OpenMap(gszMapName))
  642.                              fSuccess = TRUE;
  643.                          else
  644.                             fSuccess = FALSE;
  645.                          break;
  646.                      default:
  647.                          fSuccess = FALSE;
  648.                          break;
  649.                  }
  650.                  if (fSuccess)
  651.                  {
  652.                     EnableMenuItem(hClientMenu, MM_OPT_5, MF_GRAYED);
  653.                     EnableMenuItem(hClientMenu, MM_OPT_6, MF_ENABLED);
  654.                     SetWindowText(hTextWnd, GetStringRes (IDS_SEL_MAP_VIEW));
  655.                  }
  656.                  else
  657.                  {
  658.                     SetWindowText(hTextWnd,
  659.                                   GetStringRes (IDS_ERR_OPEN_MAPPING_FAILED));
  660.                  }
  661.                  return 0L;
  662.             }
  663.             case MM_OPT_6:
  664.             { //Map View of File
  665.                 SetWindowText(hTextWnd, GetStringRes (IDS_MAPPING_VIEW));
  666.                 if (hMem1 && (pShrMem1 = (LPVOID) MapView(hMem1)))
  667.                 {
  668.                    EnableMenuItem(hClientMenu, MM_OPT_6, MF_GRAYED);
  669.                    EnableMenuItem(hClientMenu, MM_OPT_7, MF_ENABLED);
  670.                    SetWindowText(hTextWnd, GetStringRes (IDS_SEL_ACCESS_READ));
  671.                 }
  672.                 else
  673.                 {
  674.                    SetWindowText(hTextWnd,
  675.                                  GetStringRes(IDS_ERR_MAPVIEW_FAILED));
  676.                 }
  677.                 return 0L;
  678.             }
  679.             case MM_OPT_7:
  680.             { //Access
  681.                 RECT    rcl;
  682.                  SetWindowText(hTextWnd,
  683.                                GetStringRes(IDS_ACCESSING_SERVER_READ));
  684.                  fSuccess = FALSE;  // assume failure.
  685.                  if (pShrMem1)
  686.                  {
  687.                     GetClientRect(hwnd, &rcl);
  688.                     hEdit = CreateWindow("edit", NULL,
  689.                                    WS_CHILD     | WS_VISIBLE     |
  690.                                    WS_HSCROLL   | WS_VSCROLL     |
  691.                                    WS_BORDER    | ES_LEFT        |
  692.                                    ES_MULTILINE | ES_AUTOHSCROLL |
  693.                                    ES_READONLY  | ES_AUTOVSCROLL,
  694.                                    0,0, rcl.right-rcl.left,
  695.                                    rcl.bottom-rcl.top-GetWindowLong(hTextWnd, GWL_USERDATA),
  696.                                    hwnd, (HMENU) 1, ghModule, NULL);
  697.                     if (hEdit)
  698.                     {
  699.                        SetFocus(hEdit);
  700.                        fSuccess = TRUE;
  701.                     }
  702.                  }
  703.                  if (fSuccess)
  704.                  {
  705.                     SendMessage(hEdit, WM_SETTEXT, 0L, (LONG)pShrMem1);
  706.                     SetTimer(hwnd, 2, 1000, NULL);
  707.                     EnableMenuItem(hClientMenu, MM_OPT_7, MF_GRAYED);
  708.                     EnableMenuItem(hClientMenu, MM_OPT_8, MF_ENABLED);
  709.                  }
  710.                  else
  711.                  {
  712.                     MessageBox(ghwndMain,
  713.                                GetStringRes (IDS_ERR_ACCESS_READ_FAILED),
  714.                                NULL, MB_OK);
  715.                     SetWindowText(hTextWnd,
  716.                                   GetStringRes (IDS_ERR_ACCESS_READ_FAILED));
  717.                  }
  718.              return 0L;
  719.              }
  720.             case MM_OPT_8:
  721.             { // refresh now
  722.                HANDLE hActive;
  723.                hActive = (HANDLE) SendMessage(GetParent(hwnd), WM_MDIGETACTIVE, 0L, 0L);
  724.                SendMessage(hEdit, WM_SETTEXT, 0L, (LONG)pShrMem1);
  725.                return 0L;
  726.             }
  727.         }
  728.     }
  729.     case WM_TIMER:
  730.     {
  731.         HANDLE hActive;
  732.         if (IsWindow (hEdit) && pShrMem1)
  733.         {
  734.            hActive = (HANDLE) SendMessage(GetParent(hwnd), WM_MDIGETACTIVE, 0L, 0L);
  735.            SendMessage(hEdit, WM_SETTEXT, 0L, (LONG)pShrMem1);
  736.         }
  737.         return 0L;
  738.     }
  739.     case WM_MDIACTIVATE:
  740.         if ((HWND) lParam == hwnd)
  741.         {
  742.            SendMessage(GetParent(hwnd), WM_MDISETMENU,
  743.                        (DWORD)  hClientMenu,
  744.                        (LONG)   hClientMenuWindow);
  745.            DrawMenuBar(GetParent(GetParent(hwnd))) ;
  746.         }
  747.         return 0;
  748.     case WM_SIZE:
  749.         if (hEdit)
  750.            MoveWindow(hEdit, 0, 0,
  751.                       LOWORD(lParam),
  752.                       HIWORD(lParam)-GetWindowLong(hTextWnd, GWL_USERDATA),
  753.                       TRUE);
  754.         MoveWindow(hTextWnd, 0,
  755.                    HIWORD(lParam) - GetWindowLong(hTextWnd, GWL_USERDATA),
  756.                    LOWORD(lParam),
  757.                    HIWORD(lParam), TRUE);
  758.         return DefMDIChildProc(hwnd, message, wParam, lParam);
  759.     case WM_CREATE:
  760.     {
  761.         PPERWNDINFO      pWndInfo;
  762.         PNODE            pHead;
  763.         HANDLE           hHead, hTmp;
  764.         RECT             rect;
  765.         GetClientRect(hwnd, &rect);
  766.         hTextWnd = CreateWindow("Text", NULL,
  767.                                 WS_BORDER | SS_LEFT | WS_CHILD | WS_VISIBLE,
  768.                                 0, 0, 0, 0,
  769.                                 hwnd,
  770.                                 (HMENU) 2,
  771.                                 ghModule,
  772.                                 NULL);
  773.         EnableMenuItem(hClientMenu, MM_OPT_5, MF_ENABLED);
  774.         SetWindowText(hTextWnd, GetStringRes (IDS_SEL_OPEN_FILE));
  775.         // now find match
  776.         hHead = (HANDLE) GetWindowLong(ghwndMain, 0);
  777.         if (hHead && (pHead = (PNODE)LocalLock(hHead)))
  778.         {
  779.            while ((pHead->ChildWnd.hThisWnd != hwnd) && (pHead->hNext))
  780.            {
  781.               hTmp = hHead;
  782.               hHead = pHead->hNext;
  783.               LocalUnlock(hTmp);
  784.               pHead = (PNODE) LocalLock(hHead);
  785.            }
  786.            if (pHead->ChildWnd.hThisWnd == hwnd)
  787.            {
  788.               pWndInfo = &pHead->ChildWnd;
  789.               GetClientRect(pWndInfo->hThisWnd, &pWndInfo->rcClient);
  790.            }
  791.            LocalUnlock(hHead);
  792.            return DefMDIChildProc(hwnd, message, wParam, lParam);
  793.         }
  794.         return DefMDIChildProc(hwnd, message, wParam, lParam);
  795.     }
  796.     case WM_CLOSE:
  797.     {
  798.         PPERWNDINFO      pWndInfo;
  799.         PNODE            pHead, pTrail;
  800.         HANDLE           hHead, hTmp;
  801.         EnableMenuItem(hClientMenu, MM_OPT_6, MF_GRAYED);
  802.         EnableMenuItem(hClientMenu, MM_OPT_7, MF_GRAYED);
  803.         EnableMenuItem(hClientMenu, MM_OPT_8, MF_GRAYED);
  804.         SendMessage(GetParent(hwnd), WM_MDISETMENU,
  805.                     (DWORD) hMenu,
  806.                     (LONG)   hMenuWindow) ;
  807.         DrawMenuBar(GetParent(GetParent(hwnd))) ;
  808.         // Don't need to read from the edit control anymore
  809.         KillTimer(hwnd, 2);
  810.         // Unmap view, close mapping!
  811.         if (pShrMem1)
  812.         {
  813.            UnmapViewOfFile (pShrMem1);
  814.            pShrMem1 = NULL;
  815.         }
  816.         if (hMem1)
  817.         {
  818.            CloseHandle (hMem1);
  819.            hMem1 = NULL;
  820.         }
  821.         // now find match
  822.         hHead = (HANDLE) GetWindowLong(ghwndMain, 0);
  823.         if (hHead && (pHead = (PNODE)LocalLock(hHead)))
  824.         {
  825.             pTrail = pHead;
  826.             while ((pHead->ChildWnd.hThisWnd != hwnd) && (pHead->hNext))
  827.             {
  828.                hTmp = hHead;
  829.                pTrail = pHead;
  830.                hHead = pHead->hNext;
  831.                LocalUnlock(hTmp);
  832.                pHead = (PNODE) LocalLock(hHead);
  833.             }
  834.             if (pHead->ChildWnd.hThisWnd == hwnd)
  835.             {
  836.                 pWndInfo = &pHead->ChildWnd;
  837.                 if (pTrail == pHead)
  838.                     SetWindowLong(ghwndMain, 0, (LONG) pHead->hNext);
  839.                 else
  840.                     pTrail->hNext = pHead->hNext;
  841.                 LocalUnlock(hHead);
  842.                 LocalFree(hHead);
  843.             }
  844.             else
  845.             {
  846.                 LocalUnlock(hHead);
  847.             }
  848.         }
  849.         return DefMDIChildProc(hwnd, message, wParam, lParam);
  850.     }
  851.     case WM_DESTROY:
  852.         return 0l;
  853.     default:
  854.          return DefMDIChildProc(hwnd, message, wParam, lParam);
  855.     }
  856. }
  857. /***************************************************************************
  858. * About
  859. *
  860. * About dialog proc.
  861. *
  862. ***************************************************************************/
  863. BOOL CALLBACK APIENTRY About( HWND hDlg,
  864.                               UINT message,
  865.                               DWORD wParam,
  866.                               LONG lParam)
  867. {
  868.     switch (message)
  869.     {
  870.         case WM_INITDIALOG:
  871.             return TRUE;
  872.         case WM_COMMAND:
  873.             if (wParam == IDOK)
  874.                 EndDialog(hDlg, wParam);
  875.             break;
  876.     }
  877.     return FALSE;
  878.     UNREFERENCED_PARAMETER(lParam);
  879.     UNREFERENCED_PARAMETER(hDlg);
  880. }
  881. /***************************************************************************
  882. * MapFileName
  883. *
  884. * MapFileName dialog proc.
  885. *
  886. ***************************************************************************/
  887. BOOL CALLBACK MapFileName( HWND hDlg,
  888.                            UINT message,
  889.                            DWORD wParam,
  890.                            LONG lParam)
  891. {
  892.     switch (message)
  893.     {
  894.     case WM_INITDIALOG:
  895.         return TRUE;
  896.     case WM_COMMAND:
  897.         switch (wParam)
  898.         {
  899.             case IDBTN_OK:
  900.             {
  901.                 if (!GetDlgItemText(hDlg, IDEDIT_MAPNAME, gszMapName, 20))
  902.                 {
  903.                    strncpy(gszMapName, "MapName1", 10);        // default name
  904.                 }
  905.                 EndDialog(hDlg, IDBTN_OK);
  906.                 break;
  907.             }
  908.         }
  909.     }
  910.     return FALSE;
  911.     UNREFERENCED_PARAMETER(lParam);
  912.     UNREFERENCED_PARAMETER(hDlg);
  913. }
  914. /***************************************************************************
  915. * FileType
  916. *
  917. * FileType dialog proc.
  918. *
  919. ***************************************************************************/
  920. BOOL CALLBACK FileType( HWND hDlg,
  921.                         UINT message,
  922.                         DWORD wParam,
  923.                         LONG lParam)
  924. {
  925.     switch (message)
  926.     {
  927.        case WM_INITDIALOG:
  928.            return TRUE;
  929.        case WM_COMMAND:
  930.            switch (wParam)
  931.            {
  932.               case IDBTN_PAGE:
  933.               {
  934.                  EndDialog(hDlg, IDBTN_PAGE);
  935.                  break;
  936.               }
  937.               case IDBTN_MAP:
  938.               {
  939.                   if ((GetDlgItemText(hDlg, IDEDIT_MAPFILE, gszFile, 20)) == 0)
  940.                       EndDialog(hDlg, IDBTN_PAGE);  // default to use PAGE file
  941.                   else
  942.                       EndDialog(hDlg, IDBTN_MAP);
  943.                   break;
  944.               }
  945.            }
  946.     }
  947.     return FALSE;
  948.     UNREFERENCED_PARAMETER(lParam);
  949.     UNREFERENCED_PARAMETER(hDlg);
  950. }
  951. /*************************************************************************
  952. *
  953. * TextWndProc
  954. *
  955. * Text Window proc.
  956. *
  957. ***************************************************************************/
  958. LONG APIENTRY TextWndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam)
  959. {
  960.     static HFONT hFont = (HFONT) NULL;
  961.     switch (message)
  962.     {
  963.        case WM_CREATE:
  964.        {
  965.           LOGFONT    lf;
  966.           HDC        hDC;
  967.           HFONT      hOldFont;
  968.           TEXTMETRIC tm;
  969.           RECT       rect;
  970.           LONG       lHeight;
  971.           SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, FALSE);
  972.           hDC = GetDC(hwnd);
  973.           // this is the height for 8 point size font in pixels
  974.           lf.lfHeight = 8 * GetDeviceCaps(hDC, LOGPIXELSY) / 72;
  975.           hFont = CreateFontIndirect(&lf);
  976.           hOldFont = SelectObject(hDC, hFont);
  977.           GetTextMetrics(hDC, &tm);
  978.           GetClientRect(GetParent(hwnd), &rect);
  979.           // base the height of the window on size of text
  980.           lHeight = tm.tmHeight+6*GetSystemMetrics(SM_CYBORDER)+2;
  981.           // saved the height for later reference
  982.           SetWindowLong(hwnd, GWL_USERDATA, lHeight);
  983.           SetWindowPos(hwnd, NULL, 0, rect.bottom-lHeight,
  984.                        rect.right-rect.left, lHeight,
  985.                        SWP_NOZORDER | SWP_NOMOVE);
  986.           ReleaseDC(hwnd, hDC);
  987.           break;
  988.        }
  989.     case WM_DESTROY:
  990.        if (hFont)
  991.           DeleteObject(hFont);
  992.        break;
  993.     case WM_SETTEXT:
  994.        DefWindowProc(hwnd, message, wParam, lParam);
  995.        InvalidateRect(hwnd,NULL,FALSE);
  996.        UpdateWindow(hwnd);
  997.        return 0L;
  998.     case WM_PAINT:
  999.     {
  1000.        PAINTSTRUCT ps;
  1001.        RECT   rc;
  1002.        char   ach[128];
  1003.        int    len, nxBorder, nyBorder;
  1004.        HFONT  hOldFont = NULL;
  1005.        BeginPaint(hwnd, &ps);
  1006.        GetClientRect(hwnd,&rc);
  1007.        nxBorder = GetSystemMetrics(SM_CXBORDER);
  1008.        rc.left  += 9*nxBorder;
  1009.        rc.right -= 9*nxBorder;
  1010.        nyBorder = GetSystemMetrics(SM_CYBORDER);
  1011.        rc.top    += 3*nyBorder;
  1012.        rc.bottom -= 3*nyBorder;
  1013.        // 3D Text
  1014.        len = GetWindowText(hwnd, ach, sizeof(ach));
  1015.        SetBkColor(ps.hdc, GetSysColor(COLOR_BTNFACE));
  1016.        SetBkMode(ps.hdc, TRANSPARENT);
  1017.        SetTextColor(ps.hdc, RGB(64,96,96));
  1018.        if (hFont)
  1019.            hOldFont = SelectObject(ps.hdc, hFont);
  1020.        ExtTextOut(ps.hdc, rc.left+2*nxBorder+2, rc.top+2,
  1021.                   ETO_OPAQUE | ETO_CLIPPED, &rc, ach, len, NULL);
  1022.        SetTextColor(ps.hdc, RGB(128,128,128));
  1023.        if (hFont)
  1024.            hOldFont = SelectObject(ps.hdc, hFont);
  1025.        ExtTextOut(ps.hdc, rc.left+2*nxBorder+1, rc.top+1, ETO_CLIPPED,
  1026.                    &rc, ach, len, NULL);
  1027.        SetTextColor(ps.hdc, RGB(255,255,255));
  1028.        if (hFont)
  1029.            hOldFont = SelectObject(ps.hdc, hFont);
  1030.        ExtTextOut(ps.hdc, rc.left+2*nxBorder, rc.top, ETO_CLIPPED,
  1031.                    &rc, ach, len, NULL);
  1032.        SetBkMode(ps.hdc, OPAQUE);
  1033.        if (hOldFont)
  1034.            SelectObject(ps.hdc, hOldFont);
  1035.        EndPaint(hwnd, &ps);
  1036.        return 0L;
  1037.        }
  1038.     }
  1039.     return DefWindowProc(hwnd, message, wParam, lParam);
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. //
  1043. // FUNCTION:    GetStringRes (int id INPUT ONLY)
  1044. //
  1045. // COMMENTS:    Load the resource string with the ID given, and return a
  1046. //              pointer to it.  Notice that the buffer is common memory so
  1047. //              the string must be used before this call is made a second time.
  1048. //
  1049. //---------------------------------------------------------------------------
  1050. LPTSTR GetStringRes (int id)
  1051. {
  1052.   static TCHAR buffer[MAX_PATH];
  1053.   buffer[0]=0;
  1054.   LoadString (GetModuleHandle (NULL), id, buffer, MAX_PATH);
  1055.   return buffer;
  1056. }