Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++

A Slider with 2 Buttons

Rate me:
Please Sign up or sign in to vote.
3.62/5 (21 votes)
9 Aug 2006CPOL2 min read 71.5K   3K   38   9
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)


Written By
Web Developer
Australia Australia

Please visit our Download Home to obtain many interesting software for free ...


Comments and Discussions

 
GeneralThank you very much Pin
haithink12-Dec-13 14:39
haithink12-Dec-13 14:39 
GeneralMy vote of 4 Pin
lllxy19-May-13 1:17
lllxy19-May-13 1:17 
GeneralMy vote of 4 Pin
evan3699-Feb-11 15:20
evan3699-Feb-11 15:20 
I like it.
QuestionHow about setting when mouse dragging? Pin
Super Garrison9-May-07 3:49
Super Garrison9-May-07 3:49 
AnswerRe: How about setting when mouse dragging? Pin
Divya Rathore6-Dec-07 23:27
Divya Rathore6-Dec-07 23:27 
GeneralRe: How about setting when mouse dragging? Pin
mensfort6-May-10 22:42
mensfort6-May-10 22:42 
GeneralRe: How about setting when mouse dragging? Pin
Darryl Bryk21-Jun-10 10:09
Darryl Bryk21-Jun-10 10:09 
GeneralRe: How about setting when mouse dragging? Pin
Darryl Bryk21-Jun-10 10:12
Darryl Bryk21-Jun-10 10:12 
GeneralIt is cool Pin
Shangshu8-Mar-07 6:47
Shangshu8-Mar-07 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.