Click here to Skip to main content
15,881,027 members
Articles / Desktop Programming / MFC
Article

CBitmapSlider

Rate me:
Please Sign up or sign in to vote.
4.87/5 (81 votes)
18 Sep 20034 min read 287.5K   17.7K   184   67
A CStatic derived class that has slider functions. It is skinned using bitmaps.

Sample Image - CBitmapSlider.gif

Introduction

Do you need a slider but default control looks too simple? I hope this class, CBitmapSlider, can help you. All you have to do is read this article and make a few bitmaps.

Features

  • Transparency effect for both channel and thumb.
  • Customize size of the thumb.
  • Flicker free.

It uses CMemDC from Keith Rule's article to implement double buffering, and uses functions from Raja Segar's article to display transparent bitmap.

Using the code

  1. Add "BitmapSlider.h", "BitmapSlider.cpp" and "memdc.h" to your project.
  2. Insert or import bitmaps to resource of your project. One slider can have utmost four bitmaps (channel, active channel, thumb, active thumb). By various combinations of bitmaps, the control can have different forms. However, there are some restrictions.

    • The bitmap size of channel and active channel must be equal. And the bitmap size of thumbs and active thumb must also be equal.
    • The size of a thumb must be smaller than the size of a channel.
    • To use a bitmap for active state, a bitmap for normal state must exist.

  3. Add control into your dialog resource.

    Both "Picture" control and "Static Text" control are available. But if you have bitmaps for the channel, I recommend "Picture" control. If you will not use a bitmap for a channel, add "Static Text" control and make sure to change its size as same size as you want.

    Image 2 Image 3

  4. Change properties.

    Change it's own ID from default value (IDC_STATIC) to a new one. If you added "Picture" control, set "Type" as "Bitmap" and select "Image" as ID of a bitmap that you want to use as channel.

    Image 4

    Make sure "Notify" is checked. "Tabstop" is optional.

    Image 5

  5. Add variable for the control.

    Add member variable using MFC ClassWizard (Ctrl+W). and change from "CStatic" to "CBitmapSlider" manually.

  6. Insert codes to initialize the control. I recommend OnInitDialog for CDialog, OnInitUpdate for CFormView. There are detailed documents about methods at "Member Functions" section.

    Example Code

    BOOL CMyDialog::OnInitDialog()()
    {
        ...
    
        m_Slider.SetBitmapChannel( IDB_CHANNEL, NULL, TRUE );
        m_Slider.SetBitmapThumb( IDB_THUMB, IDB_THUMB_ACTIVE, TRUE );
        m_Slider.SetRange( -30, 200 );
        m_Slider.SetPos( -30 );
        m_Slider.SetMarginLeft( 8 );
        m_Slider.SetMarginRight( 8 );
    
        ...
    }

  7. Implement message handler.

    Insert these lines to implement message handler.

    • In the header file.
      afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
    • In the message map.
      ON_MESSAGE(WM_BITMAPSLIDER_MOVING, OnMyMessage)
    • In the source code.
      LRESULT CMyDlg::OnMyMessage(WPARAM wParam, LPARAM lParam) {
      ...
      }

    "Notification Messages" section explains about messages.

