⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jwlistbmpvscrollbar.cpp

📁 在evc环境下
💻 CPP
字号:
// JWBmpVScrollbar.cpp : implementation file
//

#include "stdafx.h"
 
#include "JWListBmpVScrollbar.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CJWListBmpVScrollbar

CJWListBmpVScrollbar::CJWListBmpVScrollbar()
{
	bMouseDown = false;
	bMouseDownArrowUp = false;
	bMouseDownArrowDown = false;
	bDragging = false;

	nThumbTop = 36;
	dbThumbInterval = 0.000000;
	pList = NULL;

	m_upArrowWidth = 0;
	m_upArrowHeight= 0;
	m_thumbWidth  = 0;
	m_thumbHeight  = 0;
}

CJWListBmpVScrollbar::~CJWListBmpVScrollbar()
{
}


BEGIN_MESSAGE_MAP(CJWListBmpVScrollbar, CStatic)
	//{{AFX_MSG_MAP(CJWListBmpVScrollbar)
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJWListBmpVScrollbar message handlers

BOOL CJWListBmpVScrollbar::OnEraseBkgnd(CDC* pDC) 
{
	return CStatic::OnEraseBkgnd(pDC);
}

void CJWListBmpVScrollbar::OnLButtonDown(UINT nFlags, CPoint point) 
{
	SetCapture();
	CRect clientRect;
	GetClientRect(&clientRect);

	CRect rc;
	pList->GetClientRect( &rc );
	pList->InvalidateRect(&rc, FALSE); 
	
	int nHeight = clientRect.Height() - (m_downArrowHeight-1)/*25*/;
	

	CRect rectUpArrow(0,0/*11*/,m_upArrowWidth/*12*/,m_upArrowHeight/*37*/);
	CRect rectDownArrow(0,nHeight,m_downArrowWidth/*12*/,nHeight + m_downArrowHeight/*26*/);
	CRect rectThumb(0,nThumbTop,m_thumbWidth/*12*/,nThumbTop + m_thumbHeight/*26*/);

	if(rectThumb.PtInRect(point))
	{
		bMouseDown = true;
	}

	if(rectDownArrow.PtInRect(point))
	{
		bMouseDownArrowDown = true;
		SetTimer(2,250,NULL);
	}

	if(rectUpArrow.PtInRect(point))
	{
		bMouseDownArrowUp = true;
		SetTimer(2,250,NULL);
	}
	
	CStatic::OnLButtonDown(nFlags, point);
}

void CJWListBmpVScrollbar::OnLButtonUp(UINT nFlags, CPoint point) 
{
	UpdateThumbPosition();
	KillTimer(1);
	ReleaseCapture();
	
	bool bInChannel = true;

	CRect clientRect;
	GetClientRect(&clientRect);

	int nHeight = clientRect.Height() - (m_downArrowHeight-1)/*25*/;
	CRect rectUpArrow(0,0/*11*/,m_upArrowWidth/*12*/,m_upArrowHeight/*37*/);
	CRect rectDownArrow(0,nHeight,m_downArrowWidth/*12*/,nHeight + m_downArrowHeight/*26*/);
	CRect rectThumb(0,nThumbTop,m_thumbWidth/*12*/,nThumbTop + m_thumbHeight/*26*/);

	if(rectUpArrow.PtInRect(point) && bMouseDownArrowUp)
	{
		ScrollUp();	
		bInChannel = false;
	}

	if(rectDownArrow.PtInRect(point) && bMouseDownArrowDown)
	{
		ScrollDown();
		bInChannel = false;
	}

	if(rectThumb.PtInRect(point))
	{
		bInChannel = false;
	}

	if(bInChannel == true && !bMouseDown)
	{
		if(point.y > nThumbTop)
		{
			PageDown();
		}
		else
		{
			PageUp();
		}
	}

	bMouseDown = false;
	bDragging = false;
	bMouseDownArrowUp = false;
	bMouseDownArrowDown = false; 
	
	CStatic::OnLButtonUp(nFlags, point);
}

