首页 > 代码库 > C++ Code_ScrollBar

C++ Code_ScrollBar

主题 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1.  ScrollBar的使用

2.  

3.  

4.  

5.  

    

  属性

HScrollBar

VScrollBar

直接拖拽1其中任意空间到对话框上面是,你一拖拽滚动条,它立即回到原始位置

    
    

代码::

  

/*

在控件上面添加1个HScrollBar和1个Edit控件

 

*/

//初始化部分添加代码

    // TODO: Add extra initialization here
    CScrollBar *pScroll=(CScrollBar*)GetDlgItem(IDC_SCROLLBAR1);
    pScroll->SetScrollRange(0, 100);
    pScroll->SetScrollPos(0);

    SetDlgItemInt(IDC_EDIT1, 0);

 

//为对话杠添加1个OnHScroll消息,添加如下代码

void CProject01Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
    int iPos=pScrollBar->GetScrollPos();
    switch (nSBCode)
    {
    case SB_LINERIGHT:
            iPos+=1;
            break;
    case SB_LINELEFT:
            iPos-=1;
            break;
    case SB_PAGERIGHT:
            iPos+=10;
            break;
    case SB_PAGELEFT:
            iPos-=10;
            break;
    case SB_THUMBTRACK:
            iPos=nPos;
            break;
    default:
        break;
    }
    if (iPos<0) iPos=0;
    if (iPos>100) iPos=100;
    pScrollBar->SetScrollPos(iPos);
    SetDlgItemInt(IDC_EDIT1, iPos);
    
    CDialog::OnHScroll(nSBCode, nPos, pScrollBar);

}

 

//为Edit1添加OnChange消息

void CProject01Dlg::OnChangeEdit1()
{
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.
    CString STR;
    GetDlgItemText(IDC_EDIT1, STR);
    STR.TrimLeft();
    STR.TrimRight();
    INT iPos=0;
    if (STR!="-" && STR!="")
    {
        if (!UpdateData())
        {
            return;
        }
        iPos=m_nEdt1;
    }
    CScrollBar *pScroll=(CScrollBar*)GetDlgItem(IDC_SCROLLBAR1);
    pScroll->SetScrollPos(iPos);
    
    // TODO: Add your control notification handler code here
    
}
 
    

效果图:

  



来自为知笔记(Wiz)


附件列表