Click here to Skip to main content
Click here to Skip to main content

CBitmapSlider

, 18 Sep 2003
Rate this:
Please Sign up or sign in to vote.
A CStatic derived class that has slider functions. It is skinned using bitmaps.
<!-- Article image -->

Sample Image - CBitmapSlider.gif

<!-- Add the rest of your HTML here -->

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.

  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.

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

  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

About the Author

Junho Ryu
Web Developer
Korea (Republic Of) Korea (Republic Of)
Click here to view Junho Ryu's resume.

Comments and Discussions

 
GeneralRe: Problem with transparent Slider Pinmemberjankee2-Feb-08 15:12 
GeneralCrashes in debug mode. PinmemberWREY20-Sep-03 18:51 
GeneralRe: Crashes in debug mode. PinmemberJoon-ho Ryu21-Sep-03 14:20 
GeneralRe: Crashes in debug mode. Pinmemberhannahb20-Jul-05 5:29 
GeneralCool! PinmemberJuan Carlos Cobas18-Sep-03 22:33 
GeneralRe: Cool! PinmemberJoon-ho Ryu19-Sep-03 1:46 
GeneralYou got my 5 Pinmemberabhinaba18-Sep-03 21:47 
GeneralRe: You got my 5 PinmemberJoon-ho Ryu19-Sep-03 1:40 
Thank you. Smile | :)
Oh, why don't you share your plan with others?
If you play a role in this control, it will be good for everyone.
GeneralGreat! PinmemberNicolas Bonamy28-Aug-03 7:21 
GeneralRe: Great! PinmemberJoon-ho Ryu28-Aug-03 14:36 
GeneralRe: Great! PinmemberNicolas Bonamy29-Aug-03 4:52 
GeneralRe: Great! PinmemberJoon-ho Ryu29-Aug-03 6:25 
GeneralNice, very easy ;) PinmemberKochise27-Aug-03 23:05 
GeneralRe: Nice, very easy ;) PinmemberJoon-ho Ryu28-Aug-03 14:25 
GeneralCSkinSlider PinmemberKochise28-Aug-03 21:02 
GeneralRe: CSkinSlider PinmemberJoon-ho Ryu29-Aug-03 1:11 
GeneralDownload Executable PinmemberChristoph Lederer27-Aug-03 22:19 
GeneralRe: Download Executable PinmemberJoon-ho Ryu27-Aug-03 22:39 
QuestionWhy the hell people don't include compiled exe? PinmemberDavide Calabro27-Aug-03 21:18 
AnswerRe: Why the hell people don't include compiled exe? PinmemberTibor Blazko27-Aug-03 21:54 
GeneralRe: Why the hell people don't include compiled exe? PinmemberJoon-ho Ryu27-Aug-03 22:43 
GeneralRe: Why the hell people don't include compiled exe? PinmemberTibor Blazko27-Aug-03 22:59 
AnswerRe: Why the hell people don't include compiled exe? PinmemberRick York19-Sep-03 18:41 
GeneralGood Work! PinmemberHolger Persch27-Aug-03 19:41 
GeneralRe: Good Work! PinmemberJoon-ho Ryu27-Aug-03 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 19 Sep 2003
Article Copyright 2003 by Junho Ryu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid