演示垂直滚动条CScrollbar控件的使用

/*
Title:演示垂直滚动条CScrollbar控件的使用
Description: OnVScroll demo
Environment: VS2010SP1
Author:kagula
Date:2015-8-11
*/

//set scrollbar knob position
void CtestScrollDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	int nMax;
	int nMin;
	pScrollBar->GetScrollRange(&nMin,&nMax);

	int currentPos = pScrollBar->GetScrollPos();  
	int stride = (nMax-nMin)/10;

#ifdef _DEBUG
	TRACE(L"pScrollBar->GetScrollPos()=%d\n",currentPos);
#endif

	switch(nSBCode)
	{
	case SB_THUMBPOSITION://Drop knob
	case SB_THUMBTRACK://Drag knob
		pScrollBar->SetScrollPos(nPos);
		break;
	case SB_LINEUP: //click up arrow 
	case SB_PAGEUP: //stroke page up in keyboard
		if((currentPos-stride)>nMin)
			currentPos-=stride;
		else
			currentPos=nMin;
		pScrollBar->SetScrollPos(currentPos);
		break;
	case SB_LINEDOWN:  //click down arrow
	case SB_PAGEDOWN: //stroke page down in keyboard
		if((currentPos+stride)<nMax)
			currentPos+=stride;
		else
			currentPos=nMax;
		pScrollBar->SetScrollPos(currentPos);
		break;
	} 

	CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}

//create scrollbar controller
void CtestScrollDlg::OnBnClickedBtnAdd()
{
	if (m_pScrollbar==nullptr)
	{
		CRect rect;
		GetClientRect(rect);
		rect.left = rect.right - GetSystemMetrics(SM_CXVSCROLL);

		m_pScrollbar = new CScrollBar;
		m_pScrollbar->Create(WS_VISIBLE|SB_VERT,rect,this,IDC_SCROLLBAR1);
		m_pScrollbar->SetScrollRange(0,100);
	}
}

//release scrollbar controller
void CtestScrollDlg::OnBnClickedBtnDel()
{
	if (m_pScrollbar)
	{
		m_pScrollbar->DestroyWindow();
		delete m_pScrollbar;
		m_pScrollbar = nullptr;
	}
}


void CtestScrollDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	OnBnClickedBtnDel();
}



你可能感兴趣的:(演示垂直滚动条CScrollbar控件的使用)