void CJWListBmpVScrollbar::OnMouseMove(UINT nFlags, CPoint point) 
{
 	CRect clientRect;
	GetClientRect(&clientRect);

	CRect rc;
	pList->GetClientRect( &rc );
	pList->InvalidateRect(&rc, FALSE); 

	SetRedraw( FALSE );
	if(bMouseDown)
	{
 		// 当前滚动条应该滑动到哪个位置。
		nThumbTop = point.y - m_thumbHeight/2/*13*/; //-13 so mouse is in middle of thumb

		double nMax = pList->GetScrollLimit(SB_VERT);
		// double nHeight = clientRect.Height();
		int nPos = pList->GetScrollPos(SB_VERT);

	 	// 除去两头多于区域,滚动槽的长度。98=(13+36)*2
		double nHeight = clientRect.Height()-(m_downArrowHeight+m_upArrowHeight+m_thumbHeight-4)/*74*/;
		double nVar = nMax;
		// 滑块的运动范围 / 整个的list的的高度
		dbThumbInterval = nHeight/nVar;

		// 计算当前位置
		int nScrollLenth = (int)((nThumbTop-(m_downArrowHeight-2)/*24*/)/dbThumbInterval)-nPos;
	
		LimitThumbPosition();
		Draw();

		CSize size;
		size.cx = 0;
 		size.cy = nScrollLenth ; 

		pList->Scroll(size);
	}
	SetRedraw( TRUE );
	CStatic::OnMouseMove(nFlags, point);
}

void CJWListBmpVScrollbar::OnPaint() 
{
	CPaintDC dc(this); 
	
	Draw();
}

void CJWListBmpVScrollbar::OnTimer(UINT nIDEvent) 
{
	if(nIDEvent == 1)
	{
		if(bMouseDownArrowDown)
		{
			ScrollDown();
		}
		
		if(bMouseDownArrowUp)
		{
			ScrollUp();
		}
	}
	else if(nIDEvent == 2)
	{
		if(bMouseDownArrowDown)
		{
			KillTimer(2);
			SetTimer(1, 50, NULL);
		}
		
		if(bMouseDownArrowUp)
		{
			KillTimer(2);
			SetTimer(1, 50, NULL);
		}
	}
	CStatic::OnTimer(nIDEvent);
}

void CJWListBmpVScrollbar::PageDown()
{
 	pList->SendMessage(WM_VSCROLL, MAKELONG(SB_PAGEDOWN,0),NULL);
 	UpdateThumbPosition();
}

void CJWListBmpVScrollbar::PageUp()
{
	pList->SendMessage(WM_VSCROLL, MAKELONG(SB_PAGEUP,0),NULL);
	UpdateThumbPosition();
}

void CJWListBmpVScrollbar::ScrollUp()
{
	pList->SendMessage(WM_VSCROLL, MAKELONG(SB_LINEUP,0),NULL);
	UpdateThumbPosition();
}

void CJWListBmpVScrollbar::ScrollDown()
{
	pList->SendMessage(WM_VSCROLL, MAKELONG(SB_LINEDOWN,0),NULL);
	UpdateThumbPosition();
}

void CJWListBmpVScrollbar::UpdateThumbPosition()
{
	CRect clientRect;
	GetClientRect(&clientRect);

	double nPos = pList->GetScrollPos(SB_VERT);
	double nMax = pList->GetScrollLimit(SB_VERT);
	double nHeight = (clientRect.Height()-(m_thumbHeight+m_downArrowHeight+m_upArrowHeight-4)/*74*/);
	double nVar = nMax;

	dbThumbInterval = nHeight/nVar;

	double nNewdbValue = (dbThumbInterval * nPos);
	int nNewValue = (int)nNewdbValue;


	nThumbTop = m_downArrowHeight-2/*24*/+nNewValue;

	LimitThumbPosition();

	Draw();
}


