home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / RESDLL.ZIP / MAIN.C next >
C/C++ Source or Header  |  1992-09-29  |  5KB  |  186 lines

  1. /******************************************************************************\
  2. *
  3. *  PROGRAM:     MAIN.C
  4. *
  5. *  PURPOSE:     To test the resource-only DLL "THE_DLL.DLL".
  6. *
  7. *  FUNCTIONS:   WinMain()     - initialization, create window, msg loop
  8. *               MainWndProc() - processes main window msgs
  9. *               DrawBitmap()  - draw a specified bitmap in a specified DC
  10. *
  11. *  COMMENTS:    Loads the bitmap, the cursor, and the icon from
  12. *               THE_DLL.DLL. Uses the first two ni registering the
  13. *               app's window class. Uses the bitmap when painting
  14. *               the client area.
  15. *
  16. *
  17. *                           Microsoft Developer Support
  18. *                     Copyright (c) 1992 Microsoft Corporation
  19. *
  20. \******************************************************************************/
  21.  
  22. #include <windows.h>
  23. #include "Main.h"
  24. #include "the_dll.h"
  25.  
  26.  
  27.  
  28. /******************************************************************************\
  29. *
  30. *  FUNCTION:    WinMain (standard WinMain INPUTS/RETURNS)
  31. *
  32. *  GLOBAL VARS: ghLib - library instance handle
  33. *
  34. *  LOCAL VARS:  hwnd - handle of the main standard window
  35. *               msg  - msg to get/dispatch
  36. *
  37. \******************************************************************************/
  38.  
  39. int WINAPI WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  40.                     LPSTR lpCmdLine,  int nCmdShow)
  41. {
  42.   HWND hwnd;
  43.   MSG msg;
  44.  
  45.  
  46.   if ((ghLib = LoadLibrary ((LPTSTR)"the_dll.dll")) == NULL)
  47.   {
  48.     MessageBox (NULL, (LPCTSTR) "WinMain(): LoadLibrary() failed",
  49.                 (LPCTSTR) "Err! - RESDLL", MB_OK | MB_ICONEXCLAMATION);
  50.     return 0;
  51.   }
  52.  
  53.   if (!hPrevInstance)
  54.   {
  55.     WNDCLASS wc;
  56.  
  57.     wc.style         = CS_HREDRAW | CS_VREDRAW;;
  58.     wc.lpfnWndProc   = (WNDPROC)MainWndProc;
  59.     wc.cbClsExtra    = 0;
  60.     wc.cbWndExtra    = 0;
  61.     wc.hInstance     = hInstance;
  62.     wc.hIcon         = LoadIcon (ghLib, "dllicon");
  63.     wc.hCursor       = LoadCursor (ghLib, "dllcursor");
  64.     wc.hbrBackground = GetStockObject (WHITE_BRUSH);
  65.     wc.lpszMenuName  = NULL;
  66.     wc.lpszClassName = (LPCTSTR) "RESDLL";
  67.  
  68.     if (!RegisterClass (&wc))
  69.     {
  70.       MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed",
  71.                   (LPCTSTR) "Err! - MAIN", MB_OK | MB_ICONEXCLAMATION);
  72.       return(FALSE);
  73.     }
  74.   }
  75.  
  76.   if (!(hwnd = CreateWindow ("RESDLL", "RESDLL Sample Application",
  77.                              WS_OVERLAPPEDWINDOW,
  78.                              CW_USEDEFAULT, CW_USEDEFAULT,
  79.                              CW_USEDEFAULT, CW_USEDEFAULT,
  80.                              NULL, NULL, hInstance, NULL)))
  81.     return (NULL);
  82.  
  83.   ShowWindow (hwnd, nCmdShow);
  84.  
  85.   while (GetMessage (&msg, NULL, NULL, NULL))
  86.   {
  87.     TranslateMessage (&msg);
  88.     DispatchMessage  (&msg);
  89.   }
  90.   FreeLibrary (ghLib);
  91.   return (msg.wParam);
  92. }
  93.  
  94.  
  95.  
  96. /******************************************************************************\
  97. *
  98. *  FUNCTION:    MainWndProc (standard window procedure INPUTS/RETURNS)
  99. *
  100. *  GLOBAL VARS: ghLib - library instance handle
  101. *
  102. *  LOCAL VARS:  hbm - handle of bitmap in THE_DLL.DLL
  103. *
  104. \******************************************************************************/
  105.  
  106. LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam,
  107.                               LPARAM lParam)
  108. {
  109.   static HBITMAP hbm;
  110.  
  111.   switch (message)
  112.   {
  113.     case WM_CREATE:
  114.  
  115.       hbm = LoadBitmap (ghLib, "dllbitmap");
  116.       break;
  117.  
  118.     case WM_PAINT:
  119.     {
  120.       RECT        rect;
  121.       PAINTSTRUCT ps;
  122.  
  123.       GetClientRect (hwnd, &rect);
  124.       BeginPaint (hwnd, &ps);
  125.  
  126.       //
  127.       // draw the dllbitmap centered in middle of our client rect
  128.       //
  129.  
  130.       DrawBitmap (ps.hdc, hbm,
  131.                   rect.right/2 - DLLBITMAP_WIDTH/2,
  132.                   rect.bottom/2 - DLLBITMAP_HEIGHT/2);
  133.       EndPaint (hwnd, &ps);
  134.       return 0;
  135.     }
  136.  
  137.     case WM_DESTROY:
  138.  
  139.       DeleteObject ((HANDLE) hbm);
  140.       PostQuitMessage (NULL);
  141.       break;
  142.  
  143.     default:
  144.  
  145.       return (DefWindowProc(hwnd, message, wParam, lParam));
  146.   }
  147.   return (NULL);
  148. }
  149.  
  150.  
  151.  
  152. /******************************************************************************\
  153. *
  154. *  FUNCTION:    DrawBitmap
  155. *
  156. *  INPUTS:      hdc    - device context in which to draw bitmap
  157. *               hbm    - handle of bitmap to draw
  158. *               xStart - x-coordinate of upper-left corner of destination
  159. *                        rectangle
  160. *               yStart - y-coordinate of upper-left corner of destination
  161. *                        rectangle
  162. *
  163. *  LOCAL VARS:  bm     - BITMAP info of "hbm"
  164. *               hdcMem - a memory DC used for blt-ing
  165. *
  166. *  COMMENTS:    Draws a bitmap "hbm" in a DC "hdc" given the upper-left
  167. *               corner "xStart,yStart" of a destination rectangle.
  168. *
  169. \******************************************************************************/
  170.  
  171. void DrawBitmap (HDC hdc, HBITMAP hbm, int xStart, int yStart)
  172. {
  173.   BITMAP bm;
  174.   HDC    hdcMem;
  175.  
  176.   hdcMem = CreateCompatibleDC (hdc);
  177.   SelectObject (hdcMem, hbm);
  178.   SetMapMode (hdcMem, GetMapMode(hdc));
  179.  
  180.   GetObject (hbm, sizeof(BITMAP), (LPSTR)&bm);
  181.   BitBlt (hdc, xStart, yStart, bm.bmWidth, bm.bmHeight,
  182.           hdcMem, 0, 0, SRCCOPY);
  183.  
  184.   DeleteDC(hdcMem);
  185. }
  186.