上一篇中,滑块的基本功能已经实现了。但是外观上还不能满足我们的要求。
这里我们将主要完善滑块轨道的外观:
这里我们要准备三副图片 存放在/res/文件夹下,分别表示色调,饱和度和亮度
H.gif S.gif B.gif
这里我采用的gif图片,而不是bmp位图;主要是色彩上gif更能满足我的要求。
我利用IPicture将这图片加载到程序中,当然还可以用GDI+实现。
自定义函数:CNewSlider::DrawBKChannel(CDC *pDc, CRect rc)
CNewSlider::DrawBKChannel(CDC
*
pDc, CRect rc)
... {
pDc->SetBkMode(TRANSPARENT);
CFile mFile;
LONG nLength;
if ( mFile.Open( _T( "./res/" +m_strBkFile),CFile::modeRead) &&( ( nLength = mFile.GetLength() ) > 0))
...{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nLength );
LPVOID pvData = NULL;
if ( hGlobal != NULL )
...{
if ( ( pvData = GlobalLock(hGlobal) ) != NULL )
...{
mFile.Read( pvData, nLength );
mFile.Close();
GlobalUnlock( hGlobal );
IStream *pStm;
CreateStreamOnHGlobal( hGlobal, TRUE, &pStm) ;
IPicture *pPic;
if( SUCCEEDED( OleLoadPicture( pStm, nLength, TRUE, IID_IPicture, (LPVOID*)&pPic ) ) )
...{
pStm->Release();
pStm=NULL;
OLE_XSIZE_HIMETRIC mWidth;
OLE_YSIZE_HIMETRIC mHeight;
pPic->get_Width(&mWidth );
pPic->get_Height( &mHeight );
CSize sz(mWidth,mHeight);
pDc->HIMETRICtoDP(&sz);
double fX,fY;
fX=(double)sz.cx;
fY=(double)sz.cy;
if( FAILED( pPic->Render( pDc->GetSafeHdc(), 7, 8, (DWORD)fX , (DWORD)fY , 0, mHeight, mWidth, -mHeight, NULL ) ) )
AfxMessageBox( _T("渲染图片失败") );
pPic->Release();
pPic=NULL;
FreeResource(hGlobal);
}
else
AfxMessageBox( _T("从stream中装载图片失败") );
}
}
else
AfxMessageBox( _T("分配内存失败") );
}
else
AfxMessageBox( _T("打开文件失败") );
}
... {
pDc->SetBkMode(TRANSPARENT);
CFile mFile;
LONG nLength;
if ( mFile.Open( _T( "./res/" +m_strBkFile),CFile::modeRead) &&( ( nLength = mFile.GetLength() ) > 0))
...{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nLength );
LPVOID pvData = NULL;
if ( hGlobal != NULL )
...{
if ( ( pvData = GlobalLock(hGlobal) ) != NULL )
...{
mFile.Read( pvData, nLength );
mFile.Close();
GlobalUnlock( hGlobal );
IStream *pStm;
CreateStreamOnHGlobal( hGlobal, TRUE, &pStm) ;
IPicture *pPic;
if( SUCCEEDED( OleLoadPicture( pStm, nLength, TRUE, IID_IPicture, (LPVOID*)&pPic ) ) )
...{
pStm->Release();
pStm=NULL;
OLE_XSIZE_HIMETRIC mWidth;
OLE_YSIZE_HIMETRIC mHeight;
pPic->get_Width(&mWidth );
pPic->get_Height( &mHeight );
CSize sz(mWidth,mHeight);
pDc->HIMETRICtoDP(&sz);
double fX,fY;
fX=(double)sz.cx;
fY=(double)sz.cy;
if( FAILED( pPic->Render( pDc->GetSafeHdc(), 7, 8, (DWORD)fX , (DWORD)fY , 0, mHeight, mWidth, -mHeight, NULL ) ) )
AfxMessageBox( _T("渲染图片失败") );
pPic->Release();
pPic=NULL;
FreeResource(hGlobal);
}
else
AfxMessageBox( _T("从stream中装载图片失败") );
}
}
else
AfxMessageBox( _T("分配内存失败") );
}
else
AfxMessageBox( _T("打开文件失败") );
}
其中:m_strBkFile是我们要加载的图片名称(H.gif,S.gif,B.gif);
滑块的外观我就简单的修改了一下,不是太好看。程序也贴出来:
void
CNewSlider::DrawThumb(CDC
*
pDC, LPNMCUSTOMDRAW lpcd)
... {
CRect crect;
GetThumbRect(&crect);
COLORREF col;
CRect rect(lpcd->rc);
if(rect.Width()>rect.Height())...{ //垂直滑块
return;
}else...{
col=RGB(110,196,70);
CRect crc;
GetChannelRect(crc);
pDC->FillSolidRect(crect.left,crect.top+2,crect.Width(),1,col);
pDC->FillSolidRect(crect.left,crect.top+3,1,crc.bottom-3,col);
pDC->FillSolidRect(crect.right-1,crect.top+3,1,crc.bottom-3,col);
CPoint spoint;
CPoint epoint;
epoint.x=crect.left+crect.Width()/2;
epoint.y=crect.bottom-3;
spoint.x=crect.left;
spoint.y=crc.bottom+1;
CPen pen(PS_SOLID,1,col);
CPen* pOldPen=pDC->SelectObject(&pen);
pDC->MoveTo(epoint);
pDC->LineTo(spoint);
spoint.x=crect.right;
pDC->MoveTo(epoint);
pDC->LineTo(spoint);
}
}
... {
CRect crect;
GetThumbRect(&crect);
COLORREF col;
CRect rect(lpcd->rc);
if(rect.Width()>rect.Height())...{ //垂直滑块
return;
}else...{
col=RGB(110,196,70);
CRect crc;
GetChannelRect(crc);
pDC->FillSolidRect(crect.left,crect.top+2,crect.Width(),1,col);
pDC->FillSolidRect(crect.left,crect.top+3,1,crc.bottom-3,col);
pDC->FillSolidRect(crect.right-1,crect.top+3,1,crc.bottom-3,col);
CPoint spoint;
CPoint epoint;
epoint.x=crect.left+crect.Width()/2;
epoint.y=crect.bottom-3;
spoint.x=crect.left;
spoint.y=crc.bottom+1;
CPen pen(PS_SOLID,1,col);
CPen* pOldPen=pDC->SelectObject(&pen);
pDC->MoveTo(epoint);
pDC->LineTo(spoint);
spoint.x=crect.right;
pDC->MoveTo(epoint);
pDC->LineTo(spoint);
}
}
至此,界面工作基本完成,不是很好看,但也不太难看。
接下来的工作比较简单了,基本需要实现RGB颜色和HSB颜色的转换。