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

📄 calculatordlg.cpp

📁 实现windows自带计算器功能
💻 CPP
字号:
// CalculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#include "math.h"  


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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog

CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculatorDlg)


	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_display = 0;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD));
}

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	DDX_Text(pDX, IDC_EDIT1, m_display);

	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
    ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTONADD, OnButtonadd)
	ON_BN_CLICKED(IDC_BUTTONSUB, OnButtonsub)
	ON_BN_CLICKED(IDC_BUTTONMUT, OnButtonmut)
	ON_BN_CLICKED(IDC_BUTTONDIV, OnButtondiv)
	ON_BN_CLICKED(IDC_BUTTONEQU, OnButtonequ)
	ON_BN_CLICKED(ID_CE, OnCe)
	ON_BN_CLICKED(IDC_BUTTONDOT, OnButtondot)
	ON_COMMAND(ID_FILE_EXIT, OnFileExit)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_BN_CLICKED(ID_C, OnC)
	ON_BN_CLICKED(IDC_BUTTONSQRT, OnButtonsqrt)
	ON_BN_CLICKED(IDC_BUTTONSIGN, OnButtonSIGN)
	ON_BN_CLICKED(IDC_BUTTONRec, OnBUTTONRec)
	ON_BN_CLICKED(IDC_BUTTONPER, OnButtonper)
	ON_BN_CLICKED(IDC_BIANHUAN, OnBianhuan)
	ON_BN_CLICKED(IDC_BUTTONEXP, OnButtonexp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers

BOOL CCalculatorDlg::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
	SetWindowText("calculator");
	
	// TODO: Add extra initialization here

	//变量初始化
    OnC();

    //加载菜单项
	m_menu.LoadMenu(IDR_MENU1);
	SetMenu(&m_menu);
	m_menu.Detach();

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

void CCalculatorDlg::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 CCalculatorDlg::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 CCalculatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

HBRUSH CCalculatorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(RGB(255,255,255));
	hbr=CreateSolidBrush(RGB(0,0,0));
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CCalculatorDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//::	SetCursor(LoadCursor(,LPCSTR(IDC_CURSOR1)));
	CDialog::OnMouseMove(nFlags, point);
}

void CCalculatorDlg::KeyDeal(int a)  //处理数字按钮
{
	if(m_dot)
	{
		m_end = m_end + point * a;
        point /= 10;
	}
	else	m_end = m_end*10 + a;
	m_display = m_end;
    UpdateData(FALSE);  //将结果显示在编辑器中
}

void CCalculatorDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(1);
}

void CCalculatorDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(2);
}

void CCalculatorDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(3);
}

void CCalculatorDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(4);
}

void CCalculatorDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(5);
}

void CCalculatorDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(6);
}

void CCalculatorDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(7);
}

void CCalculatorDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(8);
}

void CCalculatorDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(9);
}

void CCalculatorDlg::OnButton10() 
{
	// TODO: Add your control notification handler code here
	KeyDeal(0);
}

void CCalculatorDlg::OnButtondot() 
{
	// TODO: Add your control notification handler code here
        if(!m_dot)     //判断dot是否已按下过
        {
			m_dot=1;
            point=0.1;
		}
}

void CCalculatorDlg::Result()
{
	switch(m_bit)
	{
	case 1:// "+"
		m_base += m_end;
		break;
    case 2:// "-"
		m_base -= m_end;
		break;
	case 3:// "*"
		m_base *= m_end;
		break;
	case 4:// "/"
		if(m_end==0)
		{
			MessageBox("除数为0!");
			OnC();
			return;
		}
		m_base /= m_end;
		break;
	default:
		m_base = m_end;
		break;
	}
	m_end = 0;
	m_dot = 0;
	m_display = m_base;
	UpdateData(FALSE);
}

void CCalculatorDlg::OnButtonadd()   //加号运算符
{
	// TODO: Add your control notification handler code here
	Result();  //求值
	m_bit = 1;
}

void CCalculatorDlg::OnButtonsub()   //减号运算符
{
	// TODO: Add your control notification handler code here
    Result();  //求值
	m_bit = 2;
}

void CCalculatorDlg::OnButtonmut()   //乘号运算符
{
	// TODO: Add your control notification handler code here
	Result();  //记录
	m_bit =3;
}

void CCalculatorDlg::OnButtondiv()   //除号运算符
{
	// TODO: Add your control notification handler code here
	Result();  //记录

	m_bit =4;
}

void CCalculatorDlg::OnButtonequ()   //等号运算符
{
	// TODO: Add your control notification handler code here
	Result();  //求值
	m_bit = 1;
	m_end = 0;  //清零

}

void CCalculatorDlg::OnC() 
{
	// TODO: Add your control notification handler code here
	m_display = 0;
	m_dot = 0;
	m_bit = 0;
	m_end = m_base = 0;
	UpdateData(FALSE);
}

void CCalculatorDlg::OnCe() 
{
	// TODO: Add your control notification handler code here
	m_display = 0;
	m_dot = 0;
	m_end = 0;
	UpdateData(FALSE);
}

void CCalculatorDlg::OnButtonexp() 
{
	// TODO: Add your control notification handler code here
	m_end=exp(m_end);
    m_display = m_end;
    UpdateData(FALSE);
}

void CCalculatorDlg::OnButtonsqrt() 
{
	// TODO: Add your control notification handler code here
	m_end=sqrt(m_end);
    m_display = m_end;
    UpdateData(FALSE);
}

void CCalculatorDlg::OnButtonSIGN() 
{
	// TODO: Add your control notification handler code here
	m_end *= -1;
    m_display = m_end;
    UpdateData(FALSE);  
}

void CCalculatorDlg::OnBUTTONRec() 
{
	// TODO: Add your control notification handler code here
	m_end = 1/m_end;
    m_display = m_end;
    UpdateData(FALSE);
}

void CCalculatorDlg::OnButtonper() 
{
	// TODO: Add your control notification handler code here
	m_end = m_end/100;
    m_display = m_end;
    UpdateData(FALSE);
}

void CCalculatorDlg::OnFileExit() 
{
	// TODO: Add your command handler code here
	exit(0);
}

void CCalculatorDlg::OnHelpAbout() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlg;
	dlg.DoModal();
}

void CCalculatorDlg::OnBianhuan() 
{
	// TODO: Add your control notification handler code here
	CString str;
	if(GetDlgItemText(IDC_BIANHUAN,str),str=="收缩")
	{
		SetDlgItemText(IDC_BIANHUAN,"扩展");
	}
	else
	{
        SetDlgItemText(IDC_BIANHUAN,"收缩");
	}

	static CRect rectLarge;
    static CRect rectSmall;

	if(rectLarge.IsRectNull())
	{
		CRect rectSeparator;
		GetWindowRect(&rectLarge);
		GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);

		rectSmall.left=rectLarge.left;
		rectSmall.top=rectLarge.top;
		rectSmall.bottom=rectLarge.bottom;
        rectSmall.right=rectSeparator.right;
	}
    if(str=="收缩")
	{
		SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE | SWP_NOZORDER);
	}
	else
	{
        SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE | SWP_NOZORDER);
	}
}

⌨️ 快捷键说明

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