// CustomScroll.cpp : implementation file
//
#include "stdafx.h"
#include "SkinDlg.h"
#include "CustomScroll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCustomScroll
CCustomScroll::CCustomScroll()
{
m_ButtonDown = FALSE;
m_IsLeft = FALSE;
m_MinRange = 0;
m_MaxRange = 200;
m_CurPos = 0;
m_IsLeftArrow = FALSE;
m_IsRightArrow = FALSE;
m_IsTopArrow=FALSE;
m_IsBottomArrow=FALSE;
m_IsLeftRange = FALSE;
m_IsRightRange = FALSE;
m_IsTopRange=FALSE;
m_IsBottomRange;
}
CCustomScroll::~CCustomScroll()
{
}
BEGIN_MESSAGE_MAP(CCustomScroll, CStatic)
//{{AFX_MSG_MAP(CCustomScroll)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCustomScroll message handlers
BOOL CCustomScroll::CreateStatic(CWnd *pParent, DWORD dwStyle, UINT nIDStatic, UINT nID)
{
m_pParent = pParent;
ASSERT(m_pParent);
//获取父窗口中的静态文本
ASSERT(::IsWindow(pParent->GetDlgItem(nIDStatic)->m_hWnd));
CRect recttemp;
pParent->GetDlgItem(nIDStatic)->GetWindowRect(&recttemp);
pParent->ScreenToClient(&recttemp);
// m_ClientRect = recttemp;
pParent->GetDlgItem(nIDStatic)->ShowWindow(SW_HIDE);
m_bHorizontal=(dwStyle & SBS_VERT) ? FALSE : TRUE;
BOOL ret = CStatic::Create("",dwStyle,recttemp,pParent,nID);
pParent->GetDlgItem(nIDStatic)->GetClientRect(&m_ClientRect);
if (ret)
{
CBitmap bmp;
// bmp.LoadBitmap(m_LeftArrow);
if (m_bHorizontal)
{
bmp.LoadBitmap(m_LeftArrow);
BITMAP bInfo;
bmp.GetBitmap(&bInfo);
m_ThumbHeight = bInfo.bmHeight;
m_ThumbWidth = bInfo.bmWidth;
if (bmp.GetSafeHandle())
bmp.DeleteObject();
// bmp.LoadBitmap(IDB_THUMB);
bmp.LoadBitmap(IDB_THUMB);
// bmp.LoadBitmap(m_bHorizontal?IDB_HTHUMB:/*IDB_VTHUMB*/IDB_THUMB);
bmp.GetBitmap(&bInfo);
m_ThumbRect.CopyRect(CRect(m_ThumbWidth,0,m_ThumbWidth+bInfo.bmWidth,bInfo.bmHeight));
if (bmp.GetSafeHandle())
bmp.DeleteObject();
SetScrollRange(m_MinRange,m_MaxRange);
}
else
{
bmp.LoadBitmap(m_TopArrow);
BITMAP bInfo;
bmp.GetBitmap(&bInfo);
m_ThumbHeight = bInfo.bmHeight;
m_ThumbWidth = bInfo.bmWidth;
if (bmp.GetSafeHandle())
bmp.DeleteObject();
// bmp.LoadBitmap(IDB_THUMB);
bmp.LoadBitmap(IDB_THUMB);
// bmp.LoadBitmap(m_bHorizontal?IDB_HTHUMB:/*IDB_VTHUMB*/IDB_THUMB);
bmp.GetBitmap(&bInfo);
m_ThumbRect.CopyRect(CRect(0,m_ThumbHeight,bInfo.bmWidth,m_ThumbHeight+bInfo.bmHeight));
if (bmp.GetSafeHandle())
bmp.DeleteObject();
SetScrollRange(m_MinRange,m_MaxRange);
}
}
ShowWindow(SW_SHOW);
return ret;
}
void CCustomScroll::DrawHorScroll()
{
CClientDC dc(this);
CMemDC_C memdc(&dc,m_ClientRect);
CDC bmpdc;
bmpdc.CreateCompatibleDC(&dc);
//绘制左箭头
CBitmap bmp;
bmp.LoadBitmap(m_LeftArrow);
CBitmap* pOldbmp = bmpdc.SelectObject(&bmp);
CRect LeftArrowRect (m_ClientRect.left,m_ClientRect.top,m_ClientRect.left+m_ThumbWidth,m_ClientRect.bottom);
memdc.StretchBlt(m_ClientRect.left,m_ClientRect.top,m_ThumbWidth,m_ThumbHeight,&bmpdc,0,0,m_ThumbWidth,m_ThumbHeight,SRCCOPY);
if (pOldbmp)
bmpdc.SelectObject(pOldbmp);
if (bmp.GetSafeHandle())
bmp.DeleteObject();
pOldbmp = NULL;
//通道的开始位置和宽度
int nChanelStart = m_ClientRect.left+m_ThumbWidth;
int nChanelWidth = m_ClientRect.Width()- 2*m_ThumbWidth;
//绘制通道
bmp.LoadBitmap(m_ChanelBK);
pOldbmp = bmpdc.SelectObject(&bmp);
memdc.StretchBlt(nChanelStart,m_ClientRect.top,nChanelWidth,m_ClientRect.Height(),&bmpdc,0,0,1,10,SRCCOPY);
if (pOldbmp)
bmpdc.SelectObject(pOldbmp);
if (bmp.GetSafeHandle())
bmp.DeleteObject();
//绘制右箭头
bmp.LoadBitmap(m_RightArrow);
pOldbmp = bmpdc.SelectObject(&bmp);
int nRArrowStart = m_ThumbWidth+nChanelWidth;
memdc.StretchBlt(nRArrowStart,m_ClientRect.top,m_ThumbWidth,m_ClientRect.Height(),&bmpdc,0,0,m_ThumbWidth,m_ThumbHeight,SRCCOPY);
//绘制滚动块
if (bmp.GetSafeHandle())
bmp.DeleteObject();
bmp.LoadBitmap(m_ThumbBK);
pOldbmp = bmpdc.SelectObject(&bmp);
memdc.StretchBlt(m_ThumbRect.left,m_ThumbRect.top,m_ThumbRect.Width()+1,m_ThumbRect.Height(),&bmpdc,0,0,m_ThumbRect.Width(),m_ThumbRect.Height(),SRCCOPY);
}
void CCustomScroll::DrawVScroll()
{
CClientDC dc(this);
CMemDC_C memdc(&dc,m_ClientRect);
CDC bmpdc;
bmpdc.CreateCompatibleDC(&dc);
//绘制上箭头
CBitmap bmp;
bmp.LoadBitmap(m_TopArrow);
CBitmap* pOldbmp = bmpdc.SelectObject(&bmp);
// CRect LeftArrowRect (m_ClientRect.left,m_ClientRect.top,m_ClientRect.right,m_ClientRect.bottom+m_ThumbHeight);
memdc.StretchBlt(m_ClientRect.left,m_ClientRect.top,m_ThumbWidth,m_ThumbHeight,&bmpdc,0,0,m_ThumbWidth,m_ThumbHeight,SRCCOPY);
// CRect rectUpArrow (m_ClientRect.left,m_ClientRect.top,m_ClientRect.right,m_ClientRect.bottom+m_ThumbHeight);
// memdc.StretchBlt(rectUpArrow.left,rectUpArrow.top,rectUpArrow.Width(),rectUpArrow.Height(),&bmpdc,0,0,m_ThumbWidth,m_ThumbHeight,SRCCOPY);
if (pOldbmp)
bmpdc.SelectObject(pOldbmp);
if (bmp.GetSafeHandle())
bmp.DeleteObject();
pOldbmp = NULL;
//通道的开始位置和宽度
int nChanelStart = m_ClientRect.top+m_ThumbHeight;
int nChanelHeight = m_ClientRect.Height()- 2*m_ThumbHeight;
//绘制通道
bmp.LoadBitmap(m_ChanelBK);
pOldbmp = bmpdc.SelectObject(&bmp);
// memdc.StretchBlt(nChanelStart,m_ClientRect.top,nChanelWidth,m_ClientRect.Height(),&bmpdc,0,0,1,10,SRCCOPY);
memdc.StretchBlt(m_ClientRect.left,nChanelStart,m_ClientRect.Width(),nChanelHeight,&bmpdc,0,0,m_ThumbWidth,1,SRCCOPY);
//
// CRect rectChannelDown(m_ClientRect.left, m_ClientRect.top + m_ThumbHeight/2, m_ClientRect.right, nChanelStart + nChanelHeight);
//
// memdc.StretchBlt(rectChannelDown.left+1, rectChannelDown.top, rectChannelDown.Width()-1, rectChannelDown.Height(), &bmpdc, 0, 0, m_ThumbWidth, m_ThumbHeight, SRCCOPY);
if (pOldbmp)
bmpdc.SelectObject(pOldbmp);
if (bmp.GetSafeHandle())
bmp.DeleteObject();
//绘制下箭头
bmp.LoadBitmap(m_BottomArrow);
pOldbmp = bmpdc.SelectObject(&bmp);
int nRArrowStart = m_ThumbHeight+nChanelHeight;
// memdc.StretchBlt(nRArrowStart,m_ClientRect.top,m_ThumbWidth,m_ClientRect.Height(),&bmpdc,0,0,m_ThumbWidth,m_ThumbHeight,SRCCOPY);
memdc.StretchBlt(m_ClientRect.left,nRArrowStart,m_ClientRect.Width(),m_ThumbHeight,&bmpdc,0,0,m_ThumbWidth,m_ThumbHeight,SRCCOPY);
if (pOldbmp)
bmpdc.SelectObject(pOldbmp);
if (bmp.GetSafeHandle())
bmp.DeleteObject();
//绘制滚动块
// if (bmp.GetSafeHandle())
// bmp.DeleteObject();
bmp.LoadBitmap(m_ThumbBK);
pOldbmp = bmpdc.SelectObject(&bmp);
// memdc.StretchBlt(m_ThumbRect.left,m_ThumbRect.top,m_ThumbRect.Width(),m_ThumbRect.Height(),&bmpdc,0,0,/*m_ThumbRect.Width(),m_ThumbRect.Height()*/m_ThumbWidth,m_ThumbHeight,SRCCOPY);
memdc.StretchBlt(m_ThumbRect.left,m_ThumbRect.top,m_ThumbRect.Width(),m_ThumbRect.Height(),&bmpdc,0,0,/*m_ThumbRect.Width(),m_ThumbRect.Height()*/m_ThumbWidth,m_ThumbHeight,SRCCOPY);
}
void CCustomScroll::DrawControl()
{
if (m_bHorizontal)
DrawHorScroll();
else
DrawVScroll();
}
void CCustomScroll::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawControl();
// Do not call CStatic::OnPaint() for painting messages
}
void CCustomScroll::OnLButtonDown(UINT nFlags, CPoint point)
{
m_Startpt = point;
//确定滚动区域
CRect rcScroll = m_ClientRect;
DWORD wparam;
SetCapture();
if (m_bHorizontal)
{
rcScroll.left += m_ThumbWidth;
rcScroll.right-= m_ThumbWidth;
if (m_ThumbRect.PtInRect(point))
{
m_ButtonDown = TRUE;
}
else if (rcScroll.