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

📄 rtscrollbar.cpp

📁 vc++编写的界面源代码,对C++爱好者是很发的帮助。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RTScrollBar.cpp : 实现文件
//

#include "stdafx.h"
#include "RTScrollBar.h"
#include "RTDraw.h"

// CRTScrollBar

IMPLEMENT_DYNAMIC(CRTScrollBar, CScrollBar)
CRTScrollBar::CRTScrollBar()
{
	m_bMouseDown = FALSE;
	m_bMouseDownArrowForback = FALSE;
	m_bMouseDownArrowForwad = FALSE;
	m_bDragging = FALSE;
	m_nThumbStart = 0;
	m_dbThumbRemainder = 0.0f;
	m_dbThumbInterval = 0.0f;
}

CRTScrollBar::~CRTScrollBar()
{
}

CBitmap* CRTScrollBar::m_ScrollBarUpArrowBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
CBitmap* CRTScrollBar::m_ScrollBarLeftArrowBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
CBitmap* CRTScrollBar::m_ScrollBarDownArrowBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
CBitmap* CRTScrollBar::m_ScrollBarRightArrowBitmap[5] = {NULL,NULL,NULL,NULL,NULL};

UINT     CRTScrollBar::m_ScrollBarUpArrowBitmapDrawMode[5] = {0,0,0,0,0};
UINT     CRTScrollBar::m_ScrollBarLeftArrowBitmapDrawMode[5] = {0,0,0,0,0};
UINT     CRTScrollBar::m_ScrollBarDownArrowBitmapDrawMode[5] = {0,0,0,0,0};
UINT     CRTScrollBar::m_ScrollBarRightArrowBitmapDrawMode[5] = {0,0,0,0,0};

CBitmap* CRTScrollBar::m_ScrollBarHorizontalBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
CBitmap* CRTScrollBar::m_ScrollBarVerticalBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
UINT     CRTScrollBar::m_ScrollBarHorizontalBitmapDrawMode[5] = {0,0,0,0,0};
UINT     CRTScrollBar::m_ScrollBarVerticalBitmapDrawMode[5] = {0,0,0,0,0};
BOOL     CRTScrollBar::m_IsEnableSkin = TRUE;

BEGIN_MESSAGE_MAP(CRTScrollBar, CScrollBar)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()



// CRTScrollBar 消息处理程序


void CRTScrollBar::OnPaint()
{
	if(!m_IsEnableSkin)return CScrollBar::OnPaint();

	CPaintDC dc(this); 
	DWORD style = GetStyle();
	if( (style & SBS_VERT) == SBS_VERT)
	{
		DrawVertical(&dc);
	}
	else
	{
		DrawHorizontal(&dc);
	}
}

