home *** CD-ROM | disk | FTP | other *** search
/ Lion Share / lionsharecd.iso / utils_mz / v10n17.zip / OWNERDRW.ZIP / OWNERDRW.C < prev    next >
C/C++ Source or Header  |  1991-06-09  |  7KB  |  200 lines

  1. /*----------------------------------------------
  2.    OWNERDRW.C -- Owner Draw Button Demo Program
  3.                  (c) Charles Petzold, 1991
  4.   ----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "ownerdrw.h"
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10.  
  11. HANDLE hInst ;
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.             LPSTR lpszCmdLine, int nCmdShow)
  15.      {
  16.      static char szAppName [] = "OwnerDrw" ;
  17.      MSG         msg;
  18.      HWND        hwnd ;
  19.      WNDCLASS    wndclass ;
  20.  
  21.      hInst = hInstance ;
  22.  
  23.      if (!hPrevInstance) 
  24.           {
  25.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  26.           wndclass.lpfnWndProc   = WndProc ;
  27.           wndclass.cbClsExtra    = 0 ;
  28.           wndclass.cbWndExtra    = 0 ;
  29.           wndclass.hInstance     = hInstance ;
  30.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  31.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  32.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  33.           wndclass.lpszMenuName  = szAppName ;
  34.           wndclass.lpszClassName = szAppName ;
  35.  
  36.           RegisterClass (&wndclass) ;
  37.           }
  38.  
  39.      hwnd = CreateWindow (szAppName, "Onwer Draw Button Demo",
  40.                           WS_OVERLAPPEDWINDOW,
  41.               CW_USEDEFAULT, CW_USEDEFAULT,
  42.               CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.  
  45.      ShowWindow (hwnd, nCmdShow) ;
  46.      UpdateWindow (hwnd); 
  47.  
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.           {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.           }
  53.      return msg.wParam ;
  54.      }
  55.  
  56. BOOL FAR PASCAL ExitDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  57.      {
  58.      static HBITMAP   hbmOk, hbmCan ;
  59.      BITMAP           bm ;
  60.      HBITMAP          hbm ;
  61.      HBRUSH           hBrush ;
  62.      HDC              hdcMemory ;
  63.      HPEN             hPen ;
  64.      LPDRAWITEMSTRUCT lpdis ;
  65.  
  66.      switch (message)
  67.           {
  68.           case WM_INITDIALOG:
  69.                hbmOk  = LoadBitmap (hInst, "OkBitmap") ;
  70.                hbmCan = LoadBitmap (hInst, "CancelBitmap") ;
  71.                return TRUE ;
  72.  
  73.           case WM_DRAWITEM:
  74.                lpdis = (LPDRAWITEMSTRUCT) lParam ;
  75.  
  76.                switch (lpdis->CtlID)
  77.                     {
  78.                     case IDOK:      hbm = hbmOk  ;  break ;
  79.                     case IDCANCEL:  hbm = hbmCan ;  break ;
  80.                     default:        return FALSE ;
  81.                     }
  82.  
  83.                if (hbm != NULL)
  84.                     {
  85.                     GetObject (hbm, sizeof (BITMAP), (LPSTR) &bm) ;
  86.                     hdcMemory = CreateCompatibleDC (lpdis->hDC) ;
  87.                     SelectObject (hdcMemory, hbm) ;
  88.                     StretchBlt (lpdis->hDC, 0, 0,
  89.                                 lpdis->rcItem.right, lpdis->rcItem.bottom,
  90.                                 hdcMemory, 0, 0, bm.bmWidth, bm.bmHeight,
  91.                                 lpdis->itemState & ODS_SELECTED ?
  92.                                         NOTSRCCOPY : SRCCOPY) ;
  93.  
  94.                     DeleteDC (hdcMemory) ;
  95.                     }
  96.  
  97.                if (lpdis->itemState & ODS_FOCUS)
  98.                     {
  99.                     hPen = SelectObject (lpdis->hDC,
  100.                          CreatePen (PS_INSIDEFRAME,
  101.                               (lpdis->rcItem.right + lpdis->rcItem.bottom) / 40,
  102.                               RGB (255, 0, 0))) ;
  103.  
  104.                     hBrush = SelectObject (lpdis->hDC,
  105.                                    GetStockObject (NULL_BRUSH)) ;
  106.  
  107.                     Rectangle (lpdis->hDC, 0, 0, lpdis->rcItem.right,
  108.                                                  lpdis->rcItem.bottom) ;
  109.  
  110.                     DeleteObject (SelectObject (lpdis->hDC, hPen)) ;
  111.                     SelectObject (lpdis->hDC, hBrush) ;
  112.                     }
  113.                return TRUE ;
  114.  
  115.           case WM_COMMAND:
  116.                switch (wParam)
  117.                     {
  118.                     case IDOK:
  119.                          EndDialog (hDlg, TRUE) ;
  120.                          return TRUE ;
  121.  
  122.                     case IDCANCEL:
  123.                          EndDialog (hDlg, FALSE) ;
  124.                          return TRUE ;
  125.                     }
  126.                break ;
  127.  
  128.           case WM_DESTROY:
  129.                DeleteObject (hbmOk) ;
  130.                DeleteObject (hbmCan) ;
  131.                return TRUE ;
  132.           }
  133.  
  134.      return FALSE ;
  135.      }
  136.  
  137. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  138.      {
  139.      switch (message)
  140.           {
  141.           case WM_INITDIALOG:
  142.                return TRUE ;
  143.  
  144.           case WM_COMMAND:
  145.                switch (wParam)
  146.                     {
  147.             case IDOK:
  148.             case IDCANCEL:
  149.                          EndDialog (hDlg, 0) ;
  150.              return TRUE ;
  151.                     }
  152.                break ;
  153.           }
  154.      return FALSE ;
  155.      }
  156.  
  157. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  158.      {
  159.      static FARPROC lpfnExitDlgProc, lpfnAboutDlgProc ;
  160.  
  161.      switch (message)
  162.           {
  163.           case WM_CREATE:
  164.                lpfnExitDlgProc  = MakeProcInstance (ExitDlgProc,  hInst) ;
  165.                lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInst) ;
  166.                return 0 ;
  167.  
  168.           case WM_COMMAND:
  169.                switch (wParam)
  170.                     {
  171.                     case IDM_EXIT:
  172.                          SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  173.                          return 0 ;
  174.  
  175.                     case IDM_ABOUT:
  176.                          DialogBox (hInst, "AboutBox", hwnd,
  177.                                     lpfnAboutDlgProc) ;
  178.                          return 0 ;
  179.                     }
  180.                break ;
  181.  
  182.           case WM_CLOSE:
  183.                if (DialogBox (hInst, "ExitBox", hwnd, lpfnExitDlgProc))
  184.                     DestroyWindow (hwnd) ;
  185.                return 0 ;
  186.  
  187.           case WM_QUERYENDSESSION:
  188.                if (DialogBox (hInst, "ExitBox", hwnd, lpfnExitDlgProc))
  189.                     return 1L ;
  190.                return 0 ;
  191.      
  192.           case WM_DESTROY :
  193.                FreeProcInstance (lpfnExitDlgProc) ;
  194.                FreeProcInstance (lpfnAboutDlgProc) ;
  195.                PostQuitMessage (0) ;
  196.                return 0 ;
  197.           }
  198.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  199.      }
  200.