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

📄 caldlg.cpp

📁 这是一个用V++编的计算器
💻 CPP
字号:
// CalDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Cal.h"
#include "CalDlg.h"
#include "string.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()

/////////////////////////////////////////////////////////////////////////////
// CCalDlg dialog

CCalDlg::CCalDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_lResult=0;
	m_lResultTemp=0;
	m_nStat=-1;
	m_numtemp=0;
	a=1;b=1;c=1;d=1;
	m_bCheck1=TRUE;//属性表单2个控制选项值传递到这来
	m_bCheck2=TRUE;
	
}

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

BEGIN_MESSAGE_MAP(CCalDlg, CDialog)
	//{{AFX_MSG_MAP(CCalDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_12, OnBtn12)
	ON_BN_CLICKED(IDC_BTN_13, OnBtn13)
	ON_BN_CLICKED(IDC_BTN_0, OnBtn0)
	ON_BN_CLICKED(IDC_BTN_1, OnBtn1)
	ON_BN_CLICKED(IDC_BTN_2, OnBtn2)
	ON_BN_CLICKED(IDC_BTN_3, OnBtn3)
	ON_BN_CLICKED(IDC_BTN_4, OnBtn4)
	ON_BN_CLICKED(IDC_BTN_5, OnBtn5)
	ON_BN_CLICKED(IDC_BTN_6, OnBtn6)
	ON_BN_CLICKED(IDC_BTN_7, OnBtn7)
	ON_BN_CLICKED(IDC_BTN_8, OnBtn8)
	ON_BN_CLICKED(IDC_BTN_9, OnBtn9)
	ON_BN_CLICKED(IDC_BTN_11, OnBtn11)
	ON_BN_CLICKED(IDC_BTN_14, OnBtnResult)
	ON_WM_CREATE()
	ON_WM_RBUTTONDOWN()
	ON_BN_CLICKED(IDC_BTN_15, OnBtn15)
	ON_BN_CLICKED(IDC_BTN_16, OnBtn16)
	ON_BN_CLICKED(IDC_BTN_17, OnBtn17)
	ON_COMMAND(IDM_SET1, OnSet1)
	ON_COMMAND(IDM_SET2, OnSet2)
	ON_COMMAND(IDC_SET3, OnSet3)
	ON_BN_CLICKED(IDC_BTN_18, OnBtn18)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
/////////////////////////////////////////////////////////////////////////////
// CCalDlg message handlers

BOOL CCalDlg::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
    GetDlgItem(IDC_STAT_SHOW)->SetWindowText("右键->设置时间和状态显示");
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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



void CCalDlg::OnBtn13() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_RESULT)->SetWindowText(""); 
    m_lResult=0;
	m_numtemp=0;
    m_lResultTemp=0;
	a=1;
	b=1;
	c=1;
	d=1;//初始化工作
/*	CString   sTime,s1,s2;
    SYSTEMTIME   CurTime;   
    GetLocalTime(&CurTime);   
    s1.Format("%d",CurTime.wHour);
	s2.Format("%d",CurTime.wMinute);
	sTime=s1+":"+s2;
	MessageBox("sTime");
*/  //显示当前时间方法1

	  
}

void CCalDlg::OnBtn1() 
{
		// TODO: Add your control notification handler code here
    fixnum(1,'1');
}

void CCalDlg::OnBtn2() 
{
	// TODO: Add your control notification handler code here
    fixnum(2,'2');
}

void CCalDlg::OnBtn3() 
{
	// TODO: Add your control notification handler code here

	fixnum(3,'3');

	
}

void CCalDlg::OnBtn4() 
{
	// TODO: Add your control notification handler code here
	fixnum(4,'4');

}

void CCalDlg::OnBtn5() 
{
	// TODO: Add your control notification handler code here
	CString strtemp;
	fixnum(5,'5');
	
}

void CCalDlg::OnBtn6() 
{
	// TODO: Add your control notification handler code here
	fixnum(6,'6');
	
}

void CCalDlg::OnBtn7() 
{
	// TODO: Add your control notification handler code here
	fixnum(7,'7');
}

void CCalDlg::OnBtn8() 
{
	// TODO: Add your control notification handler code here
	fixnum(8,'8');
}

void CCalDlg::OnBtn9() 
{
	// TODO: Add your control notification handler code here
	fixnum(9,'9');

}

