如圖是一個簡單的加法計算器:
void CMy01_DemoDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知處理程序代碼
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1, 12);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2, 12);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);
}
void CMy01_DemoDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知處理程序代碼
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
GetDlgItemText(IDC_EDIT1, ch1, 12);
GetDlgItemText(IDC_EDIT2, ch2, 12);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
SetDlgItemText(IDC_EDIT3, ch3);
}
void CMy01_DemoDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知處理程序代碼
int num1, num2, num3;
num1 = GetDlgItemInt(IDC_EDIT1);
num2 = GetDlgItemInt(IDC_EDIT2);
num3 = num1 num2;
SetDlgItemInt(IDC_EDIT3, num3);
}
方法四和方法五是關聯法,常用到的四個函數:
DoDataExchange();
DDC_xxxx();//關聯變量
DDV_xxxx();//驗證變量
UpdateData();//UpdateData(TRUE)為從編輯框獲取數據,UpdateData(FALSE)為更新數據到編輯框
CMy01_DemoDlg::CMy01_DemoDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MY01_DEMO_DIALOG, pParent)
, m_num1(0)
, m_num2(0)
, m_num3(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy01_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_num1);
DDX_Text(pDX, IDC_EDIT2, m_num2);
DDX_Text(pDX, IDC_EDIT3, m_num3);
DDX_Control(pDX, IDC_EDIT1, m_edit1);
DDX_Control(pDX, IDC_EDIT2, m_edit2);
DDX_Control(pDX, IDC_EDIT3, m_edit3);
}
将編輯框與整形變量關聯,然後對整形成員變量直接進行操作
void CMy01_DemoDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知處理程序代碼
UpdateData(true);
m_num3 = m_num1 m_num2;
UpdateData(false);
}
将編輯框與控件變量關聯,對控件變量進行操作
void CMy01_DemoDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知處理程序代碼
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
m_edit1.GetWindowText(ch1, 12);
m_edit2.GetWindowText(ch2, 12);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
m_edit3.SetWindowText(ch3);
}
void CMy01_DemoDlg::OnBnClickedButton6()
{
// TODO: 在此添加控件通知處理程序代碼
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd, WM_GETTEXT, 12, (LPARAM)ch1);
::SendMessage(GetDlgItem(IDC_EDIT2)->m_hWnd, WM_GETTEXT, 12, (LPARAM)ch2);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
::SendMessage(GetDlgItem(IDC_EDIT3)->m_hWnd, WM_SETTEXT, 0, (LPARAM)ch3);
}
void CMy01_DemoDlg::OnBnClickedButton7()
{
// TODO: 在此添加控件通知處理程序代碼
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
SendDlgItemMessage(IDC_EDIT1, WM_GETTEXT, 12, (LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2, WM_GETTEXT, 12, (LPARAM)ch2);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
SendDlgItemMessage(IDC_EDIT3, WM_SETTEXT, 0, (LPARAM)ch3);
}
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!