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

A cool bitmap slider like Media Player's...

Rate me:
Please Sign up or sign in to vote.
3.53/5 (16 votes)
23 Jul 2004 79.7K   5.8K   49   9
A cool bitmap slider like Windows Media Player's.

Sample Image - CoolSlider.gif

Introduction

Do you like the cool slider in Windows Media Player 9.0? Yes!!

Let's build it, following this article.

Used custom classes

  • CBitItem

    Inherits none. Manages bitmap resources, one or more bitmaps.

  • CBitWnd

    Inherits CWnd. Manages owner draw bitmap window.

  • CBitSlider

    Inherits CSliderCtrl. Manages owner draw bitmap slider.

By the way:

CBitItem and CBitWnd are common tool classes for any place where you want to make bitmap owner draw window...

Sample demo code

Step 1: Get the hand cursor from OS
HCURSOR    CCoolSliderDlg::GetSysHandCursor()
{
    TCHAR        strWinDir[MAX_PATH] = {0};
    HCURSOR        hHandCursor    = NULL;
    hHandCursor = ::LoadCursor(NULL, MAKEINTRESOURCE(32649));
    
    // Still no cursor handle - load the WinHelp hand cursor
    if( hHandCursor == NULL )
    {
        GetWindowsDirectory(strWinDir, MAX_PATH);
        strcat(strWinDir, _T("\\winhlp32.exe"));
        
        // This retrieves cursor #106 from winhlp32.exe,
        // which is a hand pointer
        HMODULE hModule = ::LoadLibrary(strWinDir);
        DWORD    dwErr = GetLastError();
        if( hModule != NULL )
        {
            HCURSOR     hTempCur = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
            hHandCursor = (hTempCur != NULL) ? CopyCursor(hTempCur) : NULL;
            FreeLibrary(hModule);
        }
    }
    return hHandCursor;
}
Step 2: Build a cool-slider using exclusive bitmap resource
void CCoolSliderDlg::BuildCoolSlider()
{
    m_hHandCur = this->GetSysHandCursor();
    ASSERT( m_hHandCur != NULL );
    
    m_ctlSlider.SetFlipCursor(m_hHandCur);
    m_ctlSlider.BuildThumbItem(IDB_BITMAP_THUMB, 6, 12);
    m_ctlSlider.BuildBackItem(IDB_BITMAP_NORMAL, IDB_BITMAP_ACTIVE);
    m_ctlSlider.SetTopOffset(3);
    m_ctlSlider.SetRange(0, 100);
    m_ctlSlider.SetLineSize(0);
    m_ctlSlider.SetPos(20);
}
Step 3: Build some cool-sliders using shared bitmap resource (3 sliders use the same bitmap resource)
void CCoolSliderDlg::BuildShareSlider()
{
    ASSERT( m_hHandCur != NULL );

    ASSERT( m_lpActive == NULL );
    ASSERT( m_lpNormal == NULL );
    ASSERT( m_lpThumb == NULL );

    m_lpActive = new CBitItem(IDB_BITMAP_ACTIVE, 0, 0);
    m_lpNormal = new CBitItem(IDB_BITMAP_NORMAL, 0, 0);
    m_lpThumb  = new CBitItem(IDB_BITMAP_THUMB, 6, 12);

    m_ctlShare1.SetFlipCursor(m_hHandCur);
    m_ctlShare1.BuildThumbItem(m_lpThumb);
    m_ctlShare1.BuildBackItem(m_lpNormal, m_lpActive);
    m_ctlShare1.SetTopOffset(3);
    m_ctlShare1.SetRange(0, 100);
    m_ctlShare1.SetLineSize(0);
    m_ctlShare1.SetPos(40);

    m_ctlShare2.SetFlipCursor(m_hHandCur);
    m_ctlShare2.BuildThumbItem(m_lpThumb);
    m_ctlShare2.BuildBackItem(m_lpNormal, m_lpActive);
    m_ctlShare2.SetTopOffset(3);
    m_ctlShare2.SetRange(0, 100);
    m_ctlShare2.SetLineSize(0);
    m_ctlShare2.SetPos(60);

    m_ctlShare3.SetFlipCursor(m_hHandCur);
    m_ctlShare3.BuildThumbItem(m_lpThumb);
    m_ctlShare3.BuildBackItem(m_lpNormal, m_lpActive);
    m_ctlShare3.SetTopOffset(3);
    m_ctlShare3.SetRange(0, 100);
    m_ctlShare3.SetLineSize(0);
    m_ctlShare3.SetPos(80);
}
Step 4: Erase the dialog background using slider bitmap's back-color
BOOL CCoolSliderDlg::OnEraseBkgnd(CDC* pDC) 
{
    CRect    rcRect;
    this->GetClientRect(rcRect);
    
    pDC->FillSolidRect(rcRect, RGB(96, 123, 189));

    return TRUE;
}
Step 5: Release all the allocated resources when program exits
CCoolSliderDlg::~CCoolSliderDlg()
{
    if( m_lpNormal != NULL )
    {
        delete m_lpNormal;
        m_lpNormal = NULL;
    }
    if( m_lpActive != NULL )
    {
        delete m_lpActive;
        m_lpActive = NULL;
    }
    if( m_lpThumb != NULL )
    {
        delete m_lpThumb;
        m_lpThumb = NULL;
    }
}

Want to know more detail? Please see the demo project!!

Bug report or comments, please contact me: Jackey.

Enjoy it!!

History

  • 07/24/2004

    First version... (Only supports horizontal slider:))

  • 07/25/2004

    Added vertical slider support, thanks for PJ Arends.

  • To be continued...

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
China China
Hello everybody!!

Comments and Discussions

 
GeneralMy vote of 4 Pin
lanyuliuyun19-Aug-10 18:57
lanyuliuyun19-Aug-10 18:57 
Generalwonderful! Pin
huangzongwu21-Oct-09 21:06
huangzongwu21-Oct-09 21:06 
General.Net dll [modified] Pin
jen0s7-Sep-07 5:37
jen0s7-Sep-07 5:37 
GeneralCrash moving the slider Pin
Paolo Bozzoli30-May-06 4:03
professionalPaolo Bozzoli30-May-06 4:03 
Hi, I tested the slider in my application and i reported a big memory leak and a crash when moving the slider continuously for about two minutes. The problem is in the CBitSlider::OnMouseMove but I can't find solution. Have you got any idea?
Thx
QuestionLoading Bitmaps from Outside Pin
MacGadger19-Apr-06 19:05
MacGadger19-Apr-06 19:05 
GeneralThank you for your article Pin
Fransiscusherry29-Jan-06 11:50
Fransiscusherry29-Jan-06 11:50 
GeneralSlider dimensions Pin
camillo2-Sep-04 22:53
camillo2-Sep-04 22:53 
GeneralNice Start Pin
PJ Arends24-Jul-04 13:23
professionalPJ Arends24-Jul-04 13:23 
GeneralRe: Nice Start Pin
jackey_xp24-Jul-04 18:39
jackey_xp24-Jul-04 18:39 

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.