void CCalDlg::OnBtn0() 
{
	// TODO: Add your control notification handler code here
    fixnum(0,'0');
}

void CCalDlg::OnBtn11() //小数点处理
{
	// TODO: Add your control notification handler code here
	CString strtemp;
//	double  lnumtemp;
	GetDlgItem(IDC_RESULT)->GetWindowText(strtemp);
	if(-1==strtemp.Find("."))
	{
		strtemp+=".";
    	GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);	
		//以上是显示小数点,以下在实际数值中处理小数点

	}


	
}



void CCalDlg::OnBtnResult() 
{
	// TODO: Add your control notification handler code here
	double resulttemp=0; //结果显示
	CString strtemp;
   

	if(1==m_nStat) //如果是加法模式
	{
		if(a<1)	m_numtemp=m_lResultTemp;//第一次不运行,因为i=1 以后均运行目的是让
		//它第一次不运行,以后都运行。算法滥了点,呵呵,没想到更好的。也许你有哦。
		resulttemp=m_lResult+m_numtemp;
        strtemp.Format("%f",resulttemp);
    	GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
		
        m_lResultTemp=resulttemp; //将当前结果付给m_lResultTemp
	}
	if(2==m_nStat)//如果是减法模式 以下类推
	{
		if(b<1)	m_numtemp=m_lResultTemp;//第一次不运行,因为i=1 以后均运行
		resulttemp=m_numtemp-m_lResult;
		if(resulttemp<0)
		{
			strtemp.Format("-%f",resulttemp);
		}
		else
		{     
			strtemp.Format("%f",resulttemp);
		}

    	GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
        m_lResultTemp=resulttemp; //将当前结果付给m_lResultTemp
	}
    if (3==m_nStat)
	{
		if(c<1)	m_numtemp=m_lResultTemp;//第一次不运行,因为i=1 以后均运行
		resulttemp=m_numtemp*m_lResult;
		if(resulttemp<0)
		{
			strtemp.Format("-%f",resulttemp);
		}
		else
		{     
			strtemp.Format("%f",resulttemp);
		}

    	GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
        m_lResultTemp=resulttemp; //将当前结果付给m_lResultTemp

    }   
	if (4==m_nStat&&m_lResult!=0)
	{
		if(d<1)	m_numtemp=m_lResultTemp;//第一次不运行因为i=1,以后均运行
		resulttemp=m_numtemp/m_lResult;
		if(resulttemp<0)
		{
			strtemp.Format("-%f",resulttemp);
		}
		else
		{     
			strtemp.Format("%f",resulttemp);
		}

    	GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
        m_lResultTemp=resulttemp; //将当前结果付给m_lResultTemp

    }
	else if(4==m_nStat&&m_lResult==0)
	{
    	GetDlgItem(IDC_STAT_SHOW)->SetWindowText("警告:除数不能为零");
        GetDlgItem(IDC_RESULT)->SetWindowText("ERROR"); 
        m_lResult=0;
    	m_lResultTemp=0;
	}

	a--;b--;c--;d--;//控制变量
}

int CCalDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;    
	// TODO: Add your specialized creation code here
	SetTimer(1,1000,NULL);

	return 0;
}


void CCalDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMenu m_mSysSet;
	CMenu *tempMenu;
	m_mSysSet.LoadMenu(IDR_MENU1);
	tempMenu=m_mSysSet.GetSubMenu(0);
	ClientToScreen(&point);
    tempMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
    this);
	//以上弹出右键菜单

	
	CDialog::OnRButtonDown(nFlags, point);
}

void CCalDlg::fixnum(int num, char num_show) 
//此函数功能当按下一个数字时在屏幕最后一位添加该数字并且将实际数值也在最后一位相应增加
//有小数点和无小数点增加方法不同
//num:对应键位的数字 num_show:对应键位应该增加的数字显示->字符
{
  	CString strtemp;
	double lnumtemp;
	GetDlgItem(IDC_RESULT)->GetWindowText(strtemp);
	lnumtemp=atof(strtemp);
	if(-1==strtemp.Find(".")) //无小数点模式 (有小数点和无小数点再最后一位加1方式不一样
	{
		m_lResult=lnumtemp*10+num;
	    strtemp+=num_show;	
	}
	else
	{
		strtemp.Insert((strtemp.GetLength()-strtemp.Find("."))+100,num_show); 
		//找出小数点的位置并且在最后一位加上1
        m_lResult=atof(strtemp);

	}
   	GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
	GetDlgItem(IDC_STAT_SHOW)->SetWindowText(strtemp);

}