void CRTScrollBar::OnLButtonDown(UINT nFlags, CPoint point)
{
	if(!m_IsEnableSkin)return CScrollBar::OnLButtonDown(nFlags, point);

	m_MouseDownTime = 0;
	SetCapture();
	DWORD style = GetStyle();
	CWnd *pParent = GetParent();

	if((style & SBS_VERT)==SBS_VERT)
	{
		if(m_ThumbRect.PtInRect(point))
		{
			m_bMouseDown = true;
			m_MouseDownPoint.SetPoint(point.x - m_ThumbRect.left,point.y - m_ThumbRect.top);

			CClientDC dc(this);
			DrawVertical(&dc);
		}
		else if(m_BottomArrowRect.PtInRect(point))
		{
			m_bMouseDownArrowForwad = true;
			pParent->SendMessage(WM_VSCROLL,MAKEWPARAM(SB_LINEDOWN,0),(LPARAM)m_hWnd);
			CClientDC dc(this);
			DrawVertical(&dc);

			SetTimer(2,100,NULL);
		}
		else if(m_TopArrowRect.PtInRect(point))
		{
			m_bMouseDownArrowForback = true;
			pParent->SendMessage(WM_VSCROLL,MAKEWPARAM(SB_LINEUP,0),(LPARAM)m_hWnd);
			CClientDC dc(this);
			DrawVertical(&dc);
			
			SetTimer(2,100,NULL);
		}
		else if(m_AreaRect.PtInRect(point))
		{	
			if(pParent != NULL)
			{
				SCROLLINFO sif;
				sif.cbSize = sizeof(sif);
				GetScrollInfo(&sif);
			
				if(point.y < m_ThumbRect.top)
				{
					m_bMouseDownArrowForback = TRUE;
					pParent->SendMessage(WM_VSCROLL,MAKEWPARAM(SB_PAGEUP,0),(LPARAM)m_hWnd);
				}
				else
				{
					m_bMouseDownArrowForwad = TRUE;
					pParent->SendMessage(WM_VSCROLL,MAKEWPARAM(SB_PAGEDOWN,0),(LPARAM)m_hWnd);
				}
			}
			
			SetTimer(1,100,NULL);
		}
	}
	else
	{
		if(m_ThumbRect.PtInRect(point))
		{
			m_bMouseDown = true;
			m_MouseDownPoint.SetPoint(point.x - m_ThumbRect.left,point.y - m_ThumbRect.top);
		
			CClientDC dc(this);
			DrawHorizontal(&dc);
		}
		else if(m_RightArrowRect.PtInRect(point))
		{
			m_bMouseDownArrowForwad = true;
			pParent->SendMessage(WM_HSCROLL,MAKEWPARAM(SB_LINERIGHT,0),(LPARAM)m_hWnd);
			CClientDC dc(this);
			DrawHorizontal(&dc);
			
			SetTimer(2,100,NULL);
		}
		else if(m_LeftArrowRect.PtInRect(point))
		{
			m_bMouseDownArrowForback = true;
			pParent->SendMessage(WM_HSCROLL,MAKEWPARAM(SB_LINELEFT,0),(LPARAM)m_hWnd);
			CClientDC dc(this);
			DrawHorizontal(&dc);
			
			SetTimer(2,100,NULL);
		}
		else if(m_AreaRect.PtInRect(point))
		{
			if(pParent != NULL)
			{
				SCROLLINFO sif;
				ZeroMemory(&sif,sizeof(sif));
				sif.cbSize = sizeof(sif);
				GetScrollInfo(&sif,SIF_PAGE|SIF_RANGE|SIF_POS|SIF_TRACKPOS);

				if(point.x < m_ThumbRect.left)
				{
					m_bMouseDownArrowForback = TRUE;
					pParent->SendMessage(WM_HSCROLL,MAKEWPARAM(SB_PAGELEFT,0),(LPARAM)m_hWnd);
				}
				else
				{
					m_bMouseDownArrowForwad = TRUE;
					pParent->SendMessage(WM_HSCROLL,MAKEWPARAM(SB_PAGERIGHT,0),(LPARAM)m_hWnd);
				}
			}

			SetTimer(1,100,NULL);
		}
	}
}

void CRTScrollBar::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(!m_IsEnableSkin)return CScrollBar::OnLButtonUp(nFlags, point);
	ReleaseCapture();
	
	DWORD style = GetStyle();
	CWnd *pParent = GetParent();

	if(m_bDragging)
	{
		m_bDragging = FALSE;
		CPoint mousePoint(m_ThumbRect.left + m_MouseDownPoint.x,m_ThumbRect.top + m_MouseDownPoint.y);
		if( (style & SBS_VERT) == SBS_VERT)
		{
			if(mousePoint.y != point.y)
			{
				int move = point.y - mousePoint.y;
				double pos = m_ThumbRect.top + move - m_AreaRect.top;
				double height = m_AreaRect.Height() - m_ThumbRect.Height();
				double per = pos / height;
				SCROLLINFO sif;
				GetScrollInfo(&sif);
				int newpos = (int)((sif.nMax - sif.nMin) * per);
				if(newpos < sif.nMin)newpos = sif.nMin;
				if(newpos > sif.nMax)newpos = sif.nMax;
				if(pParent != NULL)
				{
					pParent->SendMessage(WM_VSCROLL,MAKEWPARAM(SB_THUMBPOSITION,newpos),(LPARAM)m_hWnd);
				}
			}
		}
		else
		{
			if(mousePoint.x == point.x)
			{
				int move = point.x - mousePoint.x;
				double pos = m_ThumbRect.left + move - m_AreaRect.left;
				double width = m_AreaRect.Width() - m_ThumbRect.Width();
				double per = pos / width;
				SCROLLINFO sif;
				GetScrollInfo(&sif);
				int newpos = (int)((sif.nMax - sif.nMin) * per);
				if(newpos < sif.nMin)newpos = sif.nMin;
				if(newpos > sif.nMax)newpos = sif.nMax;
				if(pParent != NULL)
				{
					pParent->SendMessage(WM_HSCROLL,MAKEWPARAM(SB_THUMBPOSITION,newpos),(LPARAM)m_hWnd);
				}
			}
		}
	}
	
	if(m_bMouseDown || m_bMouseDownArrowForback || m_bMouseDownArrowForwad)
	{
		m_bMouseDown = FALSE;
		m_bMouseDownArrowForback = FALSE;
		m_bMouseDownArrowForwad = FALSE;
        KillTimer(2);
		KillTimer(1);
		CClientDC dc(this);
		if((style & SBS_VERT) == SBS_VERT)
			DrawVertical(&dc);
		else
			DrawHorizontal(&dc);
	}
}

