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

  1. /*
  2.     Ownerb.c
  3.  
  4.     This is sample app to show how owner draw buttons work.
  5.  
  6.     12-Aug-91   Updated by NigelT to make it simpler.
  7.  
  8. */
  9.  
  10. #include <windows.h>
  11. #include "Ownerb.h"
  12.  
  13. #define IDC_BUTTON  1           // child id
  14.  
  15.  
  16. void DrawControl(HWND hWnd, LPDRAWITEMSTRUCT lpInfo);
  17.  
  18.  
  19. /* usefull global things */
  20.  
  21. HANDLE hInst;                   /* global instance handle */
  22. char *szAppName = "Ownerb";
  23. HWND hMainWnd;                  /* handle of main window */
  24.  
  25. HWND hwndButton;
  26.  
  27. // local functions
  28.  
  29. long FAR PASCAL __export MainWndProc(HWND, UINT , WPARAM, LPARAM);
  30. BOOL InitFirstInstance(HANDLE);
  31.  
  32. /***************** Main entry point routine *************************/
  33.  
  34. int PASCAL WinMain(hInstance,hPrevInstance,lpszCmdLine,cmdShow)
  35. HANDLE hInstance,hPrevInstance;
  36. LPSTR lpszCmdLine;
  37. int cmdShow;
  38. {
  39.     MSG msg;
  40.  
  41.     hInst = hInstance;          /* save our instance handle */
  42.  
  43.     if (!hPrevInstance) {
  44.         if (! InitFirstInstance(hInstance)) {
  45.             return 1;
  46.         }
  47.     }
  48.  
  49.     /* create a window for the application */
  50.  
  51.     hMainWnd = CreateWindow(szAppName,          /* class name */
  52.                         szAppName,              /* caption text */
  53.                         WS_OVERLAPPEDWINDOW,    /* window style */
  54.                         CW_USEDEFAULT, 0,
  55.                         200, 100,
  56.                         (HWND)NULL,             /* handle of parent window */
  57.                         (HMENU)NULL,            /* menu handle (default class) */
  58.                         hInstance,              /* handle to window instance */
  59.                         (LPSTR)NULL             /* no params to pass on */
  60.                         );
  61.  
  62.     if (!hMainWnd) {
  63.         return 1;
  64.     }
  65.  
  66.     ShowWindow(hMainWnd, cmdShow); /* display window as open or icon */
  67.     UpdateWindow(hMainWnd);     /* paint it */
  68.  
  69.     /* Process messages for us */
  70.  
  71.     while (GetMessage(&msg, NULL, 0, 0)) {
  72.         TranslateMessage(&msg);
  73.         DispatchMessage(&msg);
  74.     }
  75.  
  76.     return (msg.wParam);
  77. }
  78.  
  79. /************* main window message handler ******************************/
  80.  
  81. long FAR PASCAL __export MainWndProc(hWnd, message, wParam, lParam)
  82. HWND hWnd;
  83. UINT message;
  84. WPARAM wParam;
  85. LPARAM lParam;
  86. {
  87.     PAINTSTRUCT ps;             /* paint structure */
  88.  
  89.     /* process any messages we want */
  90.  
  91.     switch(message) {
  92.     case WM_CREATE:
  93.     hwndButton = CreateWindow("button",
  94.                               "Button",
  95.                               WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
  96.                               40, 20,
  97.                               40, 20,
  98.                               hWnd,
  99.                               IDC_BUTTON,
  100.                               hInst,
  101.                               (LPSTR)NULL);
  102.         break;
  103.  
  104.     case WM_PAINT:
  105.         BeginPaint(hWnd, &ps);
  106.         EndPaint(hWnd, &ps);
  107.         break;
  108.  
  109.     case WM_DRAWITEM:
  110.         // owner draw control stuff
  111.         DrawControl(hWnd, (LPDRAWITEMSTRUCT)lParam);
  112.         break;
  113.  
  114.     case WM_DESTROY:
  115.         PostQuitMessage(0);
  116.         break;
  117.  
  118.     default:
  119.         return DefWindowProc(hWnd, message, wParam, lParam);
  120.         break;
  121.     }
  122.     return NULL;
  123. }
  124.  
  125. void DrawControl(HWND hWnd, LPDRAWITEMSTRUCT lpInfo)
  126. {
  127.     HBITMAP hbm, hOldbm;
  128.     int ResourceID;
  129.     HDC hMemDC;
  130.  
  131.  
  132.     if (lpInfo->CtlType != ODT_BUTTON) return;
  133.  
  134.     // Load the bitmap for the image
  135.  
  136.     switch(lpInfo->CtlID) {
  137.     case IDC_BUTTON:
  138.         ResourceID = IDR_BUTTON;
  139.         break;
  140.  
  141.     default:
  142.         return;
  143.     }
  144.  
  145.     if (lpInfo->itemState & ODS_SELECTED) {
  146.         ResourceID += 1;
  147.     }
  148.  
  149.     hbm = LoadBitmap(hInst, MAKEINTRESOURCE(ResourceID));
  150.     if (!hbm) return;
  151.  
  152.     if ((lpInfo->itemAction & ODA_DRAWENTIRE)
  153.     || (lpInfo->itemAction & ODA_SELECT)) {
  154.  
  155.         // draw the whole button
  156.         hMemDC = CreateCompatibleDC(lpInfo->hDC);
  157.         hOldbm = SelectObject(hMemDC, hbm);
  158.         if (hOldbm) {
  159.             BitBlt(lpInfo->hDC,
  160.                    (lpInfo->rcItem).left,
  161.                    (lpInfo->rcItem).top,
  162.                    (lpInfo->rcItem).right - (lpInfo->rcItem).left,
  163.                    (lpInfo->rcItem).bottom - (lpInfo->rcItem).top,
  164.                    hMemDC,
  165.                    0, 0,
  166.                    SRCCOPY);
  167.             SelectObject(hMemDC, hOldbm);
  168.         }
  169.         DeleteDC(hMemDC);
  170.     }
  171.     DeleteObject(hbm);
  172. }
  173.  
  174. BOOL InitFirstInstance(hInstance)
  175. HANDLE hInstance;
  176. {
  177.     WNDCLASS wc;
  178.  
  179.     /* define the class of window we want to register */
  180.  
  181.     wc.lpszClassName    = szAppName;
  182.     wc.style            = CS_HREDRAW | CS_VREDRAW;
  183.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  184.     wc.hIcon            = LoadIcon(hInstance,"Icon");
  185.     wc.lpszMenuName     = "Menu";
  186.     wc.hbrBackground    = COLOR_WINDOW+1;
  187.     wc.hInstance        = hInstance;
  188.     wc.lpfnWndProc      = MainWndProc;
  189.     wc.cbClsExtra       = 0;
  190.     wc.cbWndExtra       = 0;
  191.  
  192.     if (! RegisterClass(&wc)) {
  193.         return FALSE; /* Initialisation failed */
  194.     }
  195.  
  196.     return TRUE;
  197. }
  198.