VC/MFC 滑动条Slider的用法


 在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 );


实例:

在对话框中放一个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 即为当前滑块的值。