各位高手晚上好!问题如下:
新建了基于对话框的程序,将Edit控件和Spin控件拖到对话框中。然后选中Spin控件的Auto buddy和Set buddy integer。在OnInitDialog函数中对Spin控件初始化:
CSpinButtonCtrl *pSpin = (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN1);
pSpin->SetRange(0,200);
pSpin->GetBuddy()->SetWindowText("0.0");
然后在OnVScroll函数中加入如下代码:
if(pScrollBar->GetDlgCtrlID() == IDC_SPIN1)
{
   CString   strValue;     
   strValue.Format("%3.1f",   (double)   nPos/10 );     
   ((CSpinButtonCtrl*)   pScrollBar)->GetBuddy()->SetWindowText(strValue); 
}
这样,实现Spin控件的增减量为0.1。如果只是点击Spin控件的上下箭头,一切正常。如果在Edit控件中输入一个数,比如0.3,增减不能达到预期效果。我试了以下,如果一开始输入0.3,那么按“增加”箭头会显示0.1;如果编辑框中有数字,输入0.3,按“增加”箭头会显示编辑框中的数字+1。反正不能正确显示。
请教这个问题该如何解决呀?

解决方案 »

  1.   

    #include "stdafx.h"
    #include "NumberEdit.h"
    #include "float.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CNumberEditCNumberEdit::CNumberEdit()
    {
    m_nMinValue = 0;
    m_nMaxValue = 5000;
    m_nDelta = FLT_ROUNDS;
    }CNumberEdit::~CNumberEdit()
    {
    }BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
    //{{AFX_MSG_MAP(CNumberEdit)
    ON_WM_CHAR()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CNumberEdit message handlers//键盘按下
    void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
    // TODO: Add your message handler code here and/or call default
    BOOL bRet = FALSE;
    CString str;
    GetWindowText(str);
    switch(nChar)
    {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case VK_BACK:
    bRet = TRUE;
    break;
    case '.':
    if(str.Find('.')<0)
    bRet = TRUE;
    break;
    default:
    break;
    }
    if(bRet)
    CEdit::OnChar(nChar, nRepCnt, nFlags);
    }//获取输出的范围
    void CNumberEdit::GetRange(int & min, int & max) const
    {
    min = m_nMinValue;
    max = m_nMaxValue;
    }//设置输出的范围
    void CNumberEdit::SetRange(int min, int max)
    {
    m_nMinValue = min;
    m_nMaxValue = max;
    }//获取步进值
    int CNumberEdit::GetDelta()
    {
    return m_nDelta;
    }//设置步进值
    void CNumberEdit::SetDelta(int nDelta)
    {
    m_nDelta = nDelta;
    }
    //获取小数
    int CNumberEdit::GetDecimal()const
    {
    int n = 0;
    CString str;               
    GetWindowText(str);    
    n = int(atof(str)*10);  //转换成整数
    return (n);  
    }//设置小数
    BOOL CNumberEdit::SetDecimal(int nDecimal)
    {
    if (nDecimal > m_nMaxValue || nDecimal < m_nMinValue)
    return FALSE;
    CString str;
    str.Format("%d.%d",nDecimal/10,nDecimal%10);  
    SetWindowText(str);    
    return TRUE;
    }//改变小数时
    void CNumberEdit::ChangeDecimal(int step)
    {
    int n = GetDecimal() + step * m_nDelta;
    if(n > m_nMaxValue)
    n = m_nMaxValue;
    else if(n < m_nMinValue)
    n = m_nMinValue;
    SetDecimal(n);
    }
    ///以上是CPP文件
    /////////////////////////////这里是头文件中需要增加的函数//////////////////////
    // Implementation
    public:
    virtual ~CNumberEdit(); virtual int  GetDelta();
    virtual void SetDelta(int nDelta);
    virtual void GetRange(int &min, int &max)const; 
    virtual void SetRange(int min, int max);
    virtual void ChangeDecimal(int step);
    virtual BOOL SetDecimal(int nDecimal);
    virtual int GetDecimal()const; // Generated message map functions
    protected: int m_nDelta, m_nMinValue, m_nMaxValue;
    //{{AFX_MSG(CNumberEdit)
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
    以上的文件新建一个类基类是CEdit.把以上代码添加进去。BOOL CMy22Dlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    m_edit.SetRange(0,200);         //范围
    m_edit.SetDelta(1);             //步进
    m_edit.SetDecimal(0);           //初始值
    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CMy22Dlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
    // TODO: Add your control notification handler code here
    m_edit.ChangeDecimal(-pNMUpDown->iDelta);
    m_edit.SetDecimal(m_edit.GetDecimal());

    *pResult = 0;
    }void CMy22Dlg::OnKillfocusEdit1() 
    {
    // TODO: Add your control notification handler code here
    m_edit.ChangeDecimal(0);
    m_edit.SetDecimal(m_edit.GetDecimal());
    }
    OK。