void CJWListBmpVScrollbar::Draw()
{
	CClientDC dc(this);
	CRect clientRect;
	GetClientRect(&clientRect);
	CMemDC memDC(&dc, &clientRect);
	memDC.FillSolidRect(&clientRect,  RGB(74,82,107));
	CDC bitmapDC;
	bitmapDC.CreateCompatibleDC(&dc);
	
	CBitmap* pOldBitmap;
	BITMAP  TempBmp;

	//draw the up arrow of the scrollbar
	pOldBitmap = bitmapDC.SelectObject(&m_UpArrowbitmap);
 	m_UpArrowbitmap.GetBitmap( &TempBmp );
	m_upArrowWidth  = TempBmp.bmWidth;
	m_upArrowHeight = TempBmp.bmHeight;
	if(bMouseDownArrowUp)
		memDC.BitBlt(clientRect.left,clientRect.top, m_upArrowWidth /*12*/, m_upArrowHeight/*26*/,&bitmapDC,12,0,SRCCOPY);
	else
		memDC.BitBlt(clientRect.left,clientRect.top, m_upArrowWidth /*12*/, m_upArrowHeight/*26*/,&bitmapDC,0,0,SRCCOPY);
	
	bitmapDC.SelectObject(pOldBitmap);
	pOldBitmap = NULL;
	
	//draw the background (span)
	pOldBitmap = bitmapDC.SelectObject(&m_Spanbitmap);
	memDC.StretchBlt(clientRect.left, clientRect.top+m_upArrowHeight/*26*/, m_upArrowWidth /*12*/,
		clientRect.Height()-2*m_upArrowHeight/*nHeight+12*/,&bitmapDC, 0,0, m_upArrowWidth /*12*/, 1, SRCCOPY);
	bitmapDC.SelectObject(pOldBitmap);
	pOldBitmap = NULL;

	//draw the down arrow of the scrollbar
	pOldBitmap = bitmapDC.SelectObject(&m_DownArrowbitmap);
	m_DownArrowbitmap.GetBitmap( &TempBmp );
	m_downArrowWidth  = TempBmp.bmWidth;
	m_downArrowHeight = TempBmp.bmHeight;
	memDC.BitBlt(clientRect.left,clientRect.Height()-m_upArrowHeight/*nHeight+12*/,m_upArrowWidth /*12*/,
				 m_upArrowHeight/*26*/ ,&bitmapDC,0,0,SRCCOPY);
	bitmapDC.SelectObject(pOldBitmap);
	pOldBitmap = NULL;

	//If there is nothing to scroll then don't
	//show the thumb control otherwise show it

	if(pList->GetScrollLimit(SB_VERT) != 0)
	{
		//draw the thumb control
		pOldBitmap = bitmapDC.SelectObject(&m_Thumbbitmap);
		m_Thumbbitmap.GetBitmap( &TempBmp );

		m_thumbWidth  = TempBmp.bmWidth;
		m_thumbHeight  = TempBmp.bmHeight;
 
		memDC.BitBlt(clientRect.left, clientRect.top+nThumbTop, m_thumbWidth/*12*/, m_thumbHeight/*26*/, &bitmapDC,0,0,SRCCOPY);
		bitmapDC.SelectObject(pOldBitmap);
		pOldBitmap = NULL;
	}
}

void CJWListBmpVScrollbar::LimitThumbPosition()
{
	CRect clientRect;
	GetClientRect(&clientRect);

	if(nThumbTop + m_thumbHeight/*26*/ > clientRect.Height() - m_upArrowHeight+1 ) //(clientRect.Height()-25))
	{
		nThumbTop = clientRect.Height()-m_thumbHeight-m_upArrowHeight+2;//50;
	}

	if(nThumbTop < (clientRect.top+m_downArrowHeight-1))
	{
		nThumbTop = clientRect.top+m_downArrowHeight-1;
	}
}


BOOL CJWListBmpVScrollbar::LoadVScrollBmp(UINT nUpArrowBmp, UINT	nSpanBmp, UINT nDownArrowBmp, UINT nThumbBmp)
{
	m_UpArrowbitmap.DeleteObject();
	m_UpArrowbitmap.LoadBitmap( nUpArrowBmp ); //IDB_VERTICLE_SCROLLBAR_UPARROW

 	m_Spanbitmap.DeleteObject();
 	m_Spanbitmap.LoadBitmap( nSpanBmp ); //IDB_VERTICLE_SCROLLBAR_SPAN

	m_DownArrowbitmap.DeleteObject();
	m_DownArrowbitmap.LoadBitmap( nDownArrowBmp ); //IDB_VERTICLE_SCROLLBAR_DOWNARROW

	m_Thumbbitmap.DeleteObject();
	m_Thumbbitmap.LoadBitmap( nThumbBmp ); //IDB_VERTICLE_SCROLLBAR_THUMB2

	return TRUE;
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -