home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l410 / 1.ddi / CDK / PUSH / PUSH.C$ / PUSH.bin
Encoding:
Text File  |  1992-02-13  |  6.1 KB  |  232 lines

  1. //---------------------------------------------------------------------------
  2. //        Copyright (C) 1991 Microsoft Corporation
  3. //
  4. // You have a royalty-free right to use, modify, reproduce and distribute
  5. // the Sample Custom Control Files (and/or any modified version) in any way
  6. // you find useful, provided that you agree that Microsoft has no warranty,
  7. // obligation or liability for any Custom Control File.
  8. //---------------------------------------------------------------------------
  9. // Push.c
  10. //---------------------------------------------------------------------------
  11. // Picture Push Button Control
  12. //---------------------------------------------------------------------------
  13.  
  14. #define  NOCOMM
  15. #include <windows.h>
  16.  
  17. #include <vbapi.h>
  18.  
  19. #define  CTL_DATA        // To declare static data in push.h
  20. #include "push.h"
  21.  
  22.  
  23. //---------------------------------------------------------------------------
  24. // Standard Error Values
  25. //---------------------------------------------------------------------------
  26. #define ERR_None          0
  27. #define ERR_InvPropVal        380     // Error$(380) = "Invalid property value"
  28.  
  29.  
  30. //---------------------------------------------------------------------------
  31. // Local Prototypes
  32. //---------------------------------------------------------------------------
  33. VOID NEAR DrawBtn(HCTL hctl, LPDRAWITEMSTRUCT lpdi);
  34.  
  35.  
  36. //---------------------------------------------------------------------------
  37. // Event Procedure Parameter Profiles
  38. //---------------------------------------------------------------------------
  39. typedef struct tagPARAMS
  40. {
  41.     HLSTR   ClickString;
  42.     LPVOID  Index;        // Reserve space for index parameter to array ctl
  43. } PARAMS;
  44.  
  45.  
  46. //---------------------------------------------------------------------------
  47. // Push Control Procedure
  48. //---------------------------------------------------------------------------
  49. LONG FAR PASCAL _export PushCtlProc
  50. (
  51.     HCTL    hctl,
  52.     HWND        hwnd,
  53.     USHORT      msg,
  54.     USHORT      wp,
  55.     LONG        lp
  56. )
  57. {
  58.     LONG        lResult;
  59.  
  60.     // Message pre-processing
  61.     switch (msg)
  62.     {
  63.     case WM_ERASEBKGND:
  64.         // Don't bother with erasing the background
  65.         return TRUE;
  66.  
  67.     case VBM_MNEMONIC:
  68.         // Act like a click
  69.         lp = MAKELONG(0,BN_CLICKED);
  70.         // **** FALL THROUGH ****
  71.  
  72.     case VBN_COMMAND:
  73.         switch (HIWORD(lp))
  74.         {
  75.         case BN_CLICKED:
  76.             {
  77.             PARAMS params;
  78.             char   szStrBuf[20];
  79.             int    cbCaption;
  80.             ERR    err;
  81.             LPSTR  lpstr;
  82.  
  83.             cbCaption = GetWindowText(hwnd, szStrBuf, 20);
  84.             params.ClickString = VBCreateHlstr(szStrBuf, cbCaption);
  85.             err = VBFireEvent(hctl, EVENT_PUSH_CLICK, ¶ms);
  86.                     if (!err)
  87.             {
  88.             lpstr = VBDerefHlstr(params.ClickString);
  89.                         if (lpstr == NULL)
  90.                 {
  91.                 szStrBuf[0] = '\0';
  92.                 SetWindowText(hwnd, szStrBuf);
  93.                 }
  94.                         else
  95.                 {
  96.                 lpstr[VBGetHlstrLen(params.ClickString) - 1] = '\0';
  97.                             SetWindowText(hwnd, lpstr);
  98.                 }
  99.             }
  100.             VBDestroyHlstr(params.ClickString);
  101.             }
  102.             break;
  103.         }
  104.         return 0L;
  105.  
  106.     case VBM_SETPROPERTY:
  107.         switch (wp)
  108.         {
  109.         case IPROP_PUSH_CAPTION:
  110.             // To avoid a Windows problem, make sure text is
  111.             // under 255 bytes:
  112.             if (lstrlen((LPSTR)lp) > 255)
  113.             *((LPSTR)lp + 255) = 0;
  114.             break;
  115.         }
  116.         break;
  117.  
  118.     case VBM_CHECKPROPERTY:
  119.         switch (wp)
  120.         {
  121.         case IPROP_PUSH_PICTUREUP:
  122.         case IPROP_PUSH_PICTUREDOWN:
  123.             {
  124.             PIC pic;
  125.  
  126.             VBGetPic((HPIC)lp, &pic);
  127.             switch (pic.picType)
  128.             {
  129.             case PICTYPE_NONE:
  130.             case PICTYPE_BITMAP:
  131.             case PICTYPE_ICON:
  132.                 InvalidateRect(hwnd, NULL, TRUE);
  133.                 return ERR_None;
  134.             }
  135.             return ERR_InvPropVal;
  136.             }
  137.         }
  138.         break;
  139.  
  140.     case VBN_DRAWITEM:
  141.         DrawBtn(hctl, (LPDRAWITEMSTRUCT)lp);
  142.         return 0;
  143.     }
  144.  
  145.     // Default processing:
  146.     lResult = VBDefControlProc(hctl, hwnd, msg, wp, lp);
  147.  
  148.     // Message post-processing:
  149.     switch (msg)
  150.     {
  151.     case WM_DESTROY:
  152.         VBFreePic(PUSHDEREF(hctl)->hpicDown);
  153.         VBFreePic(PUSHDEREF(hctl)->hpicUp);
  154.         break;
  155.     }
  156.  
  157.     return lResult;
  158. }
  159.  
  160.  
  161. //---------------------------------------------------------------------------
  162. // Paint the push button.
  163. //---------------------------------------------------------------------------
  164. VOID NEAR DrawBtn
  165. (
  166.     HCTL         hctl,
  167.     LPDRAWITEMSTRUCT lpdi
  168. )
  169. {
  170.     HPIC    hpic;
  171.     PIC     pic;
  172.     PPUSH   ppush = PUSHDEREF(hctl);
  173.     BITMAP  bmp;
  174.     HDC     hdcMem;
  175.     RECT    rect;
  176.     SHORT   inflate;
  177.  
  178.     switch (lpdi->itemAction)
  179.     {
  180.     case ODA_SELECT:
  181.     case ODA_DRAWENTIRE:
  182.         hpic = (lpdi->itemState & ODS_SELECTED) ? ppush->hpicDown
  183.                             : ppush->hpicUp;
  184.         VBGetPic(hpic, &pic);
  185.         switch (pic.picType)
  186.         {
  187.         case PICTYPE_BITMAP:
  188.             GetObject(pic.picData.bmp.hbitmap, sizeof(BITMAP), (LPSTR)&bmp);
  189.             hdcMem = CreateCompatibleDC(lpdi->hDC);
  190.             SelectObject(hdcMem, pic.picData.bmp.hbitmap);
  191.             StretchBlt(lpdi->hDC, 0, 0,
  192.                    lpdi->rcItem.right - lpdi->rcItem.left + 1,
  193.                    lpdi->rcItem.bottom - lpdi->rcItem.top + 1,
  194.                    hdcMem,      0, 0, bmp.bmWidth, bmp.bmHeight,
  195.                    SRCCOPY);
  196.             DeleteDC(hdcMem);
  197.             break;
  198.  
  199.         case PICTYPE_ICON:
  200.         case PICTYPE_NONE:
  201.             {
  202.             HBRUSH  hbr;
  203.             RECT    rect;
  204.  
  205.             hbr = (HBRUSH)SendMessage(GetParent(lpdi->hwndItem),
  206.             WM_CTLCOLOR, lpdi->hDC, MAKELONG(lpdi->hwndItem, 0));
  207.             GetClipBox(lpdi->hDC, &rect);
  208.             FillRect(lpdi->hDC, &rect, hbr);
  209.             if (pic.picType == PICTYPE_ICON)
  210.             DrawIcon(lpdi->hDC, 0, 0, pic.picData.wmf.hmeta);
  211.             else if (lpdi->itemState & ODS_SELECTED)
  212.             InvertRect(lpdi->hDC, &rect);
  213.             }
  214.             break;
  215.         }
  216.         // **** FALL THROUGH ****
  217.  
  218.     case ODA_FOCUS:
  219.         if (lpdi->itemState & ODS_FOCUS)
  220.         {
  221.         CopyRect((LPRECT)&rect, (LPRECT)&lpdi->rcItem);
  222.         inflate = min(3,min(rect.right    - rect.left + 1,
  223.                     rect.bottom - rect.top  + 1) / 5);
  224.         InflateRect(&rect, -inflate, -inflate);
  225.         DrawFocusRect(lpdi->hDC, &rect);
  226.         }
  227.         break;
  228.     }
  229. }
  230.  
  231. //---------------------------------------------------------------------------
  232.