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

📄 scrollbar.cpp

📁 3D游戏展示程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

void GScrollBar::Render()
{
	if(m_dwAttrib & GUI_VISIBLE)
	{
		g_pGraphics->RenderSprite(m_hBtnMin, m_ptMin.x, m_ptMin.y, &m_rcMin[m_MinState], m_dwColor);
		g_pGraphics->RenderSprite(m_hBtnMax, m_ptMax.x, m_ptMax.y, &m_rcMax[m_MaxState], m_dwColor);
		if(m_bShowSlider)
		{
			g_pGraphics->RenderSprite(m_hBtnSlider, &m_DstButton, &m_rcButton, m_dwColor);
		}
	}
}

void GScrollBar::OffSet(int x, int y)
{
	CWindow::OffSet(x, y);
	UpdateRect();
	UpdateBtnRect();
}

void GScrollBar::Enable()
{
	if(!(m_dwAttrib & GUI_ENABLE))
	{
		m_MinState = BTN_NORMAL;
		m_MaxState = BTN_NORMAL;
		m_dwAttrib |= GUI_ENABLE;
	}
}

void GScrollBar::Disable()
{
	if(m_dwAttrib & GUI_ENABLE)
	{
		m_MinState = BTN_DISABLE;
		m_MaxState = BTN_DISABLE;
		m_dwAttrib &= ~GUI_ENABLE;
	}
}

void GScrollBar::Show(bool c)
{
	CWindow::Show(c);
	if(m_dwAttrib & GUI_ENABLE)
	{
		m_MinState = BTN_NORMAL;
		m_MaxState = BTN_NORMAL;
	}
	else
	{
		m_MinState = BTN_DISABLE;
		m_MaxState = BTN_DISABLE;
	}
}

//-------------------------------------------------
// 设置滑动条的范围
//-------------------------------------------------
void GScrollBar::SetTrackRange(int nStart, int nEnd)
{
	m_nStart = nStart; 
	m_nEnd = nEnd;
	AdjustPosition();
	UpdateBtnRect();
}

//-------------------------------------------------
// 设置滑动条的位置
//-------------------------------------------------
void GScrollBar::SetTrackPos(int nPosition) 
{ 
	m_nPosition = nPosition; 
	AdjustPosition(); 
	UpdateBtnRect(); 
}

//-------------------------------------------------
// 设置每页的行数
//-------------------------------------------------
void GScrollBar::SetPageSize(int nPageSize)
{ 
	m_nPageSize = nPageSize; 
	AdjustPosition(); 
	UpdateBtnRect(); 
}

//-------------------------------------------------
// 移动滑块
//-------------------------------------------------
void GScrollBar::Scroll(int nDelta)
{
	m_nPosition += nDelta;
	AdjustPosition();
	UpdateBtnRect();
}

//-------------------------------------------------
// 显示第N行
//-------------------------------------------------
void GScrollBar::ShowItem(int nIndex)
{
	if(nIndex < 0)
		nIndex = 0;

	if(nIndex >= m_nEnd)
		nIndex = m_nEnd - 1;

	if(m_nPosition > nIndex)
		m_nPosition = nIndex;
	else if(m_nPosition + m_nPageSize <= nIndex)
	{
		m_nPosition = nIndex - m_nPageSize + 1;
	}

	UpdateBtnRect();
}

//-------------------------------------------------
// 设置控件的宽高(包括按钮)
//-------------------------------------------------
void GScrollBar::SetSize(int width, int height)
{
	if(m_SliderType)
	{
		m_rcSrc.right = m_rcSrc.left+width;
		m_rcSrc.bottom = m_rcSrc.top+height-m_rcMin[BTN_NORMAL].Height()-m_rcMax[BTN_NORMAL].Height();
	}
	else
	{
		m_rcSrc.right = m_rcSrc.left+width-m_rcMin[BTN_NORMAL].Width()-m_rcMax[BTN_NORMAL].Width();
		m_rcSrc.bottom = m_rcSrc.top+height;
	}
	UpdateRect();
	UpdateBtnRect();
}

//-------------------------------------------------
// 修正position在有效的位置
//-------------------------------------------------
void GScrollBar::AdjustPosition()
{
	if(m_nPosition < m_nStart || m_nEnd-m_nStart <= m_nPageSize)
	{
		m_nPosition = m_nStart;
	}
	else if(m_nPosition + m_nPageSize > m_nEnd)
	{
		m_nPosition = m_nEnd - m_nPageSize;
	}
}

