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

📄 calculatordlg.cpp

📁 wince下的小程序
💻 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

#define BUTTON 0.0000001
#define TOP -0.0000001

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

CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculatorDlg)
	m_flag = _T("");
	m_operator_number = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_storevalue=0.0;
	m_firstoperator=0;
	//m_twooperator=0.0;
	m_operate='n';
    //m_operate_flag=0;
	m_flag=L"";
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	DDX_Text(pDX, IDC_EDIT2, m_flag);
	DDX_Text(pDX, IDC_EDIT1, m_operator_number);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_BN_CLICKED(IDC_IS, OnIs)
	ON_BN_CLICKED(IDC_DOWN, OnDown)
	ON_BN_CLICKED(IDC_CENTRI, OnCentri)
	ON_BN_CLICKED(IDC_EXTRACT, OnExtract)
	ON_BN_CLICKED(IDC_CLEAR2, OnClear2)
	ON_BN_CLICKED(IDC_CLEAR1, OnClear1)
	ON_BN_CLICKED(IDC_DIVE, OnDive)
	ON_BN_CLICKED(IDC_MULTI, OnMulti)
	ON_BN_CLICKED(IDC_SUB, OnSub)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_REDUCE, OnReduce)
	ON_BN_CLICKED(ID9, On9)
	ON_BN_CLICKED(ID6, On6)
	ON_BN_CLICKED(ID3, On3)
	ON_BN_CLICKED(IDC_BUTTON26, OnButton26)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_BN_CLICKED(IDC_MC, OnMc)
	ON_BN_CLICKED(IDC_MR, OnMr)
	ON_BN_CLICKED(IDC_MS, OnMs)
	ON_BN_CLICKED(IDC_MADD, OnMadd)
	ON_BN_CLICKED(ID0, On0)
	ON_BN_CLICKED(IDC_BUTTON25, OnButton25)
	ON_BN_CLICKED(ID1, On1)
	ON_BN_CLICKED(ID2, On2)
	ON_BN_CLICKED(ID4, On4)
	ON_BN_CLICKED(ID5, On5)
	ON_BN_CLICKED(ID7, On7)
	ON_BN_CLICKED(ID8, On8)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

	// 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
	
	//CenterWindow(GetDesktopWindow());	// center to the hpc screen
    SetWindowPos(this,0,26,240,294,SWP_SHOWWINDOW);
	// TODO: Add extra initialization here
	//CString ss=L"120.1212";
	//double db=atof(ss);
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CCalculatorDlg::OnIs() 
{
	// TODO: Add your control notification handler code here
	double b;
	char temp[256];
	UpdateData(true);
	memset(temp,'\0',256);
	mystrcpy(temp,m_operator_number);
    b=atof(temp);
	
	//m_firstoperator=b/100;
	//m_operator_number.Format(L"%f",m_firstoperator);
	//m_operator_number=-m_operator_number;
	
	switch(m_operate)
	{
	case '+':
		m_firstoperator=b+m_firstoperator;
	    m_operator_number.Format(L"%f",m_firstoperator);
	    Show();
		break;
	case '-':
		m_firstoperator=m_firstoperator-b;
	    m_operator_number.Format(L"%f",m_firstoperator);
	    Show();
		break;
	case '*':
		m_firstoperator=b*m_firstoperator;
	    m_operator_number.Format(L"%f",m_firstoperator);
	    Show();
		break;
	case '/':
		if((b<0.0000001)&&(b>-0.0000001))MessageBox(L"除数不能为零");
	    else
		{
			m_firstoperator=m_firstoperator/b;
	        m_operator_number.Format(L"%f",m_firstoperator);
	        Show();
		}
		break;

	}
}

void CCalculatorDlg::OnDown() 
{
	// TODO: Add your control notification handler code here
	//m_operate='d';
    double b;
	char temp[256];
	memset(temp,'\0',256);
	mystrcpy(temp,m_operator_number);
    b=atof(temp);
	m_firstoperator=1/b;
	m_operator_number.Format(L"%f",m_firstoperator);
	//m_operator_number=-m_operator_number;
	Show();

}

