Custom slider control in MFC (visual studio)

A CSliderCtrl wraps a trackbar control. As such, the former shares the same limitations with the latter. Specifically, the range is set through the TBM_SETRANGE message (or the TBM_SETRANGEMIN and TBM_SETRANGEMAX messages). Either message takes an integral value, so you cannot have the control operate on fractional values.

If you need the integral values supported by the control to represent fractional values, you will have to perform the mapping in client code (scaling and translation). Possible mappings are:

  • Set the range from 0 * 4 to (100 - 14) * 4 (i.e. 0 to 344). The control position x represents the value 14 + x / 4.
  • Set the range from 14 * 4 to 100 * 4 (i.e. 56 to 400). The control position x then represents the value x / 4.

In general, fractional values cannot accurately be represented when using floating point values. In this case, however, there is no loss in accuracy; any integer value divided by a power-of-two (such as 4) can be accurately represented by a floating point value (so long as the result is still in range).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top