home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- // Copyright (C) 1991 Microsoft Corporation
- //
- // You have a royalty-free right to use, modify, reproduce and distribute
- // the Sample Custom Control Files (and/or any modified version) in any way
- // you find useful, provided that you agree that Microsoft has no warranty,
- // obligation or liability for any Custom Control File.
- //---------------------------------------------------------------------------
- // Push.c
- //---------------------------------------------------------------------------
- // Picture Push Button Control
- //---------------------------------------------------------------------------
-
- #define NOCOMM
- #include <windows.h>
-
- #include <vbapi.h>
-
- #define CTL_DATA // To declare static data in push.h
- #include "push.h"
-
-
- //---------------------------------------------------------------------------
- // Standard Error Values
- //---------------------------------------------------------------------------
- #define ERR_None 0
- #define ERR_InvPropVal 380 // Error$(380) = "Invalid property value"
-
-
- //---------------------------------------------------------------------------
- // Local Prototypes
- //---------------------------------------------------------------------------
- VOID NEAR DrawBtn(HCTL hctl, LPDRAWITEMSTRUCT lpdi);
-
-
- //---------------------------------------------------------------------------
- // Event Procedure Parameter Profiles
- //---------------------------------------------------------------------------
- typedef struct tagPARAMS
- {
- HLSTR ClickString;
- LPVOID Index; // Reserve space for index parameter to array ctl
- } PARAMS;
-
-
- //---------------------------------------------------------------------------
- // Push Control Procedure
- //---------------------------------------------------------------------------
- LONG FAR PASCAL _export PushCtlProc
- (
- HCTL hctl,
- HWND hwnd,
- USHORT msg,
- USHORT wp,
- LONG lp
- )
- {
- LONG lResult;
-
- // Message pre-processing
- switch (msg)
- {
- case WM_ERASEBKGND:
- // Don't bother with erasing the background
- return TRUE;
-
- case VBM_MNEMONIC:
- // Act like a click
- lp = MAKELONG(0,BN_CLICKED);
- // **** FALL THROUGH ****
-
- case VBN_COMMAND:
- switch (HIWORD(lp))
- {
- case BN_CLICKED:
- {
- PARAMS params;
- char szStrBuf[20];
- int cbCaption;
- ERR err;
- LPSTR lpstr;
-
- cbCaption = GetWindowText(hwnd, szStrBuf, 20);
- params.ClickString = VBCreateHlstr(szStrBuf, cbCaption);
- err = VBFireEvent(hctl, EVENT_PUSH_CLICK, ¶ms);
- if (!err)
- {
- lpstr = VBDerefHlstr(params.ClickString);
- if (lpstr == NULL)
- {
- szStrBuf[0] = '\0';
- SetWindowText(hwnd, szStrBuf);
- }
- else
- {
- lpstr[VBGetHlstrLen(params.ClickString) - 1] = '\0';
- SetWindowText(hwnd, lpstr);
- }
- }
- VBDestroyHlstr(params.ClickString);
- }
- break;
- }
- return 0L;
-
- case VBM_SETPROPERTY:
- switch (wp)
- {
- case IPROP_PUSH_CAPTION:
- // To avoid a Windows problem, make sure text is
- // under 255 bytes:
- if (lstrlen((LPSTR)lp) > 255)
- *((LPSTR)lp + 255) = 0;
- break;
- }
- break;
-
- case VBM_CHECKPROPERTY:
- switch (wp)
- {
- case IPROP_PUSH_PICTUREUP:
- case IPROP_PUSH_PICTUREDOWN:
- {
- PIC pic;
-
- VBGetPic((HPIC)lp, &pic);
- switch (pic.picType)
- {
- case PICTYPE_NONE:
- case PICTYPE_BITMAP:
- case PICTYPE_ICON:
- InvalidateRect(hwnd, NULL, TRUE);
- return ERR_None;
- }
- return ERR_InvPropVal;
- }
- }
- break;
-
- case VBN_DRAWITEM:
- DrawBtn(hctl, (LPDRAWITEMSTRUCT)lp);
- return 0;
- }
-
- // Default processing:
- lResult = VBDefControlProc(hctl, hwnd, msg, wp, lp);
-
- // Message post-processing:
- switch (msg)
- {
- case WM_DESTROY:
- VBFreePic(PUSHDEREF(hctl)->hpicDown);
- VBFreePic(PUSHDEREF(hctl)->hpicUp);
- break;
- }
-
- return lResult;
- }
-
-
- //---------------------------------------------------------------------------
- // Paint the push button.
- //---------------------------------------------------------------------------
- VOID NEAR DrawBtn
- (
- HCTL hctl,
- LPDRAWITEMSTRUCT lpdi
- )
- {
- HPIC hpic;
- PIC pic;
- PPUSH ppush = PUSHDEREF(hctl);
- BITMAP bmp;
- HDC hdcMem;
- RECT rect;
- SHORT inflate;
-
- switch (lpdi->itemAction)
- {
- case ODA_SELECT:
- case ODA_DRAWENTIRE:
- hpic = (lpdi->itemState & ODS_SELECTED) ? ppush->hpicDown
- : ppush->hpicUp;
- VBGetPic(hpic, &pic);
- switch (pic.picType)
- {
- case PICTYPE_BITMAP:
- GetObject(pic.picData.bmp.hbitmap, sizeof(BITMAP), (LPSTR)&bmp);
- hdcMem = CreateCompatibleDC(lpdi->hDC);
- SelectObject(hdcMem, pic.picData.bmp.hbitmap);
- StretchBlt(lpdi->hDC, 0, 0,
- lpdi->rcItem.right - lpdi->rcItem.left + 1,
- lpdi->rcItem.bottom - lpdi->rcItem.top + 1,
- hdcMem, 0, 0, bmp.bmWidth, bmp.bmHeight,
- SRCCOPY);
- DeleteDC(hdcMem);
- break;
-
- case PICTYPE_ICON:
- case PICTYPE_NONE:
- {
- HBRUSH hbr;
- RECT rect;
-
- hbr = (HBRUSH)SendMessage(GetParent(lpdi->hwndItem),
- WM_CTLCOLOR, lpdi->hDC, MAKELONG(lpdi->hwndItem, 0));
- GetClipBox(lpdi->hDC, &rect);
- FillRect(lpdi->hDC, &rect, hbr);
- if (pic.picType == PICTYPE_ICON)
- DrawIcon(lpdi->hDC, 0, 0, pic.picData.wmf.hmeta);
- else if (lpdi->itemState & ODS_SELECTED)
- InvertRect(lpdi->hDC, &rect);
- }
- break;
- }
- // **** FALL THROUGH ****
-
- case ODA_FOCUS:
- if (lpdi->itemState & ODS_FOCUS)
- {
- CopyRect((LPRECT)&rect, (LPRECT)&lpdi->rcItem);
- inflate = min(3,min(rect.right - rect.left + 1,
- rect.bottom - rect.top + 1) / 5);
- InflateRect(&rect, -inflate, -inflate);
- DrawFocusRect(lpdi->hDC, &rect);
- }
- break;
- }
- }
-
- //---------------------------------------------------------------------------
-