void CCalculatorDlg::OnCentri() 
{
	// TODO: Add your control notification handler code here
	//m_operate='%';
	//m_firstoperator=m_operator_number;
	double b;
	char temp[256];
	memset(temp,'\0',256);
	mystrcpy(temp,m_operator_number);
    b=atof(temp);
	m_firstoperator=b*100;
	m_operator_number.Format(L"%f",m_firstoperator);
    m_operator_number=m_operator_number+L"%";
	//m_operator_number=-m_operator_number;
	Show();
}

void CCalculatorDlg::OnExtract() 
{
	// TODO: Add your control notification handler code here
	double b;
	char temp[256];
	memset(temp,'\0',256);
	mystrcpy(temp,m_operator_number);
    b=atof(temp);
	if(b>=0.0)
	{
		m_firstoperator=sqrt(b);
	    m_operator_number.Format(L"%f",m_firstoperator);
	    //m_operator_number=-m_operator_number;
	    Show();
	}
}

void CCalculatorDlg::OnClear2() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CCalculatorDlg::OnClear1() 
{
	// TODO: Add your control notification handler code here
	m_operator_number=L"";
	m_firstoperator=0.0;
    m_operate='\0';
	UpdateData(false);
}

void CCalculatorDlg::OnDive() 
{
	// TODO: Add your control notification handler code here
		double b;
	    char temp[256];
	    memset(temp,'\0',256);
	    mystrcpy(temp,m_operator_number);
        b=atof(temp);
	    m_firstoperator=b;//m_firstoperator/b;
	        //m_operator_number.Format(L"%f",m_firstoperator);
	        //m_operator_number=-m_operator_number;
            //Show();
        m_operate='/';
        m_operator_number=L"";
			//UpdateData(false);
		Show();
		


}

void CCalculatorDlg::OnMulti() 
{
	// TODO: Add your control notification handler code here
	double b;
	    char temp[256];
	    //memset(temp,'\0',256);
	    mystrcpy(temp,m_operator_number);
		temp[m_operator_number.GetLength()]='\0';
        b=atof(temp);
	    m_firstoperator=b;
	    //m_operator_number.Format(L"%f",m_firstoperator);
	    m_operator_number=L"";
	    Show();
	    UpdateData(false);

        m_operate='*';
        //m_operator_number=L"";
		//Show();
}

void CCalculatorDlg::OnSub() 
{
	// TODO: Add your control notification handler code here
	double b;
	    char temp[256];
	    //memset(temp,'\0',256);
	    mystrcpy(temp,m_operator_number);
		temp[m_operator_number.GetLength()]='\0';
        b=atof(temp); 
		m_firstoperator=b;
	    //m_operator_number.Format(L"%f",m_firstoperator);
	    m_operator_number=L"";
	    Show();
        m_operate='-';
        //m_operator_number=L"";
		//Show();
}

void CCalculatorDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here

		double b;
	    char temp[256];
	    //memset(temp,'\0',256);
	    mystrcpy(temp,m_operator_number);
		temp[m_operator_number.GetLength()]='\0';
        b=atof(temp);
	    m_firstoperator=b;
	    //m_operator_number.Format(L"%f",m_firstoperator);
	    m_operator_number=L"";
	    Show();
	    UpdateData(false);

        m_operate='+';
        //m_operator_number=L"";
		//Show();
	
}

void CCalculatorDlg::OnReduce() 
{
	// TODO: Add your control notification handler code here
	int i=m_operator_number.GetLength( );
	if(i>=1)
	{
		m_operator_number.Delete(i-1,1);	
	    Show();
	}
	//UpdateData(false);
}

void CCalculatorDlg::On9() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,9);
	m_operator_number=m_operator_number+L"9";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::On6() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,6);
	m_operator_number=m_operator_number+L"6";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::On3() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,3);
	m_operator_number=m_operator_number+L"3";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::OnButton26() 
{
	// TODO: Add your control notification handler code here
	m_operator_number=m_operator_number+L".";
	Show();
}

//DEL void CCalculatorDlg::OnCancel() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	this->OnCancel();
//DEL }

void CCalculatorDlg::OnChangeEdit2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CCalculatorDlg::OnMc() 
{
	// TODO: Add your control notification handler code here
	
}

void CCalculatorDlg::OnMr() 
{
	// TODO: Add your control notification handler code here
	
}