Member Functions

  • SetBitmapChannel - Load bitmaps for a channel
    BOOL SetBitmapChannel(
        UINT nChannelID,
        UINT nActiveID = NULL,
        BOOL bTransparent = FALSE,
        COLORREF clrpTransColor = 0xFF000000,
        int iTransPixelX = 0,
        int iTransPixelY = 0
    );
    Parameters
    • nChannelID - ID number of the bitmap resource of the channel.
    • nActiveID - ID number of the bitmap resource of the active channel.
    • bTransparent - TRUE to apply transparency effect.
    • clrpTransColor - RGB color to treat as transparent.
    • iTransPixelX - Logical x-coordinate of a point. It's color will be treated as transparent.
    • iTransPixelY - Logical y-coordinate of a point.

    Returns  TRUE if function succeeds; otherwise FALSE.

  • SetBitmapThumb - Load bitmaps for a thumb
    BOOL SetBitmapThumb(
        UINT nThumbID,
        UINT nActiveID = NULL,
        BOOL bTransparent = FALSE,
        COLORREF clrpTransColor = 0xFF000000,
        int iTransPixelX = 0,
        int iTransPixelY = 0
    );
    Parameters
    • nThumbID - ID of bitmap resource.
    • nActiveID - ID of bitmap resource.
    • bTransparent - TRUE to apply transparency effect.
    • clrpTransColor - RGB color to treat as transparent.
    • iTransPixelX - Logical x-coordinate of a point. It's color will be treated as transparent.
    • iTransPixelY - Logical y-coordinate of a point.

    Returns: TRUE if function succeeds; otherwise FALSE.

  • SetRange - Sets the range (minimum and maximum positions) for the slider.
    BOOL SetRange(
        int nMin,
        int nMax,
        BOOL bRedraw = FALSE
    );
    Parameters:
    • nMin - Minimum position for the slider.
    • nMax - Maximum position for the slider.
    • bRedraw - TRUE to redraw after the range is set. FALSE to only change the range.

  • SetVertical - Sets slider to vertical or horizontal.
    void SetVertical(
        BOOL bVertical = TRUE
    );
    Parameters:
    • bVertical - TRUE to set vertical. FALSE to set horizontal.

  • SetMargin - Sets the margins for a control.
    void SetPos(
        int nLeft,
        int nTop,
        int nRight,
        int nBottom
    );
    Parameters:
    • nLeft - Specifies the width of the new left margin, in pixels.
    • nTop - Specifies the height of the new top margin, in pixels.
    • nRight - Specifies the width of the new right margin, in pixels.
    • nBottom - Specifies the height of the new bottom margin, in pixels.

  • SetPos - Sets the current position of the slider.
    void SetPos(
        int nPos
    );
    Parameters:
    • nPos - Specifies the new slider position.

  • SetPageSize - Sets the size of the page for a control.
    void SetPageSize(
        int nSize
    );
    Parameters:
    • nSize - The new page size of the control.

    Return Value: The previous page size.

  • GetRange - Retrieves the maximum and minimum positions for the slider.
    void GetRange(
        int& nMin,
        int& nMax
    );
    Parameters:
    • nMin - Reference to an integer that receives the minimum position.
    • nMax - Reference to an integer that receives the maximum position.

  • GetPos - Retrieves the current position of the slider.
    int GetPos();
    Return Value: The current position.

  • Enable - Enables or disables control.
    void Enable( BOOL bEnable );
    Parameter:
    • bEnable - TRUE to enable control. FALSE to disable control.

  • DrawFocusRect - Specify whether draw focus rectangle or not.
    DrawFocusRect(BOOL bDraw, BOOL bRedraw);
    Parameter:
    • bDraw - TRUE to draw focus rectangle. FALSE to hide focus rectangle.
    • bRedraw - TRUE to redraw status is changed. FALSE to only change the status.

Notification Messages

  • WM_BITMAPSLIDER_MOVING - While slider is being dragged.
    WM_BITMAPSLIDER_MOVING
    
        WPARAM wParam
        LPARAM lParam;
    Parameters:
    • wParam - Control's ID.
    • lParam - The current position.

  • WM_BITMAPSLIDER_MOVED - When dragging is finished.
    WM_BITMAPSLIDER_MOVING
    
        WPARAM wParam
        LPARAM lParam;
    Parameters:
    • wParam - Control's ID.
    • lParam - The current position.

Todo

  • Support changing size of control.

History

  • v1.5 (16/September/2003)
    • Keyboard support.
    • Fixed a few bugs.
  • v1.0 (28/August/2003)
    • Double buffring.
    • Transparency effect is added.
    • Support vertical option.
  • v0.7 (15/May/2003)
    • First release.

