首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

请问Spin控件设置步长为小数时,遇到的小疑点

2012-03-22 
请教Spin控件设置步长为小数时,遇到的小问题各位高手晚上好!问题如下:新建了基于对话框的程序,将Edit控件

请教Spin控件设置步长为小数时,遇到的小问题
各位高手晚上好!问题如下:
新建了基于对话框的程序,将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。反正不能正确显示。
请教这个问题该如何解决呀?


[解决办法]
因为Spin控件只认整数啊。
[解决办法]
帮楼主顶一下
[解决办法]

C/C++ code
 

#include "stdafx.h"
#include "NumberEdit.h"
#include "float.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNumberEdit

CNumberEdit::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文件

C/C++ code
/////////////////////////////这里是头文件中需要增加的函数//////////////////////// Implementationpublic:    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 functionsprotected:    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()}; 

热点排行