home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / MAKEAPP / CLIENT.C_ / CLIENT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  4.6 KB  |  185 lines

  1. #include "makeapp.h"
  2.  
  3. BOOL Client_Initialize(APP* papp)
  4. {
  5.     if (!papp->hinstPrev)
  6.     {
  7.         WNDCLASS cls;
  8.  
  9.         cls.hCursor         = LoadCursor(NULL, IDC_ARROW);
  10.         cls.hIcon           = NULL;
  11.         cls.lpszMenuName    = NULL;
  12.         cls.hInstance       = papp->hinst;
  13.         cls.lpszClassName   = "MakeApp_Client";
  14.         cls.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
  15.         cls.lpfnWndProc     = Client_WndProc;
  16.         cls.style           = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  17.         cls.cbWndExtra      = sizeof(CLIENT*);
  18.         cls.cbClsExtra      = 0;
  19.  
  20.         if (!RegisterClass(&cls))
  21.             return FALSE;
  22.     }
  23.  
  24.     return TRUE;
  25. }
  26.  
  27. void Client_Terminate(APP* papp, int codeTerm)
  28. {
  29. }
  30.  
  31. LRESULT CALLBACK _export Client_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  32. {
  33.     CLIENT* pcli = Client_GetPtr(hwnd);
  34.  
  35.     if (pcli == NULL)
  36.     {
  37.         if (msg == WM_NCCREATE)
  38.         {
  39.             pcli = (CLIENT*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, sizeof(CLIENT));
  40.  
  41.             if (pcli == NULL)
  42.                 return 0L;
  43.  
  44.             pcli->hwnd = hwnd;
  45.             Client_SetPtr(hwnd, pcli);
  46.         }
  47.         else
  48.         {
  49.             return Client_DefProc(hwnd, msg, wParam, lParam);
  50.         }
  51.     }
  52.  
  53.     if (msg == WM_NCDESTROY)
  54.     {
  55.         //DWORD result = HANDLE_MSG(hwnd, WM_NCDESTROY, Client_OnNCDestroy);
  56.  
  57.         LocalFree((HLOCAL)pcli);
  58.         pcli = NULL;
  59.         Client_SetPtr(hwnd, NULL);
  60.  
  61.         //return result;
  62.     }
  63.  
  64.     switch (msg)
  65.     {
  66.         HANDLE_MSG(pcli, WM_CREATE, Client_OnCreate);
  67.         HANDLE_MSG(pcli, WM_DESTROY, Client_OnDestroy);
  68.  
  69.         HANDLE_MSG(pcli, WM_PAINT, Client_OnPaint);
  70.         HANDLE_MSG(pcli, WM_ERASEBKGND, Client_OnEraseBkgnd);
  71.  
  72.         HANDLE_MSG(pcli, WM_QUERYENDSESSION, Client_OnQueryEndSession);
  73.         HANDLE_MSG(pcli, WM_ENDSESSION, Client_OnEndSession);
  74.  
  75.         HANDLE_MSG(pcli, WM_COMMAND, Client_OnCommand);
  76.     default:
  77.         return Client_DefProc(hwnd, msg, wParam, lParam);
  78.     }
  79. }
  80.  
  81. // Private structure used for passing info via WM_CREATE message
  82. typedef struct
  83. {
  84.     LPCSTR lpszText;
  85.     COLORREF clrText;
  86. } CLIENT_INIT;
  87.  
  88. HWND Client_CreateWindow(HWND hwndParent, int x, int y, int cx, int cy, BOOL fVisible, COLORREF clrText, LPCSTR lpszText)
  89. {
  90.     CLIENT_INIT init;
  91.  
  92.     init.lpszText = lpszText;
  93.     init.clrText = clrText;
  94.  
  95.     return CreateWindowEx(
  96.         0L,                 // extendedStyle
  97.             "MakeApp_Client",               // class name
  98.             NULL,                           // text
  99.             (fVisible ? (WS_CHILD | WS_VISIBLE) : WS_CHILD),
  100.             x, y, cx, cy,                   // x, y, cx, cy
  101.             hwndParent,                     // hwndParent
  102.         NULL,                // hmenu
  103.             g_app.hinst,                    // hInstance
  104.             &init);                         // lpCreateParams
  105. }
  106.  
  107. BOOL Client_OnCreate(CLIENT* pcli, CREATESTRUCT FAR* lpCreateStruct)
  108. {
  109.     CLIENT_INIT FAR* pinit = (CLIENT_INIT FAR*)lpCreateStruct->lpCreateParams;
  110.  
  111.     pcli->lpszText = pinit->lpszText;
  112.     pcli->clrText  = pinit->clrText;
  113.     return TRUE;
  114. }
  115.  
  116. void Client_OnDestroy(CLIENT* pcli)
  117. {
  118. }
  119.  
  120. void Client_OnPaint(CLIENT* pcli)
  121. {
  122.     PAINTSTRUCT ps;
  123.     HDC hdc;
  124.     RECT rc;
  125.  
  126.     hdc = BeginPaint(pcli->hwnd, &ps);
  127.  
  128.     GetClientRect(pcli->hwnd, &rc);
  129.     //
  130.     // No need to erase the background: that was done by WM_ERASEBKGND handling
  131.     //
  132.     InflateRect(&rc, -4, -4);
  133.     FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
  134.  
  135.     if (pcli->lpszText)
  136.     {
  137.         int cch = lstrlen(pcli->lpszText);
  138.         int x;
  139.         int y;
  140.         SIZE size;
  141.  
  142.         GetTextExtentPoint(hdc, pcli->lpszText, cch, &size);
  143.  
  144.         x = rc.left + (rc.right - rc.left - size.cx) / 2;
  145.         y = rc.top + (rc.bottom - rc.top - size.cy) / 2;
  146.  
  147.         SetTextColor(hdc, pcli->clrText);
  148.         TextOut(hdc, x, y, pcli->lpszText, cch);
  149.     }
  150.  
  151.     EndPaint(pcli->hwnd, &ps);
  152. }
  153.  
  154. BOOL Client_OnEraseBkgnd(CLIENT* pcli, HDC hdc)
  155. {
  156.     return FORWARD_WM_ERASEBKGND(pcli->hwnd, hdc, Client_DefProc);
  157. }
  158.  
  159. BOOL Client_OnQueryEndSession(CLIENT* pcli)
  160. {
  161.     // Signal that it's OK to end the session.
  162.     //
  163.     return TRUE;
  164. }
  165.  
  166. void Client_OnEndSession(CLIENT* pcli, BOOL fEnding)
  167. {
  168.     // No end session cleanup to do.
  169. }
  170.  
  171. void Client_OnCommand(CLIENT* pcli, int id, HWND hwndCtl, UINT codeNotify)
  172. {
  173.     // NOTE: WM_COMMAND not handled by the frame are passed to the client.
  174.     //
  175.     switch (id)
  176.     {
  177.     case CMD_SAMPLEDLG:
  178.         SampleDlg_Do(pcli->hwnd);
  179.         break;
  180.     default:
  181.         MessageBeep(0);
  182.         break;
  183.     }
  184. }
  185.