65.9K
CodeProject is changing. Read more.
Home

Improved mouse click handling for the slider control

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.55/5 (21 votes)

Dec 27, 2002

2 min read

viewsIcon

118115

downloadIcon

26393

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!