Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / MFC
Article

Improved mouse click handling for the slider control

Rate me:
Please Sign up or sign in to vote.
4.55/5 (22 votes)
26 Dec 20022 min read 116.1K   26.4K   23   21
Class that provides better mouse click handling for the standard windows slider control

The problem

I don't know if anyone has ever noticed how difficult it can be to select a value using the slider control. For instance this control:

Slider

When you try to change the setting by clicking at the 20 kHz tick mark, sometimes it doesn't move at all and if it does move, it'll first only move to the 50 kHz setting. Usually what happens is that the user will clicking a few time times and then eventually give up and just drag the thumb down.

This strange behaviour is actually caused by two separate bugs in the slider control.

Firstly, the hit region does not extend below the last tick mark:

Slider

So clicking just below the last tick mark results in the slider not moving at all

Secondly, the slider control apparently has been subclassed from the scrollbar control, and has inherited the scrollbar's mouse click behaviour. This means if you click on the column area it causes the slider to move down one page (which in the case of this example is only one position). While this behaviour is correct for the scrollbar control it is entirely inappropriate for the slider control. When I click on the 20 kHz value I expect it to move directly to the 20 kHz position, not to 50 kHz:

Slider

Solution

I have made a subclassed slider control to rectify these problems.

To use the code simply include the files NiceSlider.h & NiceSlider.cpp in your project, then in your dialog, or form class use class wizard to create a member variable for the slider control and replace CSliderCtrl with CNiceSliderCtrl:

#include "NiceSlider.h"

/////////////////////////////////////////////////////////////////////////////
// CImprovedSliderDlg dialog

class CImprovedSliderDlg : public CDialog
{
// Construction
public:
	CImprovedSliderDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CImprovedSliderDlg)
	enum { IDD = IDD_IMPROVEDSLIDER_DIALOG };
	CNiceSliderCtrl	m_sliderImproved;

Notes

A special thanks to Daniel Frey whose round slider control class I used as a starting point for my class.

This problem with the slider control I have noticed in both Windows NT and Win9x. I have not checked if they have yet fixed it in Windows 2000, or XP.

History

  • First version!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Astronautz
Spain Spain
After working in the software industry for many years, I've started my own games company that specialises in strategy games for mobile platforms.

Comments and Discussions

 
Questionexcellent, thank you Pin
Jim Dill28-Jul-20 6:19
Jim Dill28-Jul-20 6:19 
QuestionLicense terms Pin
Scott Bartine7-Dec-11 11:35
Scott Bartine7-Dec-11 11:35 
AnswerRe: License terms Pin
ed welch9-Dec-11 4:21
ed welch9-Dec-11 4:21 
GeneralThanks!!! Pin
ave_want18-Nov-10 21:55
ave_want18-Nov-10 21:55 
GeneralThanks !! Pin
Kevin109230-Dec-09 19:59
Kevin109230-Dec-09 19:59 
GeneralThanks Pin
biocomp2-Apr-08 22:21
biocomp2-Apr-08 22:21 
GeneralThanks Pin
Member 36521604-Feb-08 6:04
Member 36521604-Feb-08 6:04 
GeneralFix : Position Calc [modified] Pin
shijoong22-Aug-06 22:57
shijoong22-Aug-06 22:57 
Generalfix Pin
wheregone19-Mar-06 19:07
wheregone19-Mar-06 19:07 
Questionhow to enable tooltips? Pin
zsabo29-Sep-05 22:00
zsabo29-Sep-05 22:00 
QuestionHow about with WTL? Pin
jgoeke4-Jan-05 10:29
jgoeke4-Jan-05 10:29 
GeneralRemoving Edge Pin
Balkrishna Talele15-Jul-04 19:35
Balkrishna Talele15-Jul-04 19:35 
GeneralJust fine! Pin
Yasuhiko Yoshimura10-Sep-03 23:33
Yasuhiko Yoshimura10-Sep-03 23:33 
Generalbug fix Pin
nasu16-May-03 20:49
nasu16-May-03 20:49 
GeneralRe: bug fix Pin
ed welch19-May-03 22:50
ed welch19-May-03 22:50 
GeneralOoops, I just noticed a bug... Pin
ed welch30-Dec-02 5:05
ed welch30-Dec-02 5:05 
GeneralRe: Ooops, I just noticed a bug... Pin
nasu16-May-03 20:40
nasu16-May-03 20:40 
GeneralRe: Ooops, I just noticed a bug... Pin
ed welch19-May-03 22:39
ed welch19-May-03 22:39 
GeneralRe: Ooops, I just noticed a bug... Pin
nasu20-May-03 7:20
nasu20-May-03 7:20 
GeneralMore on trackbars Pin
Todd C. Wilson27-Dec-02 6:16
Todd C. Wilson27-Dec-02 6:16 
GeneralVery nice description of the problem Pin
Marc Clifton27-Dec-02 5:32
mvaMarc Clifton27-Dec-02 5:32 

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.