void CCalculatorDlg::OnMs() 
{
	// TODO: Add your control notification handler code here
	
}

void CCalculatorDlg::OnMadd() 
{
	// TODO: Add your control notification handler code here
	
}

void CCalculatorDlg::On0() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,0);
	m_operator_number=m_operator_number+L"0";
	CEdit* pedit=(CEdit *)GetDlgItem(IDC_EDIT1);
	pedit->SetWindowText(m_operator_number);
	//Show();
    //UpdateData(false);
}

void CCalculatorDlg::OnButton25() 
{
	// TODO: Add your control notification handler code here
	double b;
	char temp[256];
	memset(temp,'\0',256);
	mystrcpy(temp,m_operator_number);
    b=atof(temp);
	b=-b;
	//m_firstoperator=b;
	m_operator_number.Format(L"%f",b);
	//UpdateData(false);
	Show();
}

void CCalculatorDlg::On1() 
{
	// TODO: Add your control notification handler code here
    //ShowEditNumber(m_operate_flag,1);
	m_operator_number=m_operator_number+L"1";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::On2() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,2);
	m_operator_number=m_operator_number+L"2";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::On4() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,4);
	m_operator_number=m_operator_number+L"4";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::On5() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,5);
	m_operator_number=m_operator_number+L"5";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::On7() 
{
	//ShowEditNumber(m_operate_flag,7);
	// TODO: Add your control notification handler code here
	m_operator_number=m_operator_number+L"7";
	Show();
    //UpdateData(false);
	
}

void CCalculatorDlg::On8() 
{
	// TODO: Add your control notification handler code here
	//ShowEditNumber(m_operate_flag,8);
	m_operator_number=m_operator_number+L"8";
	Show();
    //UpdateData(false);
}

void CCalculatorDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CClientDC hdc(this);

		CRect closerect,dlgrect;
		GetClientRect(&dlgrect);

		closerect.left=dlgrect.Width()-25;
		closerect.top=2;
		closerect.right=dlgrect.Width()-5;
		closerect.bottom=22;

		HBRUSH m_closebrush=::CreateSolidBrush(RGB(242,232,207));
		HBRUSH m_oldclosebrush=(HBRUSH)hdc.SelectObject(m_closebrush); //hdc.SelectObject(m_closebrush);
		CPen m_closepen;
		m_closepen.CreatePen(PS_SOLID,1,RGB(252,212,207));
		CPen *m_oldclosepen=hdc.SelectObject(&m_closepen);  //hdc.SelectObject(&m_closepen);
		hdc.Ellipse(closerect);
		hdc.SelectObject(m_oldclosebrush);
		hdc.SelectObject(m_oldclosepen);
		m_oldclosepen->Detach();
		m_oldclosepen->DeleteObject();
		CSize size=hdc.GetTextExtent("X");
		hdc.SetBkMode(TRANSPARENT);
		hdc.DrawText("X",CRect((closerect.left+(closerect.Width()-size.cx)/2),(closerect.top+(closerect.Height()-size.cy)/2),closerect.right,closerect.bottom),0);

	// Do not call CDialog::OnPaint() for painting messages
}

void CCalculatorDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect closerect,dlgrect;
	GetClientRect(&dlgrect);

	if (point.x>=dlgrect.Width()-25 && point.x<dlgrect.Width()-5 && point.y>=2 && point.y<=23)
		OnCancel();
	CDialog::OnLButtonDown(nFlags, point);
}

void CCalculatorDlg::mystrcpy(char *strdes, CString strsuc)
{
	LPWSTR mytemp=strsuc.GetBuffer(strsuc.GetLength());
	int relen=::WideCharToMultiByte(CP_ACP, 0, mytemp, strsuc.GetLength(), strdes, 256, NULL, NULL);
}

void CCalculatorDlg::Show()
{
	int i=m_operator_number.GetLength( );
	while((i>=1)&&(m_operator_number.GetAt(i-1)=='0'))
		{
			m_operator_number.Delete(i-1,1);
		    i-=1;
		}
    CEdit* pedit=(CEdit *)GetDlgItem(IDC_EDIT1);
	pedit->SetWindowText(m_operator_number);
	//m_string=str;
	UpdateData( );
	

}

⌨️ 快捷键说明

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