Click here to Skip to main content
11,705,052 members (53,371 online)
Click here to Skip to main content

A Slider with 2 Buttons

, 9 Aug 2006 CPOL 42.4K 1.9K 33
Rate this:
Please Sign up or sign in to vote.
A slider with 2 buttons


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
    • 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).

    • 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.

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

      Get range and tick values of the slider

    • GetPosition(int&iL,int&iR) 
      SetPosition(int iL,int iR)

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

    • 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. 


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. 


  • 9th August, 2006: Initial post


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


About the Author

Web Developer
Australia Australia

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

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 9 Aug 2006
Article Copyright 2006 by includeh10
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid