home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l410 / 1.ddi / CDK / PIX / PIX.C$ / PIX.bin
Encoding:
Text File  |  1992-02-13  |  5.5 KB  |  192 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. // Pix.c
  10. //---------------------------------------------------------------------------
  11. // Contains control procedure for PIX control
  12. //---------------------------------------------------------------------------
  13.  
  14. #define  NOCOMM
  15. #include <windows.h>
  16.  
  17. #include <vbapi.h>
  18. #include "pix.h"
  19.  
  20.  
  21. //---------------------------------------------------------------------------
  22. // Standard Error Values
  23. //---------------------------------------------------------------------------
  24. #define ERR_None        0
  25. #define ERR_BadIndex          381   // Error$(381) = "Invalid property array index"
  26. #define ERR_BadPixFmt        32000   // User-defined error
  27.  
  28.  
  29. //---------------------------------------------------------------------------
  30. // Local Prototypes
  31. //---------------------------------------------------------------------------
  32. VOID NEAR PaintPix(PPIX ppix, HWND hwnd, HDC hdc);
  33.  
  34.  
  35. //---------------------------------------------------------------------------
  36. // Pix Control Procedure
  37. //---------------------------------------------------------------------------
  38. LONG FAR PASCAL _export PixCtlProc
  39. (
  40.     HCTL   hctl,
  41.     HWND   hwnd,
  42.     USHORT msg,
  43.     USHORT wp,
  44.     LONG   lp
  45. )
  46. {
  47.     PPIX ppix = NULL;
  48.  
  49.     switch (msg)
  50.     {
  51.         case WM_PAINT:
  52.         ppix = (PPIX)VBDerefControl(hctl);
  53.             if (wp)
  54.         PaintPix(ppix, hwnd, (HDC)wp);
  55.         else
  56.         {
  57.                 PAINTSTRUCT ps;
  58.  
  59.         BeginPaint(hwnd, &ps);
  60.         PaintPix(ppix, hwnd, ps.hdc);
  61.         EndPaint(hwnd, &ps);
  62.         }
  63.             break;
  64.  
  65.         case VBM_GETPROPERTY:
  66.         switch (wp)
  67.         {
  68.         case IPROP_PIX_LIST:    // Get element of List prop array
  69.                     {
  70.                     LONG          i;
  71.             LPDATASTRUCT  lpDs = (LPDATASTRUCT)lp;
  72.                     LPSTR         lpstr;
  73.  
  74.                     i = lpDs->index[0].data;
  75.                     if (i < 0 || i >= ARRMAX)
  76.             return ERR_BadIndex;
  77.  
  78.             ppix = (PPIX)VBDerefControl(hctl);
  79.             if (ppix->List[i] == NULL)
  80.             {
  81.             lpDs->data = (LONG)VBCreateHsz((_segment)hctl,"");
  82.             // *** ppix may now be invalid due to call to VB API ***
  83.             return ERR_None;
  84.                         }
  85.                     lpstr = VBDerefHsz(ppix->List[i]);
  86.             lpDs->data = (LONG)VBCreateHsz((_segment)hctl, lpstr);
  87.             // *** ppix may now be invalid due to call to VB API ***
  88.             return ERR_None;
  89.             }
  90.                 }
  91.             break;
  92.  
  93.         case VBM_SETPROPERTY:
  94.         switch (wp)
  95.         {
  96.         case IPROP_PIX_LIST:    // Set element of List prop array
  97.                     {
  98.                     LONG          i;
  99.             LPDATASTRUCT  lpDs =(LPDATASTRUCT)lp;
  100.                     HSZ           hsz;
  101.  
  102.                     i = lpDs->index[0].data;
  103.                     if (i < 0 || i >= ARRMAX)
  104.             return ERR_BadIndex;
  105.  
  106.             ppix = (PPIX)VBDerefControl(hctl);
  107.                     if (ppix->List[i])
  108.             VBDestroyHsz(ppix->List[i]);
  109.             hsz = VBCreateHsz((_segment)hctl, (LPSTR)(lpDs->data));
  110.             // *** ppix may now be invalid due to call to VB API ***
  111.             ppix = (PPIX)VBDerefControl(hctl);
  112.                     ppix->List[i] = hsz;
  113.             return ERR_None;
  114.                     }
  115.                 }
  116.             break;
  117.  
  118.         case VBM_CHECKPROPERTY:
  119.         switch (wp)
  120.         {
  121.                 case IPROP_PIX_PICT:
  122.                     {
  123.                     PIC     pic;
  124.  
  125.             VBGetPic((HPIC)lp, &pic);
  126.             switch (pic.picType)
  127.             {
  128.                         case PICTYPE_BITMAP:
  129.                         case PICTYPE_NONE:
  130.                 InvalidateRect(hwnd, NULL, TRUE);
  131.                 return ERR_None;
  132.                         }
  133.             return VBSetErrorMessage(ERR_BadPixFmt,
  134.                          "Picture format not supported.");
  135.                     }
  136.                 }
  137.             break;
  138.  
  139.         case WM_DESTROY:
  140.         ppix = (PPIX)VBDerefControl(hctl);
  141.         VBFreePic(ppix->hpicPict);
  142.             break;
  143.     }
  144.  
  145.     return VBDefControlProc(hctl, hwnd, msg, wp, lp);
  146. }
  147.  
  148.  
  149.  
  150. //---------------------------------------------------------------------------
  151. // Paint the bitmap into the Pix control.
  152. //---------------------------------------------------------------------------
  153. VOID NEAR PaintPix
  154. (
  155.     PPIX ppix,
  156.     HWND hwnd,
  157.     HDC  hdc
  158. )
  159. {
  160.     PIC    pic;
  161.     HPIC   hpic = ppix->hpicPict;
  162.     BITMAP bmp;
  163.     HDC    hdcMem;
  164.     RECT   rect;
  165.  
  166.     VBGetPic(hpic, &pic);
  167.     switch (pic.picType)
  168.     {
  169.         case PICTYPE_BITMAP:
  170.         GetObject(pic.picData.bmp.hbitmap, sizeof(BITMAP), (LPSTR)&bmp);
  171.         hdcMem = CreateCompatibleDC(hdc);
  172.         SelectObject(hdcMem, pic.picData.bmp.hbitmap);
  173.         GetClientRect(hwnd, &rect);
  174.         StretchBlt(hdc, 0, 0,
  175.                rect.right, rect.bottom, hdcMem, 0, 0,
  176.                bmp.bmWidth, bmp.bmHeight, SRCCOPY);
  177.         DeleteDC(hdcMem);
  178.             break;
  179.  
  180.         case PICTYPE_NONE:
  181.             {
  182.             HBRUSH  hbr;
  183.  
  184.         hbr = (HBRUSH)SendMessage(GetParent(hwnd), WM_CTLCOLOR,
  185.                                         hdc, MAKELONG(hwnd, 0));
  186.         GetClipBox(hdc, &rect);
  187.         FillRect(hdc, &rect, hbr);
  188.             break;
  189.             }
  190.     }
  191. }
  192.