//-------------------------------------------------
// 设置两按钮的位置
//-------------------------------------------------
void GScrollBar::UpdateRect()
{
	if(m_SliderType)	// 垂直
	{
		m_ptMin.x = m_ptPos.x+m_rcSrc.Width()/2-m_rcMin[BTN_NORMAL].Width()/2;
		m_ptMin.y = m_ptPos.y-m_rcMin[BTN_NORMAL].Height();

		m_ptMax.x = m_ptPos.x+m_rcSrc.Width()/2-m_rcMin[BTN_NORMAL].Width()/2;
		m_ptMax.y = m_ptPos.y+m_rcSrc.Height();
	}
	else				// 水平
	{
		m_ptMin.x = m_ptPos.x-m_rcMin[BTN_NORMAL].Width();
		m_ptMin.y = m_ptPos.y+m_rcSrc.Height()/2-m_rcMin[BTN_NORMAL].Height()/2;

		m_ptMax.x = m_ptPos.x+m_rcSrc.Width();
		m_ptMax.y = m_ptPos.y+m_rcSrc.Height()/2-m_rcMin[BTN_NORMAL].Height()/2;
	}
}

//-------------------------------------------------
// 更新滑块的RECT
//-------------------------------------------------
void GScrollBar::UpdateBtnRect()
{
	if(m_nEnd - m_nStart > m_nPageSize)
	{
		// 多页
		int varHeight;
		int nThumbVarHeight;
		int nMaxPosition = m_nEnd - m_nStart - m_nPageSize;

		if(m_SliderType)	// 垂直
		{
			varHeight = m_rcSrc.Height()*m_nPageSize/(m_nEnd-m_nStart);
			nThumbVarHeight = varHeight>SCROLLBAR_MINTHUMBSIZE?varHeight:SCROLLBAR_MINTHUMBSIZE;
			m_DstButton.left = m_ptPos.x+m_rcSrc.Width()/2-m_rcButton.Width()/2;
			m_DstButton.right = m_ptPos.x+m_rcSrc.Width()/2+m_rcButton.Width()/2;
			m_DstButton.top = m_ptPos.y+(m_nPosition-m_nStart)*(m_rcSrc.Height()-nThumbVarHeight)/nMaxPosition;
			m_DstButton.bottom = m_DstButton.top+nThumbVarHeight;
		}
		else
		{
			varHeight = m_rcSrc.Width()*m_nPageSize/(m_nEnd-m_nStart);
			nThumbVarHeight = varHeight>SCROLLBAR_MINTHUMBSIZE?varHeight:SCROLLBAR_MINTHUMBSIZE;
			m_DstButton.top = m_ptPos.y+m_rcSrc.Height()/2-m_rcButton.Height()/2;
			m_DstButton.bottom = m_ptPos.y+m_rcSrc.Height()/2+m_rcButton.Height()/2;
			m_DstButton.left = m_ptPos.x+(m_nPosition-m_nStart)*(m_rcSrc.Width()-nThumbVarHeight)/nMaxPosition;
			m_DstButton.right = m_DstButton.left+nThumbVarHeight;
		}
		m_bShowSlider = true;
	} 
	else
	{
		// 单页
		m_DstButton.bottom = m_DstButton.top;
		m_bShowSlider = false;
	}
}

//-------------------------------------------------
// Cool Scroll
//-------------------------------------------------

CoolScroll::CoolScroll(CWindow* pParent /* = NULL */):GScrollBar(pParent)
{
	m_dwGUIType = GUI_COOLSCROLL;
	m_ButtonOffset = 6;
}

bool CoolScroll::LoadFromIni(char* pfilename, char* pIndex)
{
	return GScrollBar::LoadFromIni(pfilename, pIndex);
}

DWORD CoolScroll::ProcessEvent()
{
	return GScrollBar::ProcessEvent();
}

