⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex_calculator03dlg.cpp

📁 C++控件的使用,计算器的设计 有典型起步提高进阶和提高
💻 CPP
字号:
// EX_Calculator03Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "EX_Calculator03.h"
#include "EX_Calculator03Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

char char_radio='D';
int value=0;
CString str;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEX_Calculator03Dlg dialog

CEX_Calculator03Dlg::CEX_Calculator03Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEX_Calculator03Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEX_Calculator03Dlg)
	m_InputEdit = _T("");
	m_OutputEdit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEX_Calculator03Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEX_Calculator03Dlg)
	DDX_Control(pDX, IDC_A_BUTTON, m_AButton);
	DDX_Control(pDX, IDC_F_BUTTON, m_FButton);
	DDX_Control(pDX, IDC_E_BUTTON, m_EButton);
	DDX_Control(pDX, IDC_D_BUTTON, m_DButton);
	DDX_Control(pDX, IDC_C_BUTTON, m_CButton);
	DDX_Control(pDX, IDC_B_BUTTON, m_BButton);
	DDX_Control(pDX, IDC_9_BUTTON, m_9Button);
	DDX_Control(pDX, IDC_8_BUTTON, m_8Button);
	DDX_Text(pDX, IDC_INPUT_EDIT, m_InputEdit);
	DDX_Text(pDX, IDC_OUTPUT_EDIT, m_OutputEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEX_Calculator03Dlg, CDialog)
	//{{AFX_MSG_MAP(CEX_Calculator03Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_D_RADIO, OnDRadio)
	ON_BN_CLICKED(IDC_H_RADIO, OnHRadio)
	ON_BN_CLICKED(IDC_O_RADIO, OnORadio)
	ON_BN_CLICKED(IDC_0_BUTTON, On0Button)
	ON_BN_CLICKED(IDC_1_BUTTON, On1Button)
	ON_BN_CLICKED(IDC_2_BUTTON, On2Button)
	ON_BN_CLICKED(IDC_3_BUTTON, On3Button)
	ON_BN_CLICKED(IDC_4_BUTTON, On4Button)
	ON_BN_CLICKED(IDC_5_BUTTON, On5Button)
	ON_BN_CLICKED(IDC_6_BUTTON, On6Button)
	ON_BN_CLICKED(IDC_7_BUTTON, On7Button)
	ON_BN_CLICKED(IDC_8_BUTTON, On8Button)
	ON_BN_CLICKED(IDC_9_BUTTON, On9Button)
	ON_BN_CLICKED(IDC_A_BUTTON, OnAButton)
	ON_BN_CLICKED(IDC_B_BUTTON, OnBButton)
	ON_BN_CLICKED(IDC_C_BUTTON, OnCButton)
	ON_BN_CLICKED(IDC_D_BUTTON, OnDButton)
	ON_BN_CLICKED(IDC_E_BUTTON, OnEButton)
	ON_BN_CLICKED(IDC_F_BUTTON, OnFButton)
	ON_BN_CLICKED(IDC_TO_D_BUTTON, OnToDButton)
	ON_BN_CLICKED(IDC_TO_H_BUTTON, OnToHButton)
	ON_BN_CLICKED(IDC_TO_O_BUTTON, OnToOButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEX_Calculator03Dlg message handlers

BOOL CEX_Calculator03Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	m_AButton.EnableWindow(FALSE);
	m_BButton.EnableWindow(FALSE);
	m_CButton.EnableWindow(FALSE);
	m_DButton.EnableWindow(FALSE);
	m_EButton.EnableWindow(FALSE);
	m_FButton.EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEX_Calculator03Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEX_Calculator03Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEX_Calculator03Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEX_Calculator03Dlg::OnDRadio() 
{
	// TODO: Add your control notification handler code here

	char_radio='D';
	value=0;
	m_8Button.EnableWindow(TRUE);
	m_9Button.EnableWindow(TRUE);
	m_AButton.EnableWindow(FALSE);
	m_BButton.EnableWindow(FALSE);
	m_CButton.EnableWindow(FALSE);
	m_DButton.EnableWindow(FALSE);
	m_EButton.EnableWindow(FALSE);
	m_FButton.EnableWindow(FALSE);

}

void CEX_Calculator03Dlg::OnHRadio() 
{
	// TODO: Add your control notification handler code here
	char_radio='D';
	value=0;
	m_8Button.EnableWindow(TRUE);
	m_9Button.EnableWindow(TRUE);
	m_AButton.EnableWindow(TRUE);
	m_BButton.EnableWindow(TRUE);
	m_CButton.EnableWindow(TRUE);
	m_DButton.EnableWindow(TRUE);
	m_EButton.EnableWindow(TRUE);
	m_FButton.EnableWindow(TRUE);	
}

void CEX_Calculator03Dlg::OnORadio() 
{
	// TODO: Add your control notification handler code here
	char_radio='D';
	value=0;
	m_8Button.EnableWindow(FALSE);
	m_9Button.EnableWindow(FALSE);
	m_AButton.EnableWindow(FALSE);
	m_BButton.EnableWindow(FALSE);
	m_CButton.EnableWindow(FALSE);
	m_DButton.EnableWindow(FALSE);
	m_EButton.EnableWindow(FALSE);
	m_FButton.EnableWindow(FALSE);
}

void CEX_Calculator03Dlg::On0Button() 
{
	// TODO: Add your control notification handler code here

	switch (char_radio)	{
		case 'O':
			value=value*8+0;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+0;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+0;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);					

}

void CEX_Calculator03Dlg::On1Button() 
{
	// TODO: Add your control notification handler code here
	
	switch (char_radio)	{
		case 'O':
			value=value*8+1;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+1;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+1;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On2Button() 
{
	// TODO: Add your control notification handler code here
	
	switch (char_radio)	{
		case 'O':
			value=value*8+2;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+2;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+2;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On3Button() 
{
	// TODO: Add your control notification handler code here
	
	switch (char_radio)	{
		case 'O':
			value=value*8+3;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+3;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+3;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On4Button() 
{
	// TODO: Add your control notification handler code here

	switch (char_radio)	{
		case 'O':
			value=value*8+4;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+4;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+4;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On5Button() 
{
	// TODO: Add your control notification handler code here
	
	switch (char_radio)	{
		case 'O':
			value=value*8+5;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+5;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+5;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On6Button() 
{
	// TODO: Add your control notification handler code here
	
	switch (char_radio)	{
		case 'O':
			value=value*8+6;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+6;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+6;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On7Button() 
{
	// TODO: Add your control notification handler code here
	
	switch (char_radio)	{
		case 'O':
			value=value*8+7;
			str.Format("%o",value);
			break;
		case 'D':
			value=value*10+7;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+7;
			str.Format("%x",value);
			break;
	}
	
	m_InputEdit=str;						//将输入数据显示在编辑框中
	UpdateData(FALSE);				
}

void CEX_Calculator03Dlg::On8Button() 
{
	// TODO: Add your control notification handler code here
	
	switch(char_radio){
		case 'D':
			value=value*10+8;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+8;
			str.Format("%x",value);
			break;
		}

	m_InputEdit=str;					//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::On9Button() 
{
	// TODO: Add your control notification handler code here
	
		switch(char_radio){
		case 'D':
			value=value*10+9;
			str.Format("%d",value);
			break;
		case 'H':
			value=value*16+9;
			str.Format("%x",value);
			break;
		}

	m_InputEdit=str;					//将输入数据显示在编辑框中
	UpdateData(FALSE);
}

void CEX_Calculator03Dlg::OnAButton() 
{
	// TODO: Add your control notification handler code here
	
	value=value*16+10;
	str.Format("%x",value);

	m_InputEdit=str;				//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnBButton() 
{
	// TODO: Add your control notification handler code here
	
	value=value*16+11;
	str.Format("%x",value);

	m_InputEdit=str;				//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnCButton() 
{
	// TODO: Add your control notification handler code here
	
	value=value*16+12;
	str.Format("%x",value);

	m_InputEdit=str;				//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnDButton() 
{
	// TODO: Add your control notification handler code here
	
	value=value*16+13;
	str.Format("%x",value);

	m_InputEdit=str;				//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnEButton() 
{
	// TODO: Add your control notification handler code here
	
	value=value*16+14;
	str.Format("%x",value);

	m_InputEdit=str;				//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnFButton() 
{
	// TODO: Add your control notification handler code here
	
	value=value*16+15;
	str.Format("%x",value);

	m_InputEdit=str;				//将输入数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnToDButton() 
{
	// TODO: Add your control notification handler code here

	str.Format("%d",value);		//将数据转换为对应进制下的字符串
	value=0;
	m_OutputEdit=str;				//将转换后的数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnToHButton() 
{
	// TODO: Add your control notification handler code here
	
	str.Format("%x",value);		//将数据转换为对应进制下的字符串
	value=0;
	m_OutputEdit=str;				//将转换后的数据显示在编辑框中
	UpdateData(FALSE);

}

void CEX_Calculator03Dlg::OnToOButton() 
{
	// TODO: Add your control notification handler code here
	
	str.Format("%o",value);		//将数据转换为对应进制下的字符串
	value=0;
	m_OutputEdit=str;				//将转换后的数据显示在编辑框中
	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -