C++语言 访问对话框中的控件
//创建一个基于对话框的工程,向对话框中添加静态文本,编辑框和组合框控件
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); }
学习笔记转摘于:
丝酷网 http://www.pythonschool.com/