void CRTScrollBar::OnMouseMove(UINT nFlags, CPoint point)
{
	if(!m_IsEnableSkin)return CScrollBar::OnMouseMove(nFlags, point);
    
	CWnd *pParent = GetParent();
	if(pParent == NULL)return;

	if(m_bMouseDown)m_bDragging = TRUE;
	if(!m_bDragging)return;

	CPoint mousePoint(m_ThumbRect.left + m_MouseDownPoint.x,m_ThumbRect.top + m_MouseDownPoint.y);
	DWORD style = GetStyle();

	if( (style & SBS_VERT) == SBS_VERT)
	{
		if(mousePoint.y == point.y)return;

		int move = point.y - mousePoint.y;
		double pos = m_ThumbRect.top + move - m_AreaRect.top;
		double height = m_AreaRect.Height() - m_ThumbRect.Height();
		double per = pos / height;
		SCROLLINFO sif;
		GetScrollInfo(&sif);
		int newpos = (int)((sif.nMax - sif.nMin) * per);
		if(newpos < sif.nMin) newpos = sif.nMin;
		if(newpos > sif.nMax) newpos = sif.nMax;
		sif.nTrackPos = newpos;
		if(m_nThumbTrackPos != newpos)
		{
			m_nThumbTrackPos = newpos;
			CClientDC dc(this);
			DrawVertical(&dc);
			pParent->SendMessage(WM_VSCROLL,MAKEWPARAM(SB_THUMBTRACK,sif.nTrackPos),(LPARAM)m_hWnd);
		}
	}
	else
	{
		if(mousePoint.x == point.x)return;

		int move = point.x - mousePoint.x;
		double pos = m_ThumbRect.left + move - m_AreaRect.left;
		double width = m_AreaRect.Width() - m_ThumbRect.Width();
		double per = pos / width;
		SCROLLINFO sif;
		GetScrollInfo(&sif);
		int newpos = (int)((sif.nMax - sif.nMin) * per);
		if(newpos < sif.nMin) newpos = sif.nMin;
		if(newpos > sif.nMax) newpos = sif.nMax;
		sif.nTrackPos = newpos;

		if(m_nThumbTrackPos != newpos)
		{
			m_nThumbTrackPos = newpos;
			CClientDC dc(this);
			DrawHorizontal(&dc);
			pParent->SendMessage(WM_HSCROLL,MAKEWPARAM(SB_THUMBTRACK,sif.nTrackPos),(LPARAM)m_hWnd);
		}
	}
}

void CRTScrollBar::OnTimer(UINT nIDEvent)
{
	if(!m_IsEnableSkin)return CScrollBar::OnTimer(nIDEvent);
	
	
	if(m_MouseDownTime < 3)
	{
		m_MouseDownTime++;
		return;
	}
	CWnd* pParent = GetParent();

	DWORD style = GetStyle();
	SCROLLINFO sif;
	GetScrollInfo(&sif);
	if( (style & SBS_VERT) == SBS_VERT)
	{
		if(nIDEvent == 2)

⌨️ 快捷键说明

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