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

📄 scrollstate.cpp

📁 上传的是有关mfc的详细介绍
💻 CPP
字号:
// ScrollState.cpp: implementation of the ScrollState class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ScrollState.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ScrollState::ScrollState()
{
	m_nPageCount = 0;
	m_nPageIndex = 0;
	m_nTotal=0;
	m_nRows = 0;
	m_hWnd = NULL;

	
	m_lBarTop		= 0;
	m_lBarLeft		= 0;
	m_lBarBottom	= 0;
	m_lBarRight		= 0;
}

ScrollState::~ScrollState()
{
	m_hWnd = NULL;
}

long ScrollState::OnInitScrollState(int nRecordCount, int nCount)
{
	if (nCount < 1 || nRecordCount < 0)
	{
		return -1;
	}

	this->m_nPageCount		= 0;
	this->m_nPageIndex		= 0;
	this->m_nTotal			= 0;
	this->m_nRows			= 0;

	//计算总共有多少页,记录数不足一页的当一页处理
	this->m_nTotal = nRecordCount;
	this->m_nRows = nCount;
	int nTemp = 0;
	if ((this->m_nTotal % this->m_nRows) > 0)
	{
		nTemp = this->m_nRows - (this->m_nTotal % this->m_nRows);
	}
	this->m_nPageCount = (this->m_nTotal + nTemp) / this->m_nRows;

	RECT rc;
	rc.top		= this->m_lBarTop;
	rc.left		= this->m_lBarLeft;
	rc.bottom	= this->m_lBarBottom;
	rc.right	= this->m_lBarRight;

	//滑块的高度
	if (this->m_nPageCount > 1)
	{
		int nHight = rc.bottom - rc.top;
		if (nHight % this->m_nPageCount)
		{
			nTemp = this->m_nPageCount - (nHight % this->m_nPageCount);	
		}
		int iRectHight = (rc.bottom - rc.top + nTemp) / this->m_nPageCount;

		rc.bottom	= iRectHight;
		rc.right	= rc.right - rc.left;
	}
	else
	{
		rc.bottom	= rc.bottom - rc.top;
		rc.right	= rc.right - rc.left;
	}

	//改变滑块的大小,注意两个函数的rc的不同
	MoveWindow(this->m_hWnd, rc.left, rc.top, rc.right, rc.bottom, true);

	//将计算出来的位置和高度宽度记录下来
	this->m_lBarHight = rc.bottom;
	this->m_lBarWidth = rc.right;

	return this->m_nPageCount;
}

void ScrollState::UpdataScrollPosition(long lPage)
{
	//算法1
	RECT rc;
	//判断底部是否超出滑块原始大小
	long lBottom = this->m_lBarTop + ((lPage + 1) * this->m_lBarHight);
	//主要更新一下top就可以了
	if (lBottom > this->m_lBarBottom || lPage == (this->m_nPageCount -1) )
	{
		rc.top		= this->m_lBarBottom - this->m_lBarHight;//rc.top - (lBottom - m_lBarBottom + 1 );
	}
	else
	{
		rc.top	= this->m_lBarTop + (lPage * this->m_lBarHight);
	}

	rc.bottom	= this->m_lBarHight;
	rc.left		= this->m_lBarLeft;
	rc.right	= this->m_lBarWidth;
	MoveWindow(this->m_hWnd, rc.left, rc.top, rc.right, rc.bottom, true);
}

void ScrollState::OnSetSize(HWND hWnd, HWND hForm)
{
	if (m_lBarTop != 0 && m_lBarLeft != 0 &&
		m_lBarBottom != 0 && m_lBarRight != 0)
	{
		return;
	}

	int width = GetSystemMetrics(SM_CYBORDER)*3;
	int nTitleHight = GetSystemMetrics(SM_CYCAPTION) + width;
	RECT rc;

	//得到滑块原始位置
	GetWindowRect(hWnd,&rc);
	RECT rc2;
	GetWindowRect(hForm,&rc2);
	m_lBarTop		= rc.top - rc2.top - nTitleHight;
	m_lBarLeft		= rc.left - rc2.left - width;
	m_lBarBottom	= rc.bottom - rc2.top - nTitleHight;
	m_lBarRight		= rc.right - rc2.left - width;

	//把句柄存起来,其它方法就不用传句柄进来了
	m_hWnd = hWnd;
}

bool ScrollState::OnPageUp(int& nRecordIndex)
{	
	if (nRecordIndex < 0 || nRecordIndex > m_nTotal)
	{
		return false;
	}

	if (this->m_nPageIndex > 0)
	{
		//向上翻
		this->m_nPageIndex--;
		int nTemp = this->m_nTotal % this->m_nRows;

		if (nRecordIndex == this->m_nTotal && nTemp != 0 )
		{
			nRecordIndex -= (nTemp + this->m_nRows);
		}
		else
		{
			nRecordIndex -= 2*this->m_nRows; 
		}

		if (nRecordIndex >= 0)
		{			
			this->UpdataScrollPosition(this->m_nPageIndex);
			return true;
		}
	}
	return false;
}

bool ScrollState::OnPageDown(int nRecordIndex)
{
	if (this->m_nPageIndex < this->m_nPageCount - 1 )
	{
		this->m_nPageIndex++;
		this->UpdataScrollPosition(this->m_nPageIndex);
		return true;
	}

	return false;
}

⌨️ 快捷键说明

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