• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++语言访问对话框中的控件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 
//创建一个基于对话框的工程,向对话框中添加静态文本,编辑框和组合框控件

CComboBox m_Symbo;
CEdit m_SecOpt;
CEdit m_Result;
CEdit m_FirstOpt;

enum Symbo {PLUS, SUB, MUL,DIV};
template <class Type>
class CCalc
{
public:
    Type Calc(Type &refFirstOpt, Type &refSecOpt, Symbo Opt)
    {
        switch (Opt)
        {
        case PLUS:
            return refFirstOpt + refSecOpt;
        case SUB:
            return refFirstOpt - refSecOpt;
        case MUL:
            return refFirstOpt * refSecOpt;
        case DIV:
            return refFirstOpt / refSecOpt;
        }
    }
};


void CCalcDlg::OnCalc() 
{
    // TODO: Add your control notification handler code here
    CString szFirstOpt, szSecOpt, szSymbo, szResult;
    m_Symbo.GetWindowText(szSymbo);
    if(!szSymbo.IsEmpty())
    {
        m_FirstOpt.GetWindowText(szFirstOpt);
        m_SecOpt.GetWindowText(szSecOpt);

        double dbFirst = atof(szFirstOpt.GetBuffer(0));
        szFirstOpt.ReleaseBuffer(0);
        
        double dbSec = atof(szSecOpt.GetBuffer(0));
        szSecOpt.ReleaseBuffer(0);
        

        Symbo opt;
        if(szSymbo == "+")
            opt = PLUS;
        else if(szSymbo == "-")
            opt = SUB;
        else if(szSymbo == "*")
            opt = MUL;
        else if(szSymbo == "/")
            opt = DIV;

        CCalc<double> CalcObj;
        double dbRet = CalcObj.Calc(dbFirst, dbSec, opt);
        szResult.Format("%-8.2f", dbRet);
        m_Result.SetWindowText(szResult);
        szResult.ReleaseBuffer(0);
            
    }

    
}
 //利用控件ID获取控件窗口指针
CString szFirstOpt, szSecOpt, szSymbo, szResult;
CComboBox *pSymbo = (CComboBox*)GetDlgItem(IDC_SYMBO);
pSymbo->GetWindowText(szSymbo);
if(!szSymbo.IsEmpty())
{
    CEdit *pFirstOpt = (CEdit*)GetDlgItem(IDC_FIRSTOPT);
    pFirstOpt->GetWindowText(szFirstOpt);
    CEdit *pSecOpt = (CEdit*)GetDlgItem(IDC_SECOPT);
    pSecOpt->GetWindowText(szSecOpt);
    double dbFirst = atof(szFirstOpt.GetBuffer(0));
    szFirst.ReleaseBuffer(0);
    double dbSec = atof(szSecOpt.GetBuffer(0));
    szSecOpt.ReleaseBuffer(0));
    Symbo opt;
    if(szSymbo == "+")
        opt = PLUS;
    else if(szSymbo == "-")
        opt = SUB;
    else if(szSymbo == "*")
        opt = MUL;
    else
        opt = DIV;

    CCalc<double> CalcObj;
    double dbRet = CalcObj.Calc(dbFirst, dbSec, opt);
    CEdit *pResult = (CEdit*)GetDlgItem(IDC_RET);
    szResult.Format("%-8.2f", dbRet);
    pResult->SetWindowText(szResult);
    szResult.ReleaseBuffer(0);

}

 

 

 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap