Click here to Skip to main content
Click here to Skip to main content
Go to top

A Slider with 2 Buttons

, 9 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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
    • 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) 
      //and 
      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. 

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)

Share

About the Author

includeh10
Web Developer
Australia Australia

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



Comments and Discussions

 
GeneralThank you very much Pinmemberhaithink12-Dec-13 14:39 
GeneralMy vote of 4 Pinmemberlllxy19-May-13 1:17 
GeneralMy vote of 4 Pinmemberevan3699-Feb-11 15:20 
QuestionHow about setting when mouse dragging? PinmemberSuper Garrison9-May-07 3:49 
AnswerRe: How about setting when mouse dragging? PinmemberDivya Rathore6-Dec-07 23:27 
GeneralRe: How about setting when mouse dragging? Pinmembermensfort6-May-10 22:42 
GeneralRe: How about setting when mouse dragging? PinmemberDarryl Bryk21-Jun-10 10:09 
GeneralRe: How about setting when mouse dragging? PinmemberDarryl Bryk21-Jun-10 10:12 
GeneralIt is cool PinmemberShangshu8-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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 9 Aug 2006
Article Copyright 2006 by includeh10
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid