home *** CD-ROM | disk | FTP | other *** search
/ On Hand / On_Hand_From_Softbank_1994_Release_2_Disc_2_1994.iso / 00202 / s / disk1 / push.c_ / push.bin
Text File  |  1993-04-28  |  10KB  |  335 lines

  1. //---------------------------------------------------------------------------
  2. //        Copyright (C) 1991-93, 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. #include <windows.h>
  15. #include <vbapi.h>
  16. #include "push.h"
  17. #include "pushvb1.h"
  18.  
  19.  
  20. //---------------------------------------------------------------------------
  21. // Global Variables
  22. //---------------------------------------------------------------------------
  23. HANDLE hmodDLL;
  24.  
  25.  
  26. //---------------------------------------------------------------------------
  27. // Standard Error Values
  28. //---------------------------------------------------------------------------
  29. #define ERR_None          0
  30. #define ERR_InvPropVal        380     // Error$(380) = "Invalid property value"
  31.  
  32.  
  33. //---------------------------------------------------------------------------
  34. // Local Prototypes
  35. //---------------------------------------------------------------------------
  36. VOID NEAR DrawBtn(HCTL hctl, LPDRAWITEMSTRUCT lpdi);
  37.  
  38.  
  39. //---------------------------------------------------------------------------
  40. // Event Procedure Parameter Profiles
  41. //---------------------------------------------------------------------------
  42. typedef struct tagPARAMS
  43.     {
  44.     HLSTR  ClickString;
  45.     LPVOID Index;      // Reserve space for index parameter to array ctl
  46.     } PARAMS;
  47.  
  48.  
  49. //---------------------------------------------------------------------------
  50. // Push Control Procedure
  51. //---------------------------------------------------------------------------
  52. LONG FAR PASCAL _export PushCtlProc
  53. (
  54.     HCTL   hctl,
  55.     HWND   hwnd,
  56.     USHORT msg,
  57.     USHORT wp,
  58.     LONG   lp
  59. )
  60. {
  61.     LONG lResult;
  62.  
  63.     // Message pre-processing
  64.     switch (msg)
  65.     {
  66.     case WM_ERASEBKGND:
  67.         // Don't bother with erasing the background
  68.         return TRUE;
  69.  
  70.     case VBM_MNEMONIC:
  71.         // Act like a click
  72.         lp = MAKELONG(0,BN_CLICKED);
  73.         // **** FALL THROUGH ****
  74.  
  75.     case VBN_COMMAND:
  76.         switch (HIWORD(lp))
  77.         {
  78.         case BN_CLICKED:
  79.             {
  80.             PARAMS params;
  81.             char   szStrBuf[20];
  82.             USHORT cbCaption;
  83.             ERR    err;
  84.             LPSTR  lpstr;
  85.  
  86.             cbCaption = (USHORT)GetWindowText(hwnd, szStrBuf, 20);
  87.             params.ClickString = VBCreateHlstr(szStrBuf, cbCaption);
  88.             err = VBFireEvent(hctl, EVENT_PUSH_CLICK, ¶ms);
  89.                     if (!err)
  90.             {
  91.             lpstr = VBDerefHlstr(params.ClickString);
  92.                         if (lpstr == NULL)
  93.                 {
  94.                 szStrBuf[0] = '\0';
  95.                 SetWindowText(hwnd, szStrBuf);
  96.                 }
  97.                         else
  98.                 {
  99.                 lpstr[VBGetHlstrLen(params.ClickString) - 1] = '\0';
  100.                             SetWindowText(hwnd, lpstr);
  101.                 }
  102.             }
  103.             VBDestroyHlstr(params.ClickString);
  104.             }
  105.             break;
  106.         }
  107.         return 0L;
  108.  
  109.     case VBM_SETPROPERTY:
  110.         switch (wp)
  111.         {
  112.         case IPROP_PUSH_CAPTION:
  113.             // To avoid a Windows problem, make sure text is
  114.             // under 255 bytes:
  115.             if (lstrlen((LPSTR)lp) > 255)
  116.             *((LPSTR)lp + 255) = 0;
  117.             break;
  118.         }
  119.         break;
  120.  
  121.     case VBM_CHECKPROPERTY:
  122.         switch (wp)
  123.         {
  124.         case IPROP_PUSH_PICTUREUP:
  125.         case IPROP_PUSH_PICTUREDOWN:
  126.             {
  127.             PIC pic;
  128.  
  129.             VBGetPic((HPIC)lp, &pic);
  130.             switch (pic.picType)
  131.             {
  132.             case PICTYPE_NONE:
  133.             case PICTYPE_BITMAP:
  134.             case PICTYPE_ICON:
  135.                 InvalidateRect(hwnd, NULL, TRUE);
  136.                 return ERR_None;
  137.             }
  138.             return ERR_InvPropVal;
  139.             }
  140.         }
  141.         break;
  142.  
  143.     case VBN_DRAWITEM:
  144.         DrawBtn(hctl, (LPDRAWITEMSTRUCT)lp);
  145.         return 0;
  146.     }
  147.  
  148.     // Default processing:
  149.     lResult = VBDefControlProc(hctl, hwnd, msg, wp, lp);
  150.  
  151.     // Message post-processing:
  152.     switch (msg)
  153.     {
  154.     case WM_DESTROY:
  155.         VBFreePic(LppushDEREF(hctl)->hpicDown);
  156.         VBFreePic(LppushDEREF(hctl)->hpicUp);
  157.         break;
  158.     }
  159.  
  160.     return lResult;
  161. }
  162.  
  163.  
  164. //---------------------------------------------------------------------------
  165. // Paint the push button.
  166. //---------------------------------------------------------------------------
  167. VOID NEAR DrawBtn
  168. (
  169.     HCTL         hctl,
  170.     LPDRAWITEMSTRUCT lpdi
  171. )
  172. {
  173.     HPIC    hpic;
  174.     PIC     pic;
  175.     LPPUSH  lppush = LppushDEREF(hctl);
  176.     BITMAP  bmp;
  177.     HDC     hdcMem;
  178.     RECT    rect;
  179.     SHORT   inflate;
  180.  
  181.     switch (lpdi->itemAction)
  182.     {
  183.     case ODA_SELECT:
  184.     case ODA_DRAWENTIRE:
  185.         hpic = (lpdi->itemState & ODS_SELECTED) ? lppush->hpicDown
  186.                             : lppush->hpicUp;
  187.         VBGetPic(hpic, &pic);
  188.         switch (pic.picType)
  189.         {
  190.         case PICTYPE_BITMAP:
  191.             GetObject(pic.picData.bmp.hbitmap, sizeof(BITMAP), (LPSTR)&bmp);
  192.             hdcMem = CreateCompatibleDC(lpdi->hDC);
  193.             SelectObject(hdcMem, pic.picData.bmp.hbitmap);
  194.             StretchBlt(lpdi->hDC, 0, 0,
  195.                    lpdi->rcItem.right - lpdi->rcItem.left + 1,
  196.                    lpdi->rcItem.bottom - lpdi->rcItem.top + 1,
  197.                    hdcMem,      0, 0, bmp.bmWidth, bmp.bmHeight,
  198.                    SRCCOPY);
  199.             DeleteDC(hdcMem);
  200.             break;
  201.  
  202.         case PICTYPE_ICON:
  203.         case PICTYPE_NONE:
  204.             {
  205.             HBRUSH  hbr;
  206.             RECT    rect;
  207.  
  208.             hbr = (HBRUSH)SendMessage(GetParent(lpdi->hwndItem),
  209.             WM_CTLCOLOR, lpdi->hDC, MAKELONG(lpdi->hwndItem, 0));
  210.             GetClipBox(lpdi->hDC, &rect);
  211.             FillRect(lpdi->hDC, &rect, hbr);
  212.             if (pic.picType == PICTYPE_ICON)
  213.             DrawIcon(lpdi->hDC, 0, 0, pic.picData.wmf.hmeta);
  214.             else if (lpdi->itemState & ODS_SELECTED)
  215.             InvertRect(lpdi->hDC, &rect);
  216.             }
  217.             break;
  218.         }
  219.         // **** FALL THROUGH ****
  220.  
  221.     case ODA_FOCUS:
  222.         if (lpdi->itemState & ODS_FOCUS)
  223.         {
  224.         CopyRect((LPRECT)&rect, (LPRECT)&lpdi->rcItem);
  225.         inflate = (SHORT)min(3,min(rect.right  - rect.left + 1,
  226.                     rect.bottom - rect.top  + 1) / 5);
  227.         InflateRect(&rect, -inflate, -inflate);
  228.         DrawFocusRect(lpdi->hDC, &rect);
  229.         }
  230.         break;
  231.     }
  232. }
  233.  
  234.  
  235. //---------------------------------------------------------------------------
  236. // Register custom control. This routine is called by VB when the custom
  237. // control DLL is loaded for use.
  238. //---------------------------------------------------------------------------
  239. BOOL FAR PASCAL _export VBINITCC
  240. (
  241.     USHORT usVersion,
  242.     BOOL   fRuntime
  243. )
  244. {
  245.     // Avoid warnings on unused (but required) formal parameters
  246.     fRuntime = fRuntime;
  247.  
  248.     // Register control(s)
  249.     if (usVersion < VB_VERSION)
  250.     return VBRegisterModel(hmodDLL, &modelPush_Vb1);
  251.     else
  252.     return VBRegisterModel(hmodDLL, &modelPush);
  253. }
  254.  
  255.  
  256. //---------------------------------------------------------------------------
  257. // Provide custom control model information to host environment.
  258. //---------------------------------------------------------------------------
  259. LPMODELINFO FAR PASCAL _export VBGetModelInfo
  260. (
  261.     USHORT usVersion
  262. )
  263. {
  264.     if (usVersion < VB_VERSION)
  265.     return &modelinfoPush_Vb1;
  266.     else
  267.     return &modelinfoPush;
  268. }
  269.  
  270.  
  271. //---------------------------------------------------------------------------
  272. // Initialize library. This routine is called when the first client loads
  273. // the DLL.
  274. //---------------------------------------------------------------------------
  275. int FAR PASCAL LibMain
  276. (
  277.     HANDLE hModule,
  278.     WORD   wDataSeg,
  279.     WORD   cbHeapSize,
  280.     LPSTR  lpszCmdLine
  281. )
  282. {
  283.     // Avoid warnings on unused (but required) formal parameters
  284.     wDataSeg = wDataSeg;
  285.     cbHeapSize = cbHeapSize;
  286.     lpszCmdLine = lpszCmdLine;
  287.  
  288.     hmodDLL = hModule;
  289.  
  290.     return 1;
  291. }
  292.  
  293.  
  294. //---------------------------------------------------------------------------
  295. // WEP
  296. //---------------------------------------------------------------------------
  297. // C7 and QCWIN provide default a WEP:
  298. //---------------------------------------------------------------------------
  299. #if (_MSC_VER < 610)
  300.  
  301. int FAR PASCAL WEP(int fSystemExit);
  302.  
  303. //---------------------------------------------------------------------------
  304. // For Windows 3.0 it is recommended that the WEP function reside in a
  305. // FIXED code segment and be exported as RESIDENTNAME.  This is
  306. // accomplished using the alloc_text pragma below and the related EXPORTS
  307. // and SEGMENTS directives in the .DEF file.
  308. //
  309. // Read the comments section documenting the WEP function in the Windows
  310. // 3.1 SDK "Programmers Reference, Volume 2: Functions" before placing
  311. // any additional code in the WEP routine for a Windows 3.0 DLL.
  312. //---------------------------------------------------------------------------
  313. #pragma alloc_text(WEP_TEXT,WEP)
  314.  
  315. //---------------------------------------------------------------------------
  316. // Performs cleanup tasks when the DLL is unloaded.  WEP() is
  317. // called automatically by Windows when the DLL is unloaded (no
  318. // remaining tasks still have the DLL loaded).    It is strongly
  319. // recommended that a DLL have a WEP() function, even if it does
  320. // nothing but returns success (1), as in this example.
  321. //---------------------------------------------------------------------------
  322. int FAR PASCAL WEP
  323. (
  324.     int fSystemExit
  325. )
  326. {
  327.     // Avoid warnings on unused (but required) formal parameters
  328.     fSystemExit = fSystemExit;
  329.  
  330.     return 1;
  331. }
  332. #endif // C6
  333.  
  334. //---------------------------------------------------------------------------
  335.