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

CBitmapSlider

By , 18 Sep 2003
Rate this:
Please Sign up or sign in to vote.
<!-- 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

 
BugError with the image in some function and Message Map Pinmemberkimkeang3-Apr-13 22:35 
GeneralRe: Error with the image in some function and Message Map Pinmemberkimkeang3-Apr-13 23:30 
GeneralMy vote of 5 PinmemberT_Tanprasert10-Jan-12 15:35 
Questionmake thumb not visible Pinmembershifengpfc_msn@msn.com3-Jul-11 17:14 
GeneralThank you Pinmemberfuturejo27-Jul-09 6:42 
QuestionIt can work well in serial port communication! Pinmemberwalker_ck24-Apr-09 17:52 
GeneralGreat implementation, but ... Pinmember1Bob18-Dec-08 5:43 
GeneralRe: Great implementation, but ... Pinmember1Bob18-Dec-08 10:14 
GeneralThank you Pinmemberhefulong2-Jul-08 16:34 
GeneralThank you very much! Pinmemberkaiser13142-Mar-08 19:34 
GeneralRe: Thank you very much! Pinmemberjoyjjjz25-Sep-08 20:42 
QuestionInvalidate(); PinmemberAlasTheDay23-Aug-07 21:14 
GeneralBug in Focus rect PinmemberWill4923-Apr-07 4:22 
JokePerfectoooo !!!!!!!!!!!!!!! PinmemberManni Singh25-May-06 9:11 
QuestionRe: Perfectoooo !!!!!!!!!!!!!!! Pinmemberjamesw200323-Feb-07 11:21 
Generalgood thanks Pinmemberhpxs29-Sep-05 17:27 
GeneralSizing issue. Pinsussextrealm7-Aug-05 7:27 
General&amp;#44048;&amp;#49324;&amp;#50836; Pinmemberjckkorea23-Oct-04 4:08 
GeneralCBitmapSlider for PocketPC Pinmembercodehar7-Sep-04 5:01 
GeneralRe: CBitmapSlider for PocketPC Pinmemberjschisler7-Dec-04 7:40 
I just commented out the lines in CMemDC that aren't supported on Pocket PC and it compiled. I had to adjust the thumb a bit in the code, but other than that (and a few graphic anomolies), it seems to work fine.
GeneralGreat stuff, but I have a problem Pinmembermartintiger6-Aug-04 7:33 
GeneralNice Work Pinmemberbeash20-May-04 4:13 
GeneralAwesome, but one suggestion PinmemberGreg Ellis23-Feb-04 4:58 
QuestionAny chance of making it an ActiveX Control? Pinmemberbigolslabomeat12-Jan-04 17:27 
GeneralExcellent creativity, PinmemberBalkrishna Talele17-Dec-03 1:11 

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
Web03 | 2.8.140415.2 | Last Updated 19 Sep 2003
Article Copyright 2003 by Junho Ryu
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid