Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

CKnobControlST v1.0

Rate me:
Please Sign up or sign in to vote.
4.89/5 (31 votes)
24 Jun 20033 min read 142.7K   7K   96   30
davide_calabro@yahoo.com

Sample Image

SoftechSoftware homepage
SoftechSoftware Email

Abstract

You need GDI+ installed to use this control.

CKnobControlST is a class derived from MFC CSliderCtrl class. The control has been created as an exercise of converting an existing control written in C# into a new one with the same features written in C++. The conversion was quite simple. You can find the original control here. CKnobControlST code is more robust than the original one.

How to integrate CKnobControlST in your application

In your project include the following files:

  • KnobControlST.h
  • KnobControlST.cpp
With dialog editor create a slider control called, for example, IDC_KNOB.

Then create a member variable for this control:
CKnobControlST m_knbControl;

Now attach the knob control to CKnobControlST. For dialog-based applications, in your OnInitDialog:

// Call the base-class method
CDialog::OnInitDialog();

// Create the IDC_KNOB slider control
m_knbControl.SubclassDlgItem(IDC_KNOB, this);

Or in your DoDataExchange:

// Call the base method
CDialog::DoDataExchange(pDX);

// Create the IDC_KNOB slider control
DDX_Control(pDX, IDC_KNOB, m_knbControl);

The control will have the same default range and start position as in the base MFC class CSliderCtrl. These values can be modified using the same methods of the base class such as, for example, SetRange or SetPos. Following is the list of base class methods currently supported:

  • GetRangeMax
  • GetRangeMin
  • GetRange
  • SetRangeMax
  • SetRangeMin
  • SetRange
  • GetPos
  • SetPos
At this moment other specific CSliderCtrl features are not supported.

Your slider control is now a CKnobControlST!

Class methods

SetIcon

Sets the icon to be displayed. Any previous icon will be removed.

// Parameters:
//     [IN]   nIcon
//            A Windows icon resource ID
//     [IN]   bRepaint
//            If TRUE the control will be immediately repainted
//     [IN]   hInstance
//            Handle of the instance that contains the icon.
//            If NULL the icon will be loaded from the .EXE resources
//
// Return value:
//     KNOBCONTROLST_OK
//        Function executed successfully.
//     KNOBCONTROLST_INVALIDRESOURCE
//        The resource specified cannot be found or loaded.
//
DWORD SetIcon(int nIcon, BOOL bRepaint = TRUE, HINSTANCE hInstance = NULL)

SetIcon

Sets the icon to be displayed. Any previous icon will be removed.

// Parameters:
//     [IN]   hIcon
//            Handle fo the icon to show.
//            Pass NULL to remove any icon from the control.
//     [IN]   bRepaint
//            If TRUE the control will be immediately repainted
//
// Return value:
//     KNOBCONTROLST_OK
//        Function executed successfully.
//     KNOBCONTROLST_INVALIDRESOURCE
//        The resource specified cannot be found or loaded.
//
DWORD SetIcon(HICON hIcon, BOOL bRepaint = TRUE)

SetColors

Sets the foreground and background colors of the control.
// Parameters:
//     [IN]   cFgColor
//            A GDI+ Color object indicating the color of the knob control.
//     [IN]   cBkColor
//            A GDI+ Color object indicating the background color of the <BR>//            knob control.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetColors(Color cFgColor, Color cBkColor, BOOL bRepaint = TRUE)

SetFgColor

Sets the foreground color of the control.
// Parameters:
//     [IN]   cFgColor
//            A GDI+ Color object indicating the color of the knob control.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetFgColor(Color cFgColor, BOOL bRepaint = TRUE)

SetBkColor

Sets the background color of the control.
// Parameters:
//     [IN]   cBkColor
//            A GDI+ Color object indicating the background color of the knob <BR>//            control.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetBkColor(Color cBkColor, BOOL bRepaint = TRUE)

SetScaleColor

Sets the scale color of the control.
// Parameters:
//     [IN]   cColor
//            A GDI+ Color object indicating the scale color of the knob <BR>//            control.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetScaleColor(Color cColor, BOOL bRepaint = TRUE)

SetAlpha

Sets the alpha component of the colors used to draw the control.
// Parameters:
//     [IN]   byAlpha
//            A BYTE value indicating the alpha component.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetAlpha(BYTE byAlpha, BOOL bRepaint = TRUE)

GetAlpha

Returns the alpha component of the colors used to draw the control.
// Return value:
//     A BYTE indicating the alpha component.
//
BYTE GetAlpha()

ShowLargeScale

Sets if the large scale around the control must be drawn.
// Parameters:
//     [IN]   bShow
//            If TRUE the scale will be drawn.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void ShowLargeScale(BOOL bShow, BOOL bRepaint = TRUE)

ShowSmallScale

Sets if the small scale around the control must be drawn.
// Parameters:
//     [IN]   bShow
//            If TRUE the scale will be drawn.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void ShowSmallScale(BOOL bShow, BOOL bRepaint = TRUE)

SetLargeChangeValue

Sets the value of the large control step.
// Parameters:
//     [IN]   nLargeChange
//            New value of the large control step.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetLargeChangeValue(int nLargeChange, BOOL bRepaint = TRUE)

SetSmallChangeValue

Sets the value of the small control step.
// Parameters:
//     [IN]   nSmallChange
//            New value of the large control step.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetSmallChangeValue(int nSmallChange, BOOL bRepaint = TRUE)

SetTooltipText

Sets the text to show in the control tooltip.
// Parameters:
//     [IN]   nId
//            ID number of the string resource containing the text to show.
//     [IN]   bActivate
//            If TRUE the tooltip will be created active.
//
void SetTooltipText(int nId, BOOL bActivate = TRUE)

SetTooltipText

Sets the text to show in the control tooltip.
// Parameters:
//     [IN]   lpszText
//            Pointer to a null-terminated string containing the text to show.
//     [IN]   bActivate
//            If TRUE the tooltip will be created active.
//
void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE)

ActivateTooltip

Enables or disables the control tooltip.
// Parameters:
//     [IN]   bActivate
//            If TRUE the tooltip will be activated.
//
void ActivateTooltip(BOOL bActivate = TRUE)

SetChangeCallback

Sets the callback message that will be sent to the specified window each time the control reaches a new position.
// Parameters:
//     [IN]   hWnd
//            Handle of the window that will receive the callback message.
//            Pass NULL to remove any previously specified callback message.
//     [IN]   nMessage
//            Callback message to send to window.
//     [IN]   wParam
//            First 32 bits user specified value that will be passed to the <BR>//            callback function.
//     [IN]   lParam
//            Second 32 bits user specified value that will be passed to the <BR>//            callback function.
//
// Remarks:
//      the callback function must be in the form:
//      LRESULT On_ChangeCallback(WPARAM wParam, LPARAM lParam)
//      Where:
//              [IN]    wParam
//                      First 32 bits user specified value.
//              [IN]    lParam
//                      Second 32 bits user specified value.
//
// Return value:
//     KNOBCONTROLST_OK
//        Function executed successfully.
//
DWORD SetChangeCallback(HWND hWnd, UINT nMessage, WPARAM wParam = 0, <BR>                        LPARAM lParam = 0)

DrawTransparent

Enables the transparent mode. Note: this operation is not reversible.

DrawTransparent should be called just after the control is created. Do not use trasparent controls until you really need it (you have a bitmapped background) since each transparent control makes a copy in memory of its background. This may bring unnecessary memory use and execution overload.

// Parameters:
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
DWORD DrawTransparent(BOOL bRepaint = FALSE)

OnDrawBackground

This function is called every time the control background needs to be painted.
If the control is in transparent mode this function will NOT be called. This is a virtual function that can be rewritten in CKnobControlST-derived classes.
// Parameters:
//     [IN]   pGfx
//            Pointer to a GDI+ Graphics object that indicates the graphic <BR>//            context.
//     [IN]   pRect
//            Pointer to a CRect object that indicates the bounds of the
//            area to be painted.
//
// Return value:
//     KNOBCONTROLST_OK
//        Function executed successfully.
//
virtual DWORD OnDrawBackground(Graphics* pGfx, LPCRECT pRect)

OnDrawIcon

