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));
if( hHandCursor == NULL )
{
GetWindowsDirectory(strWinDir, MAX_PATH);
strcat(strWinDir, _T("\\winhlp32.exe"));
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...