MFC_Spin Control

蓝咒 提交于 2020-02-26 08:21:16

如图所示控件由edit结合spin组成.

在对话框的.h里添加

    afx_msg void OnEnChangeEdit();
    afx_msg void OnEnKillfocusEdit();
    afx_msg void OnDeltaposSpin(NMHDR *pNMHDR, LRESULT *pResult);
    
    bool updateEdit();

private:    
    CEdit m_edit;
    double m_dEdit;
    double m_dDelta;
    CSpinButtonCtrl m_Spin;  

在对话框的cpp里添加

XXXDlg::XXXDlg(CWnd* pParent /*=NULL*/)
	: XXXChildDlg(XXXDlg::IDD, pParent)
    ,m_dEdit(2)
    ,m_dDelta(1)


void XXXDlg::DoDataExchange(CDataExchange* pDX)
{
    __super::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT, m_edit);
    DDX_Text(pDX, IDC_EDIT, m_dEdit);
    DDX_Control(pDX, IDC_SPIN, m_Spin);
}

BEGIN_MESSAGE_MAP(XXXDlg, XXXChildDlg)
    ON_EN_CHANGE(IDC_EDIT, &XXXDlg::OnEnChangeEdit)
    ON_EN_KILLFOCUS(IDC_EDIT, &XXXDlg::OnEnKillfocusEdit)
    ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN, &XXXDlg::OnDeltaposSpin)
END_MESSAGE_MAP()

BOOL XXXDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    m_dEdit = 0;
    m_edit.SetWindowText("");

    return TRUE;
}

void XXXDlg::OnEnChangeEdit()
{
    updateEdit();
}

void XXXDlg::OnEnKillfocusEdit()
{
    if(!updateEdit())
    {
        m_edit.SetSel(0, -1);
        m_edit.SetFocus();
    }
}

void XXXDlg::OnDeltaposSpin(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

    double dTemp = m_dEdit;
    if(pNMUpDown->iDelta < 0)
    {
        m_dEdit += m_dDelta;
    }
    else
    {
        m_dEdit  -= m_dDelta;
    }
    UpdateData(FALSE);

    if (!updateEdit())
    {
        m_dEdit = dTemp;
        UpdateData(FALSE);
    }

    *pResult = 0;
}

bool XXXDlg::updateEdit()
{
    if (m_dEdit <=  1.e-10)
    {
        m_edit.SetFocus();
        return false;
    }

    if(!UpdateData(TRUE))
    {
        return false;
    }

    return true;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!