在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//创建一个基于对话框的工程,向对话框中添加静态文本,编辑框和组合框控件 CComboBox m_Symbo; 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); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论