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

📄 calculatordlg.cpp

📁 用C++实现的计算器的源程序
💻 CPP
字号:
// CalculatorDlg.cpp : implementation file
//

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

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

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

CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculatorDlg)
	m_strExp = _T("");
	m_strRes = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bitmap.LoadBitmap(IDB_BITMAP_CONVERT2);
}

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	DDX_Control(pDX, IDC_BTN_CONVERT, m_btn_convert);
	DDX_Control(pDX, IDC_RADIO_RADIAN, m_radio_radian);
	DDX_Control(pDX, IDC_RADIO_DEGREE, m_radio_degree);
	DDX_Control(pDX, IDC_EDIT_INPUT, m_strExpc);
	DDX_Text(pDX, IDC_EDIT_INPUT, m_strExp);
	DDX_Text(pDX, IDC_EDIT_RESULT, m_strRes);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_NUM, OnButtonNum)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_OP, OnButtonOp)
	ON_BN_CLICKED(IDC_BUTTON_FUNC, OnButtonFunc)
	ON_BN_CLICKED(IDC_BTN_CONST, OnBtnConst)
	ON_BN_CLICKED(IDC_BTN_HISTREXP, OnBtnHistrexp)
	ON_COMMAND(ID_M_PI, OnMPi)
	ON_COMMAND(ID_M_C, OnMC)
	ON_COMMAND(ID_M_NA, OnMNa)
	ON_COMMAND(ID_M_G, OnMG)
	ON_COMMAND(ID_M_R, OnMR)
	ON_COMMAND(ID_M_K, OnMK)
	ON_COMMAND(ID_M_VM, OnMVm)
	ON_COMMAND(ID_M_KE, OnMKe)
	ON_COMMAND(ID_M_E0, OnME0)
	ON_COMMAND(ID_M_E, OnME)
	ON_BN_CLICKED(IDC_BTN_CONVERT, OnBtnConvert)
	ON_COMMAND(ID_M_HEX, OnMHex)
	ON_COMMAND(ID_M_OCT, OnMOct)
	ON_COMMAND(ID_M_BIN, OnMBin)
	ON_COMMAND(ID_M_DEC, OnMDec)
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(IMENU_ITEM_ID,IMENU_ITEM_ID+100,OnMenuSelect)
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
	
	// TODO: Add extra initialization here
	m_menu2.CreatePopupMenu();
	pNUMDlg=new CCNUM;
	pNUMDlg->Create(IDD_DIALOG_NUM);
	pOPDlg=new CCOP;
	pOPDlg->Create(IDD_DIALOG_OP);
	pFUNCDlg=new CCFUNC;
	pFUNCDlg->Create(IDD_DIALOG_FUNC);
	bIsNumBoardOpen=0;
	bIsOpBoardOpen=0;
	bIsFuncBoardOpen=0;
	bIsOk=0;
	m_radio_radian.SetCheck(1);
	m_btn_convert.SetBitmap(m_bitmap);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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;
}

void CCalculatorDlg::OnOK() 
{
	UpdateData(1);
	if(m_radio_degree.GetCheck()==1) calcu.m_bDegree=1;
//	calcu.Dec2Hex(&m_strExp);
	CString strRes=calcu.MainPro(m_strExp);
//	m_combo_histrexp.AddString(m_strExp);
//	m_combo_histrresult.AddString(strRes);
	m_strRes=strRes;
	m_strResTmp=strRes;
	AddToHistr(m_strExp+"= "+strRes);
	UpdateData(0);
//	CDialog::OnOK();
}

void CCalculatorDlg::OnTimer(UINT nIDEvent) 
{
	UpdateData(1);
	if(pNUMDlg->m_strN=="OVER")
	{
		bIsNumBoardOpen=0;
		KillTimer(0);
	}
	else if(pNUMDlg->m_strN=="BACK")
	{
		UpdateData(1);
		Paster("",0);
		m_strExpc.GetSel(m_nStart,m_nEnd);
		m_strExp.Delete(m_nStart-1);
		UpdateData(0);
		m_strExpc.SetFocus();
		m_strExpc.SetSel(m_nStart-1,m_nStart-1);
		pNUMDlg->m_strN="";
	}
	else if(!pNUMDlg->m_strN.IsEmpty())
	{
		Paster(pNUMDlg->m_strN,0);
		pNUMDlg->m_strN="";
	}

	if(pOPDlg->m_strN=="OVER")
	{
		bIsOpBoardOpen=0;
		KillTimer(1);
	}
	else if(!pOPDlg->m_strN.IsEmpty())
	{
		Paster(pOPDlg->m_strN,0);
		pOPDlg->m_strN="";
	}

	if(pFUNCDlg->m_strN=="OVER")
	{
		bIsFuncBoardOpen=0;
		KillTimer(2);
	}
	else if(!pFUNCDlg->m_strN.IsEmpty())
	{
		Paster(pFUNCDlg->m_strN,1);
		pFUNCDlg->m_strN="";
	}
	if(nIDEvent==3) 
	{
		GetWindowRect(m_rect);
		pNUMDlg->SetWindowPos(NULL,m_rect.left+375,m_rect.top,-1,-1,SWP_NOSIZE);
		pFUNCDlg->SetWindowPos(NULL,m_rect.left,m_rect.top-165,-1,-1,SWP_NOSIZE);
		pOPDlg->SetWindowPos(NULL,m_rect.left+256,m_rect.top-118,-1,-1,SWP_NOSIZE);
	}
	UpdateData(0);	
	CDialog::OnTimer(nIDEvent);
}

void CCalculatorDlg::OnButtonNum() 
{
	if(!bIsNumBoardOpen)
	{
		GetWindowRect(m_rect);
		pNUMDlg->SetWindowPos(NULL,m_rect.left+375,m_rect.top,-1,-1,SWP_NOSIZE);
		pNUMDlg->ShowWindow(SW_SHOW);
		SetTimer(0,200,NULL);
		bIsNumBoardOpen=1;
	}
	else
	{
		pNUMDlg->ShowWindow(SW_HIDE);
		bIsNumBoardOpen=0;
		KillTimer(0);
	}
}

void CCalculatorDlg::OnButtonOp() 
{
	if(!bIsOpBoardOpen)
	{
		GetWindowRect(m_rect);
		pOPDlg->SetWindowPos(NULL,m_rect.left+256,m_rect.top-118,-1,-1,SWP_NOSIZE);
		pOPDlg->ShowWindow(SW_SHOW);
		SetTimer(1,200,NULL);
		bIsOpBoardOpen=1;
	}
	else
	{
		pOPDlg->ShowWindow(SW_HIDE);
		bIsOpBoardOpen=0;
		KillTimer(1);
	}
}

void CCalculatorDlg::OnButtonFunc() 
{
	if(!bIsFuncBoardOpen)
	{
		GetWindowRect(m_rect);
		pFUNCDlg->SetWindowPos(NULL,m_rect.left,m_rect.top-167,-1,-1,SWP_NOSIZE);
		pFUNCDlg->ShowWindow(SW_SHOW);
		SetTimer(2,200,NULL);
		bIsFuncBoardOpen=1;
	}
	else
	{
		pFUNCDlg->ShowWindow(SW_HIDE);
		bIsFuncBoardOpen=0;
		KillTimer(2);
	}
}

void CCalculatorDlg::Paster(CString strSign,BOOL bIsSingle)
{
	UpdateData(1);
	CString str;
	m_nlen=m_strExp.GetLength();
	m_strExpc.GetSel(m_nStart,m_nEnd);
	str=m_strExp.Mid(m_nEnd,m_nlen-m_nEnd);
	m_strExp=m_strExp.Left(m_nStart)+strSign;
	m_nlen=m_strExp.GetLength();
	if(bIsSingle) m_nlen-=2;
	m_strExp+=str;
	UpdateData(0);
	m_strExpc.SetFocus();
	m_strExpc.SetSel(m_nlen,m_nlen);
}


