home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / FERRAMEN / VB_SLIDE / SLIDER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-13  |  7.5 KB  |  333 lines

  1. //---------------------------------------------------------------------------
  2. // Slider.c
  3. //---------------------------------------------------------------------------
  4. // Slider Control
  5. //---------------------------------------------------------------------------
  6.  
  7. #define NOCOMM
  8.  
  9. #include <windows.h>
  10.  
  11. #include "vbapi.h"
  12. #include "slider.h"
  13.  
  14. #define EBER_NONE 0
  15.  
  16. SHORT    sButtonWidth = 0;
  17. SHORT    sButtonHeight = 0;
  18. SHORT    sSlideWidth = 0;
  19. SHORT    sSlideHeight = 0;
  20.  
  21. HBITMAP hbmpSlide = 0;
  22. HBITMAP hbmpButton = 0;
  23. HBITMAP hbmpMask = 0;
  24.  
  25.  
  26. typedef union tagPARAMS // event procedure parameter profiles
  27. {
  28.     struct {
  29.     LPVOID    Index;    // reserve space for index parameter to array ctl
  30.     } Void;        // NO PARAMS: Change, GotFocus, LostFocus events
  31. }
  32. PARAMS;
  33.  
  34. void    PaintSlider(HCTL hctl, HWND hwnd, HDC hdc);
  35. SHORT    GetButtonPos(HCTL hctl);
  36. void    SetButtonPos(HCTL hctl, SHORT sPos);
  37. void    MoveButton(HCTL hctl, HWND hwnd, SHORT sPos);
  38.  
  39. //---------------------------------------------------------------------------
  40. // Slider control function
  41. //---------------------------------------------------------------------------
  42. LONG _export SliderCtlProc
  43. (
  44.     HCTL    hctl,
  45.     HWND        hwnd,
  46.     USHORT      msg,
  47.     USHORT      wp,
  48.     LONG        lp
  49. )
  50. {
  51.     LONG        lResult;
  52.  
  53.     // Message pre-processing
  54.     switch( msg )
  55.     {
  56.     case WM_PAINT:
  57.         if (wp)
  58.         PaintSlider(hctl, hwnd, (HDC)wp);
  59.         else {
  60.         PAINTSTRUCT ps;
  61.  
  62.         BeginPaint(hwnd, &ps);
  63.         PaintSlider(hctl, hwnd, ps.hdc);
  64.         EndPaint(hwnd, &ps);
  65.         }
  66.         break;
  67.  
  68.     case WM_SIZE:
  69.         {
  70.         PSLIDER pSlider;
  71.  
  72.         pSlider = SLIDERDEREF(hctl);
  73.  
  74.         pSlider->sHeight = ((HIWORD(lp) - sButtonHeight) / sSlideHeight)
  75.                 * sSlideHeight + sButtonHeight;
  76.         }
  77.         break;
  78.  
  79.     case WM_LBUTTONDOWN:
  80.         {
  81.         PSLIDER pSlider;
  82.  
  83.         pSlider = SLIDERDEREF(hctl);
  84.  
  85.         if (PtInRect(&pSlider->rectButton, MAKEPOINT(lp))) {
  86.         pSlider->fCapture = TRUE;
  87.         pSlider->sYoff = HIWORD(lp) - pSlider->rectButton.top;
  88.         SetCapture(hwnd);
  89.         }
  90.         }
  91.         break;
  92.  
  93.     case WM_LBUTTONUP:
  94.     case WM_MOUSEMOVE:
  95.         {
  96.         PSLIDER pSlider;
  97.  
  98.         pSlider = SLIDERDEREF(hctl);
  99.  
  100.         if (pSlider->fCapture) {
  101.         MoveButton(hctl, hwnd, HIWORD(lp) - pSlider->sYoff);
  102.         if (msg == WM_LBUTTONUP) {
  103.             ReleaseCapture();
  104.             pSlider->fCapture = FALSE;
  105.             }
  106.         }
  107.         }
  108.         break;
  109.  
  110.     case WM_NCCREATE:
  111.         {
  112.         PSLIDER pSlider;
  113.  
  114.         pSlider = SLIDERDEREF(hctl);
  115.  
  116.         pSlider->rectButton.top    = 0;
  117.         pSlider->rectButton.bottom = sButtonHeight;
  118.         pSlider->rectButton.left   = (sSlideWidth - sButtonWidth) / 2;
  119.         pSlider->rectButton.right  = pSlider->rectButton.left + sButtonWidth;
  120.         }
  121.         break;
  122.  
  123.     case VBM_GETPROPERTY:
  124.         switch ( wp ) {
  125.         case IPROP_SLIDER_VALUE:
  126.             (*(SHORT FAR *)lp) = GetButtonPos(hctl);
  127.             return EBER_NONE;
  128.         }
  129.         break;
  130.  
  131.     case VBM_SETPROPERTY:
  132.         switch ( wp ) {
  133.         case IPROP_SLIDER_VALUE:
  134.             MoveButton(hctl, hwnd, (SHORT)lp);
  135.             return EBER_NONE;
  136.         }
  137.         break;
  138.  
  139.     case WM_KEYDOWN:
  140.         switch (wp) {
  141.         case VK_UP:
  142.             MoveButton(hctl, hwnd, GetButtonPos(hctl)-1);
  143.             break;
  144.  
  145.         case VK_DOWN:
  146.             MoveButton(hctl, hwnd, GetButtonPos(hctl)+1);
  147.             break;
  148.  
  149.         case VK_PRIOR:
  150.             MoveButton(hctl, hwnd, GetButtonPos(hctl)-40);
  151.             break;
  152.  
  153.         case VK_NEXT:
  154.             MoveButton(hctl, hwnd, GetButtonPos(hctl)+40);
  155.             break;
  156.         }
  157.         break;
  158.  
  159.     case VBM_CREATED:
  160.         MoveButton(hctl, hwnd, 0);
  161.         break;
  162.     }
  163.  
  164.     // Default processing:
  165.     lResult = VBDefControlProc( hctl, hwnd, msg, wp, lp );
  166.  
  167.     // Message post-processing:
  168.     switch( msg )
  169.     {
  170.  
  171.     // none!
  172.  
  173.     }
  174.  
  175.     return lResult;
  176. }
  177.  
  178. //---------------------------------------------------------------------------
  179.  
  180. void    SetButtonPos
  181. (
  182.     HCTL    hctl,
  183.     SHORT   sPos
  184. )
  185. {
  186.     PSLIDER pSlider;
  187.     PARAMS  params;
  188.  
  189.     pSlider = SLIDERDEREF(hctl);
  190.     if (sPos < 0)
  191.     sPos = 0;
  192.     if (sPos + sButtonHeight >= pSlider->sHeight)
  193.     sPos = pSlider->sHeight - sButtonHeight;
  194.     if (pSlider->rectButton.top != sPos) {
  195.     pSlider->rectButton.top    = sPos;
  196.     pSlider->rectButton.bottom = sPos + sButtonHeight;
  197.     pSlider->rectButton.left   = (sSlideWidth - sButtonWidth) / 2;
  198.     pSlider->rectButton.right  = pSlider->rectButton.left + sButtonWidth;
  199.     VBFireEvent(hctl, EVENT_SLIDER_CHANGE, ¶ms);
  200.     }
  201. }
  202.  
  203. SHORT    GetButtonPos
  204. (
  205.     HCTL    hctl
  206. )
  207. {
  208.     return SLIDERDEREF(hctl)->rectButton.top;
  209. }
  210.  
  211. void    PaintSlider
  212. (
  213.     HCTL    hctl,
  214.     HWND    hwnd,
  215.     HDC     hdc
  216. )
  217. {
  218.     HDC     hMemDC;
  219.     RECT    rectSlide;
  220.     PSLIDER pSlider;
  221.     HBITMAP hbmpSave;
  222.     HBRUSH  hbrSlide;
  223.     HBRUSH  hbrSave;
  224.  
  225.     pSlider = SLIDERDEREF(hctl);
  226.  
  227.     hMemDC = CreateCompatibleDC(hdc);
  228.     hbmpSave = SelectObject(hMemDC, hbmpSlide);
  229.  
  230.     rectSlide.top    = sButtonHeight / 2;
  231.     rectSlide.left   = 0;
  232.     rectSlide.bottom = rectSlide.top + sSlideHeight;
  233.     rectSlide.right  = sSlideWidth;
  234.  
  235.     SendMessage(GetParent(hwnd), WM_CTLCOLOR, hdc, MAKELONG(hwnd, 0));
  236.     hbrSlide = CreateSolidBrush(GetTextColor(hdc));
  237.     if (hbrSlide)
  238.     hbrSave = SelectObject(hdc, hbrSlide);
  239.     while (rectSlide.top <= pSlider->sHeight) {
  240.     if (RectVisible(hdc, &rectSlide))
  241.         BitBlt(hdc,
  242.            rectSlide.left,
  243.            rectSlide.top,
  244.            sSlideWidth,
  245.            (pSlider->sHeight >= rectSlide.bottom) ? sSlideHeight : 1,
  246.            hMemDC,
  247.            0, 0,
  248.            0x00B8074AL);    // PSDPxax
  249.     OffsetRect(&rectSlide, 0, sSlideHeight);
  250.     }
  251.     if (hbrSlide)
  252.     SelectObject(hdc, hbrSave);
  253.     DeleteObject(hbrSlide);
  254.  
  255.     if (RectVisible(hdc, &pSlider->rectButton)) {
  256.     SelectObject(hMemDC, hbmpMask);
  257.     BitBlt(hdc,
  258.            pSlider->rectButton.left,
  259.            pSlider->rectButton.top,
  260.            sButtonWidth,
  261.            sButtonHeight,
  262.            hMemDC,
  263.            0, 0,
  264.            SRCAND);
  265.     SelectObject(hMemDC, hbmpButton);
  266.     BitBlt(hdc,
  267.            pSlider->rectButton.left,
  268.            pSlider->rectButton.top,
  269.            sButtonWidth,
  270.            sButtonHeight,
  271.            hMemDC,
  272.            0, 0,
  273.            SRCPAINT);
  274.     }
  275.  
  276.     // ExcludeClipRect(hdc, 0, 0, sSlideWidth, sSlideHeight);
  277.  
  278.     rectSlide.top    = 0;
  279.     rectSlide.right  = 9999;
  280.  
  281.     // FillRect(hdc, &rectSlide, COLOR_WINDOW+1);
  282.     SelectObject(hMemDC, hbmpSave);
  283.     DeleteDC(hMemDC);
  284. }
  285.  
  286. //--------------------------------------------------------------------------
  287. // MoveButton
  288. //--------------------------------------------------------------------------
  289. void MoveButton
  290. (
  291.     HCTL    hctl,
  292.     HWND    hwnd,
  293.     SHORT   sPos
  294. )
  295. {
  296.     RECT    rectOld;
  297.     RECT    rectUnion;
  298.     PSLIDER pSlider;
  299.     HDC     hdcMove;
  300.     HDC     hdcMain;
  301.     HBITMAP hbmpMove;
  302.     HBITMAP hbmpSave;
  303.  
  304.     pSlider = SLIDERDEREF(hctl);
  305.  
  306.     rectOld = pSlider->rectButton;
  307.     SetButtonPos(hctl, sPos);
  308.     UnionRect(&rectUnion, &rectOld, &pSlider->rectButton);
  309.     hdcMain = GetDC(hwnd);
  310.     hdcMove = CreateCompatibleDC(hdcMain);
  311.     hbmpMove = CreateCompatibleBitmap(hdcMain,
  312.         rectUnion.right -  rectUnion.left,
  313.         rectUnion.bottom - rectUnion.top);
  314.     hbmpSave = SelectObject(hdcMove, hbmpMove);
  315.     SetWindowOrg(hdcMove, rectUnion.left, rectUnion.top);
  316.     VBSendControlMsg(hctl, WM_ERASEBKGND, hdcMove, 0L);
  317.     PaintSlider(hctl, hwnd, hdcMove);
  318.     SetWindowOrg(hdcMove, 0, 0);
  319.     BitBlt(hdcMain,
  320.         rectUnion.left,
  321.         rectUnion.top,
  322.         rectUnion.right -  rectUnion.left,
  323.         rectUnion.bottom - rectUnion.top,
  324.         hdcMove,
  325.         0,
  326.         0,
  327.         SRCCOPY);
  328.     SelectObject(hdcMove, hbmpSave);
  329.     DeleteObject(hbmpMove);
  330.     DeleteDC(hdcMove);
  331.     ReleaseDC(hwnd, hdcMain);
  332. }
  333.