This function is called every time the icon associated to the control needs to be painted.
If the control has no icon this function will NOT be called. This is a virtual function that can be rewritten in CKnobControlST-derived classes.
// Parameters:
//     [IN]   pGfx
//            Pointer to a GDI+ Graphics object that indicates the graphic <BR>//            context.
//     [IN]   rpKnob
//            Pointer to a GDI+ Rect object that indicates the bounds of the
//            area taken by the control.
//
// Return value:
//     KNOBCONTROLST_OK
//        Function executed successfully.
//
virtual DWORD OnDrawIcon(Graphics* pGfx, Rect* rpKnob)

GetVersionI

Returns the class version as a short value.
// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()

GetVersionC

Returns the class version as a string value.
// Return value:
//     Pointer to a null-terminated string containig the class version.
//
static LPCTSTR GetVersionC()

History

  • v1.0 (15/June/2003)
    First release

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

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
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUsing the control in ActiveX Pin
pvn2332015-Jan-09 4:23
pvn2332015-Jan-09 4:23 
GeneralBug with negative range Pin
prcarp15-Feb-05 5:16
prcarp15-Feb-05 5:16 
GeneralRe: Fix for negative range Pin
prcarp15-Feb-05 5:28
prcarp15-Feb-05 5:28 
GeneralRe: Fix for negative range Pin
theboton10-Dec-06 20:11
theboton10-Dec-06 20:11 
GeneralI cant use it. Pin
Bahman Tahayori4-Oct-04 4:31
Bahman Tahayori4-Oct-04 4:31 
GeneralRe: I cant use it. Pin
Davide Calabro4-Oct-04 5:05
Davide Calabro4-Oct-04 5:05 
GeneralRe: I cant use it. Pin
Bahman Tahayori4-Oct-04 20:59
Bahman Tahayori4-Oct-04 20:59 
GeneralRe: I cant use it. Pin
hwoh26-Dec-06 14:39
hwoh26-Dec-06 14:39 
Generalvery good ,but don't support bitmap background ! Pin
wwjhw2-Mar-04 15:37
wwjhw2-Mar-04 15:37 
GeneralVersion 1.1 is available Pin
Davide Calabro29-Jun-03 12:30
Davide Calabro29-Jun-03 12:30 
GeneralBug Pin
Hans Dietrich27-Jun-03 4:51
mentorHans Dietrich27-Jun-03 4:51 
GeneralRe: Bug Pin
Davide Calabro27-Jun-03 5:31
Davide Calabro27-Jun-03 5:31 
GeneralRe: Bug Pin
Hans Dietrich27-Jun-03 7:08
mentorHans Dietrich27-Jun-03 7:08 
GeneralRe: Bug Pin
Davide Calabro27-Jun-03 7:25
Davide Calabro27-Jun-03 7:25 
GeneralRe: Bug Pin
Hans Dietrich27-Jun-03 10:00
mentorHans Dietrich27-Jun-03 10:00 
GeneralNice! Pin
Ravi Bhavnani26-Jun-03 20:33
professionalRavi Bhavnani26-Jun-03 20:33 
GeneralRe: Nice! Pin
Davide Calabro26-Jun-03 20:42
Davide Calabro26-Jun-03 20:42 
GeneralAwesome! Pin
Hans Dietrich26-Jun-03 18:14
mentorHans Dietrich26-Jun-03 18:14 
GeneralRe: Awesome! Pin
Davide Calabro26-Jun-03 20:43
Davide Calabro26-Jun-03 20:43 
GeneralVC6 on Win2000 no GDI+ Pin
Nikolai Teofilov25-Jun-03 23:09
Nikolai Teofilov25-Jun-03 23:09 
GeneralRe: VC6 on Win2000 no GDI+ Pin
Davide Calabro25-Jun-03 23:33
Davide Calabro25-Jun-03 23:33 
GeneralRe: VC6 on Win2000 no GDI+ Pin
whymegod4-Dec-03 11:18
whymegod4-Dec-03 11:18 
GeneralRe: VC6 on Win2000 no GDI+ Pin
David Lance5-Dec-04 14:50
David Lance5-Dec-04 14:50 
Generalsome functional improvements for this nice control Pin
Davide Pizzolato25-Jun-03 20:01
Davide Pizzolato25-Jun-03 20:01 
GeneralRe: some functional improvements for this nice control Pin
Davide Calabro25-Jun-03 22:12
Davide Calabro25-Jun-03 22:12 

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.