Thanks To

  • Holger Persch - Both suggested and taught to implement keyboard support.
  • Nicolas Bonamy - Advised to implement OnDestroy to detach bitmap.

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
Korea (Republic of) Korea (Republic of)
Click here to view Junho Ryu's resume.

Comments and Discussions

 
GeneralRe: CBitmapSlider for PocketPC Pin
jschisler7-Dec-04 7:40
jschisler7-Dec-04 7:40 
GeneralGreat stuff, but I have a problem Pin
martintiger6-Aug-04 7:33
martintiger6-Aug-04 7:33 
GeneralNice Work Pin
beash20-May-04 4:13
beash20-May-04 4:13 
GeneralAwesome, but one suggestion Pin
Greg Ellis23-Feb-04 4:58
Greg Ellis23-Feb-04 4:58 
QuestionAny chance of making it an ActiveX Control? Pin
bigolslabomeat12-Jan-04 17:27
bigolslabomeat12-Jan-04 17:27 
GeneralExcellent creativity, Pin
Balkrishna Talele17-Dec-03 1:11
Balkrishna Talele17-Dec-03 1:11 
GeneralRe: Excellent creativity, Pin
Junho Ryu29-Dec-03 17:57
Junho Ryu29-Dec-03 17:57 
QuestionHow to use in C# Pin
Armoghan Asif4-Nov-03 0:37
Armoghan Asif4-Nov-03 0:37 
This is a wonderful control. I have never seen anything like it.
But how can i use it in C#?
Thx in advance
AnswerRe: How to use in C# Pin
Junho Ryu6-Nov-03 18:13
Junho Ryu6-Nov-03 18:13 
AnswerRe: How to use in C# Pin
texor21-Dec-08 22:50
texor21-Dec-08 22:50 
GeneralProblem with transparent Slider Pin
Olaf Gramkow1-Nov-03 11:11
Olaf Gramkow1-Nov-03 11:11 
GeneralRe: Problem with transparent Slider Pin
Junho Ryu4-Nov-03 0:20
Junho Ryu4-Nov-03 0:20 
GeneralRe: Problem with transparent Slider Pin
Olaf Gramkow4-Nov-03 9:10
Olaf Gramkow4-Nov-03 9:10 
GeneralRe: Problem with transparent Slider Pin
Junho Ryu6-Nov-03 17:57
Junho Ryu6-Nov-03 17:57 
GeneralRe: Problem with transparent Slider Pin
Greg Ellis23-Dec-04 9:50
Greg Ellis23-Dec-04 9:50 
GeneralRe: Problem with transparent Slider Pin
extrealm7-Aug-05 3:57
extrealm7-Aug-05 3:57 
GeneralRe: Problem with transparent Slider Pin
Raffaelli Stefano17-Sep-06 22:26
Raffaelli Stefano17-Sep-06 22:26 
GeneralRe: Problem with transparent Slider Pin
jankee2-Feb-08 15:12
jankee2-Feb-08 15:12 
GeneralCrashes in debug mode. Pin
WREY20-Sep-03 18:51
WREY20-Sep-03 18:51 
GeneralRe: Crashes in debug mode. Pin
Junho Ryu21-Sep-03 14:20
Junho Ryu21-Sep-03 14:20 
GeneralRe: Crashes in debug mode. Pin
hannahb20-Jul-05 5:29
hannahb20-Jul-05 5:29 
GeneralCool! Pin
Juan Carlos Cobas18-Sep-03 22:33
Juan Carlos Cobas18-Sep-03 22:33 
GeneralRe: Cool! Pin
Junho Ryu19-Sep-03 1:46
Junho Ryu19-Sep-03 1:46 
GeneralYou got my 5 Pin
abhinaba18-Sep-03 21:47
abhinaba18-Sep-03 21:47 
GeneralRe: You got my 5 Pin
Junho Ryu19-Sep-03 1:40
Junho Ryu19-Sep-03 1:40 

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.