Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

A Slider with 2 Buttons

3.62/5 (21 votes)
9 Aug 2006CPOL2 min read 2   3K  
A slider with 2 buttons

Introduction

My application needs to decrease 4 edges (left, top, right, bottom) of a rectangle object, maximum decreasing amount for all edges is 100%.

The following picture shows 3 cases allowed for decreasing top edge.

It is not easy to implement the task for user except that I offer a suitable control, so I created a slider with 2 buttons, which can move inward but cannot be overlapped or cross each other.

The following interface is a screen shot of the sample program which contains 2 horizontal and 2 vertical sliders.

Files, Classes, Functions and Message

  1. Files and Classes

    The directory "lib" inside the sample program contains 4 files: SliderBoth and SliderBothBtn (H and CPP).
    Each file has a class with the same name as the file.
    The 2 classes are inherited from CWnd - so the slider belongs to MFC issues.

    SliderBoth class is the main control, SliderBothBtn is the button class of SliderBoth.
    You only need to include SliderBoth in the file in which you use the slider.

  2. Functions of class SliderBoth
    • C++
      BOOL Create(CWnd*pMum,CRect rcBd,BOOL bHorz,int iID)

      bHorz: if it is a horizontal or vertical slider
      iID: control ID of the slider, which must be unique for parent window (similar to button or menu ID)
      rcBd: rectangle of the slider, but its width or height is fixed without being affected by the rectangle (see function MoveWindow(...) below).

    • C++
      BOOL SetRange(int iMin,int iMax,int iTick)

      Minimum, maximum and tick values of the slider, which must be:
      iMax>iMin, iTick>0 and (iMax-iMin)%2==0, (iMax-iMin)% iTick==0Because the slider draws a longer tick sign in the middle for my program.
      You can remove the last 2 restrictions easily by modifying the source code.

    • C++
      GetRange(int&iMin,int&iMax,int&iTick)

      Get range and tick values of the slider

    • C++
      GetPosition(int&iL,int&iR) 
      //and 
      SetPosition(int iL,int iR)

      Get or Set button positions
      iL means left or bottom button position for horizontal or vertical slider respectively.

    • C++
      void MoveWindow(CRect*pRect,BOOL bRepaint=TRUE)
      void MoveWindow(int iX,int iY,int iW,int iH,BOOL bRepaint=TRUE) 

      The two functions override CWnd related functions, which move height of horizontal slider or width of vertical slider to 27 pixels always.

    Normally you only need to call Create() and SetRange() functions.

  3. Message

    If mouse is released (WM_LBUTTONUP) after moving slider's buttons, a message is posted by PostMessage() from the slider to its parent window in the format:
    message: WM_COMMANDwParam: slider's ID, lParam: zero (not used)
    (The slider uses only this message).

    The parent window calls function GetPosition(...) on the message to obtain button positions of the slider.

    No message is sent:

    If button positions are unchanged after moving - E.g. moved to left then right to previous position.

    Note: It is very easy to modify the source code if you need message while mouse is moving. 

Conclusion

All of my articles, including this one, are purely from my test applications, I don't have any extra time to write a real "article" as many guys do in the place - currently the slider works perfectly in my program, that is all I can do at the stage.

If you find bugs or need more features in your cases, please post a reply, I may modify the slider if I have time some day. 

History

  • 9th August, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)