BOOL CCalculatorDlg::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->wParam==HTCAPTION && pMsg->message==WM_NCLBUTTONDOWN && pMsg->hwnd==this->m_hWnd)
	{
		if(!bIsOk)
		{
			SetTimer(3,20,NULL);
	//		MessageBox("set");
			bIsOk=1;
		}
	}
	else
	{
		if(bIsOk)
		{
			KillTimer(3);
	//		MessageBox("kill");
			bIsOk=0;
		}	
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CCalculatorDlg::OnBtnConst() 
{
	UpdateData(1);
	CWnd * pFrame;					
	pFrame=AfxGetApp()->GetMainWnd();
	CWnd* pWndPopupOwner = this;
	VERIFY(m_menu.LoadMenu(IDR_MENU1));
	CPoint point;
	GetCursorPos(&point);
	::SetForegroundWindow(m_hWnd); 
	CMenu* pPopup = m_menu.GetSubMenu(0);
	pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,pWndPopupOwner,NULL);
	m_menu.DestroyMenu();	
}

void CCalculatorDlg::AddToHistr(CString strExp)
{
	if(m_menu2.m_hMenu)
	{
		int b=m_menu2.GetMenuItemCount();
		BOOL bRet=m_menu2.AppendMenu(MF_STRING,IMENU_ITEM_ID+b,strExp);
		if(bRet) m_menuItem.Add(strExp);
	}
}

void CCalculatorDlg::OnMenuSelect(UINT nID)
{
	int i=nID-IMENU_ITEM_ID;
	CString str=m_menuItem.GetAt(i);
	Paster(str,0);
}

void CCalculatorDlg::OnBtnHistrexp() 
{
	CPoint point;
	GetCursorPos(&point);
	m_menu2.TrackPopupMenu(TPM_CENTERALIGN,point.x,point.y,this);	
}

void CCalculatorDlg::OnMPi() 
{
	Paster("PI",0);
}

void CCalculatorDlg::OnMC() 
{
	Paster("LS",0);
}

void CCalculatorDlg::OnMNa() 
{
	Paster("NA",0);	
}

void CCalculatorDlg::OnMG() 
{
	Paster("GR",0);	
}

void CCalculatorDlg::OnMR() 
{
	Paster("MG",0);	
}

void CCalculatorDlg::OnMK() 
{
	Paster("BC",0);	
}

void CCalculatorDlg::OnMVm() 
{
	Paster("VM",0);	
}

void CCalculatorDlg::OnMKe() 
{
	Paster("EF",0);	
}

void CCalculatorDlg::OnME0() 
{
	Paster("DC",0);	
}

void CCalculatorDlg::OnME() 
{
	Paster("E",0);	
}

void CCalculatorDlg::OnBtnConvert() 
{
	UpdateData(1);
	CWnd * pFrame;					
	pFrame=AfxGetApp()->GetMainWnd();
	CWnd* pWndPopupOwner = this;
	VERIFY(m_menu.LoadMenu(IDR_MENU1));
	CPoint point;
	GetCursorPos(&point);
	::SetForegroundWindow(m_hWnd); 
	CMenu* pPopup = m_menu.GetSubMenu(1);
	pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,pWndPopupOwner,NULL);
	m_menu.DestroyMenu();		
}

void CCalculatorDlg::OnMHex() 
{
	m_strRes=m_strResTmp;
	calcu.Dec2Hex(&m_strRes);
	if(m_strRes.Compare(".")==0) m_strRes=m_strResTmp;
	UpdateData(0);
}

void CCalculatorDlg::OnMOct() 
{
	m_strRes=m_strResTmp;
	calcu.Dec2Oct(&m_strRes);
	if(m_strRes.Compare(".")==0) m_strRes=m_strResTmp;
	UpdateData(0);
}

void CCalculatorDlg::OnMBin() 
{
	m_strRes=m_strResTmp;
	calcu.Dec2Bin(&m_strRes);
	if(m_strRes.Compare(".")==0) m_strRes=m_strResTmp;
	UpdateData(0);
}

void CCalculatorDlg::OnMDec() 
{
	m_strRes=m_strResTmp;
	UpdateData(0);                                                                          
}

⌨️ 快捷键说明

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