void CoolScroll::Render()
{
	if(m_dwAttrib & GUI_VISIBLE)
	{
		g_pGraphics->BeginSprite();
		if(m_MinState == BTN_NORMAL || m_MinState == BTN_DISABLE)
		{
			g_pGraphics->RenderSprite(m_hBtnMin, m_ptMin.x, m_ptMin.y, &m_rcMin[m_MinState], m_dwColor);
		}
		else 
		{
			if(m_MinState==BTN_OVER)
				g_pGraphics->RenderSprite(m_hBtnMin, m_ptMin.x, m_ptMin.y, &m_rcMin[BTN_NORMAL], m_dwColor);
			if(m_MinState==BTN_DOWN)
				g_pGraphics->RenderSprite(m_hBtnMin, m_ptMin.x, m_ptMin.y, &m_rcMin[BTN_DOWN], m_dwColor);
			g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
			g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
			g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
			g_pGraphics->RenderSprite(m_hBtnMin, m_ptMin.x, m_ptMin.y, &m_rcMin[BTN_OVER], m_dwColor);
			g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
			g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		}

		if(m_MaxState == BTN_NORMAL || m_MaxState == BTN_DISABLE)
		{
			g_pGraphics->RenderSprite(m_hBtnMax, m_ptMax.x, m_ptMax.y, &m_rcMax[m_MaxState], m_dwColor);
		}
		else 
		{
			if(m_MaxState==BTN_OVER)
				g_pGraphics->RenderSprite(m_hBtnMax, m_ptMax.x, m_ptMax.y, &m_rcMax[BTN_NORMAL], m_dwColor);
			if(m_MaxState==BTN_DOWN)
				g_pGraphics->RenderSprite(m_hBtnMax, m_ptMax.x, m_ptMax.y, &m_rcMax[BTN_DOWN], m_dwColor);
			g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
			g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
			g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
			g_pGraphics->RenderSprite(m_hBtnMax, m_ptMax.x, m_ptMax.y, &m_rcMax[BTN_OVER], m_dwColor);
			g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
			g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		}

		if(m_bShowSlider)
		{
			g_pGraphics->RenderSprite(m_hBtnSlider, &m_DstButton, &m_rcButton, m_dwColor);
		}
		g_pGraphics->EndSprite();
	}
}

void CoolScroll::UpdateRect()
{
	if(m_SliderType)	// 垂直
	{
		m_ptMin.x = m_ptPos.x;
		m_ptMin.y = m_ptPos.y-32+6;

		m_ptMax.x = m_ptPos.x;
		m_ptMax.y = m_ptPos.y+m_rcSrc.Height()-6;
	}
	else				// 水平
	{
		m_ptMin.x = m_ptPos.x-32+6;
		m_ptMin.y = m_ptPos.y;

		m_ptMax.x = m_ptPos.x+m_rcSrc.Width()-6;
		m_ptMax.y = m_ptPos.y;
	}
}

void CoolScroll::UpdateBtnRect()
{
	if(m_nEnd - m_nStart > m_nPageSize)
	{
		// 多页
		int varHeight;
		int nMaxPosition = m_nEnd - m_nStart - m_nPageSize;

		if(m_SliderType)	// 垂直
		{
			varHeight = m_rcSrc.Height()*m_nPageSize/(m_nEnd-m_nStart)-20;
			//nThumbVarHeight = varHeight>SCROLLBAR_MINTHUMBSIZE?varHeight:SCROLLBAR_MINTHUMBSIZE;
			m_DstButton.left = m_ptPos.x;
			m_DstButton.right = m_ptPos.x+32;
			m_DstButton.top = m_ptPos.y-6+(m_nPosition-m_nStart)*(m_rcSrc.Height()-20)/nMaxPosition;
			m_DstButton.bottom = m_DstButton.top+32;
		}
		else
		{
			varHeight = m_rcSrc.Width()*m_nPageSize/(m_nEnd-m_nStart)-20;
			//nThumbVarHeight = varHeight>SCROLLBAR_MINTHUMBSIZE?varHeight:SCROLLBAR_MINTHUMBSIZE;
			m_DstButton.top = m_ptPos.y;
			m_DstButton.bottom = m_ptPos.y+32;
			m_DstButton.left = m_ptPos.x-6+(m_nPosition-m_nStart)*(m_rcSrc.Width()-20)/nMaxPosition;
			m_DstButton.right = m_DstButton.left+32;
		}
		m_bShowSlider = true;
	} 
	else
	{
		// 单页
		m_DstButton.bottom = m_DstButton.top;
		m_bShowSlider = false;
	}
	//GScrollBar::UpdateBtnRect();
}

⌨️ 快捷键说明

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