在MFC中滑动条(CSliderCtrl)是个常用的控件,用法如下:
主要要方法有:
1、设置、取得滑动范围:
void SetRange( int nMin, int nMax, BOOL bRedraw = FALSE );
void GetRange( int& nMin, int& nMax ) const;
2、设置、取得按下左右箭头滑动间隔:
int SetLineSize( int nSize );
int GetLineSize( ) const;
3、设置、取得按下PgUp、PgDown时滑动间隔:
int SetPageSize( int nSize );
int GetPageSize( ) const;
4、设置、取得滑块位置:
void SetPos( int nPos );
int GetPos( ) const;
5、设置滑动条刻度的频度:
void SetTicFreq( int nFreq ); //将Tick marks和Auto ticks两个属性同时选中才能看到刻度,在下拉框中可以选择刻度的显示位置
实例:
在对话框中放一个Slider控件,添加相应的Ctrl型变量为m_slider。在对话框初始化函数OnInitDialog()中添加:
BOOL CDlgSetup::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_slider.SetRang(0,100);//设置滑动范围
m_slider.SetTicFreq(10);//每10个单位画一刻度
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Slider控件本身并没有响应滑动的消息函数,但可以通过主窗体的OnHScroll()响应。在类向导中为对话框添加WM_HSCROLL消息,在响应函数中添加:
void CDlgSetup::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CSliderCtrl *pSlidCtrl=(CSliderCtrl*)GetDlgItem(IDC_SLIDER1);
m_int=pSlidCtrlHue->GetPos();//取得当前位置值
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
//m_int 即为当前滑块的值。
当有多个slider控件时,我的处理方法,自认为还是比较巧妙的
void CCameraParamDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
UINT* pD;
CSliderCtrl* pS = (CSliderCtrl*)pScrollBar;
if((void*)pScrollBar == (void*)&m_ctrH)
pD = &m_H;
else if((void*)pScrollBar == (void*)&m_ctrV)
pD = &m_V;
else if((void*)pScrollBar == (void*)&m_ctrNx)
pD = &m_nX;
else if((void*)pScrollBar == (void*)&m_ctrNy)
pD = &m_nY;
else if((void*)pScrollBar == (void*)&m_ctrNz)
pD = &m_nZ;
else if((void*)pScrollBar == (void*)&m_ctrUx)
pD = &m_uX;
else if((void*)pScrollBar == (void*)&m_ctrUy)
pD = &m_uY;
else if((void*)pScrollBar == (void*)&m_ctrUz)
pD = &m_uZ;
*pD = pS->GetPos();
UpdateData(FALSE);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}