void CCalDlg::OnBtn12() //加法按纽
{
	// TODO: Add your control notification handler code here
	m_numtemp=m_lResult;
	m_nStat=1; //计算器计算状态
	GetDlgItem(IDC_RESULT)->SetWindowText("");
    GetDlgItem(IDC_STAT_SHOW)->SetWindowText("加法模式.");
}

void CCalDlg::OnBtn15() //减法按钮
{
	// TODO: Add your control notification handler code here
	m_numtemp=m_lResult;
	m_nStat=2; //计算器计算状态
	GetDlgItem(IDC_RESULT)->SetWindowText("");
    GetDlgItem(IDC_STAT_SHOW)->SetWindowText("减法模式.");
}

void CCalDlg::OnBtn16() //乘法按扭
{
	// TODO: Add your control notification handler code here
	m_numtemp=m_lResult;
	m_nStat=3; //计算器计算状态
	GetDlgItem(IDC_RESULT)->SetWindowText("");
    GetDlgItem(IDC_STAT_SHOW)->SetWindowText("乘法模式.");
	
}

void CCalDlg::OnBtn17() //除法按扭
{
	// TODO: Add your control notification handler code here
	m_numtemp=m_lResult;
	m_nStat=4; //计算器计算状态
	GetDlgItem(IDC_RESULT)->SetWindowText("");
    GetDlgItem(IDC_STAT_SHOW)->SetWindowText("除法模式.");

}

void CCalDlg::OnSet1() 
{
	// TODO: Add your command handler code here
	CPropertySheet sheet("计算器设置");
	CProp1 prop1;
	CProp2 prop2;
	prop1.m_check1=m_bCheck1;
	prop1.m_check2=m_bCheck2;
    sheet.AddPage(&prop1);
    sheet.AddPage(&prop2);
	if(IDOK==sheet.DoModal())
	{
		if(TRUE==prop1.m_check1)
		{
			GetDlgItem(IDC_STAT_SHOW)->ShowWindow(SW_SHOW);
		}
		else
		{
			GetDlgItem(IDC_STAT_SHOW)->ShowWindow(SW_HIDE);
		}
		
	    if(TRUE==prop1.m_check2)
		{
			GetDlgItem(IDC_EDIT_TIME)->ShowWindow(SW_SHOW);
		}
		else
		{
			GetDlgItem(IDC_EDIT_TIME)->ShowWindow(SW_HIDE);
		}
		
		m_bCheck1=prop1.m_check1;
        m_bCheck2=prop1.m_check2;

	}

}

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

void CCalDlg::OnSet3() 
{
	// TODO: Add your command handler code here
    CDialog::OnOK();
}

void CCalDlg::OnBtn18() 
{
 	// TODO: Add your control notification handler code here
	    //按下±键为将一个数设置为其的相反数 有问题
    CString strtemp,strtemp1;
	GetDlgItem(IDC_RESULT)->GetWindowText(strtemp);
	if(m_lResult<0)
	{
		m_lResult=-1*m_lResult;
		strtemp.Format("%f",m_lResult);
		GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
		strtemp1="#非负数状态";
		GetDlgItem(IDC_STAT_SHOW)->SetWindowText(strtemp1);

	}
	else
	{	
		m_lResult=-1*m_lResult;
		strtemp.Format("-%f",m_lResult);
		GetDlgItem(IDC_RESULT)->SetWindowText(strtemp);
		strtemp1="#负数状态";
		GetDlgItem(IDC_STAT_SHOW)->SetWindowText(strtemp1);

	}
	
}


void CCalDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
  /*  time_t osBinaryTime;  // C run-time time (defined in <time.h>)
    time( &osBinaryTime ) ;  // Get the current time from the 
	// operating system.
	CTime time3( osBinaryTime );  // CTime from C run-time time
	CString str=time3.Format("%H:%M:%S");
	GetDlgItem(IDC_EDIT_TIME)->SetWindowText(str);*///时间显示方法2

	CTime t=CTime::GetCurrentTime();
	CString str=t.Format("当前时间: %H:%M:%S");
	GetDlgItem(IDC_EDIT_TIME)->SetWindowText(str);//显示方法3
	
	
	CDialog::OnTimer(nIDEvent);
}

void CCalDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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