MFC计算器 ---- 进阶版

8 篇文章 0 订阅

执行之后的效果图

在这里插入图片描述
源码https://download.csdn.net/download/sinat_38626955/12647267
模仿Win10自带的计算器,搞一个好看点的计算器
在这里插入图片描述
虽然功能和之前发的简单计算器没什么差别[https://download.csdn.net/download/sinat_38626955/12535865]

代码

思想:
声明两个CString strNum1 & strNum2分别表示两个需要计算的数字
声明一个CString 保存操作符
当strNum2不为空时(!= “”)调用OnOperatorEqual()进行计算
主要需要注意:
除号的运算,正负号,小数点,连续运算(1+2+3)
以下主要贴出如何编辑对话框,按钮,字体等代码,毕竟计算机运算代码还是比较简单的,就不贴了。

以下代码为构造函数:

// 在构造函数中进行初始化
CCalculatorDialogDlg::CCalculatorDialogDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculatorDialogDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculatorDialogDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_strNum1 = "";
	m_strNum2 = "";
	m_strAuxShow = "";
	m_strShow = "0";
	// Set Font
	m_fontShow.CreatePointFont(245, "Microsoft YaHei UI"); // Show区域的字体
	m_fontAuxShow.CreatePointFont(180, "Microsoft YaHei UI"); // AuxShow区域的字体
	m_fontBtnOpnd.CreatePointFont(150, "Microsoft YaHei UI"); // 按钮上数字的字体
	m_fontBtnOptr.CreatePointFont(150, "宋体"); // 其他符号字体
	
	// Set the background color of Dialog
	m_brush.CreateSolidBrush(RGB(230, 230, 230)); // 计算器背景颜色

	m_bEnter = TRUE; // 用来判断是否将Show字段加入到AuxShow中
	m_bDoEqual = FALSE; // 是否调用等于函数
	//
}

该函数是用于交换数据
看不明白的👉戳这里👈

void CCalculatorDialogDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDialogDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	DDX_Text(pDX, IDC_AUXILIARY_SHOW, m_strAuxShow);
	DDX_Text(pDX, IDC_ENTER_AND_SHOW, m_strShow);
	//}}AFX_DATA_MAP
}

用于修改DC的属性
以下修改字体以及AuxShow区域的字体颜色,并设置字体背景透明

HBRUSH CCalculatorDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	// Set Font and Transparent Background
	if (IDC_AUXILIARY_SHOW == pWnd->GetDlgCtrlID())
	{
		pDC->SetTextColor(RGB(150, 150, 150));
		pDC->SelectObject(&m_fontAuxShow);
	}
	else
	{
		pDC->SelectObject(&m_fontShow);
	}
	pDC->SetBkMode(TRANSPARENT);

	// TODO: Return a different brush if the default is not desired
	//return hbr;
	return m_brush;
}

绘制对话框

void CCalculatorDialogDlg::LayOut()
{
	// Set Transparent Dialog
	// 设置对话框透明度
	SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst = LoadLibrary("User32.DLL");  // Loading DLL
	if(hInst) 
	{ 
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD); 
		MYFUNC func = NULL;	// the Pointer of Function
		// Get the Pointer of SetLayeredWindowAttributes()
		func = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
		// Use SetLayeredWindowAttributes to Set Transparency
		if(func)func(this->GetSafeHwnd(), RGB(0, 0, 0), 240, 0x2); 
		FreeLibrary(hInst); 
	}
//设置按钮Style
...
...
}

接上一段代码块省略部分
调用该函数设置每个按钮
不是很明白CButtonST的可以👉戳这里👈

void CCalculatorDialogDlg::SetButtonStyle(int index, UINT nID, CFont &font, COLORREF crColorOut, COLORREF crColorIn, COLORREF crColorFocus)
{
	// Set the Background Style of Button
	m_btnSTOperate[index].SubclassDlgItem(nID, this);
	m_btnSTOperate[index].DrawBorder(FALSE);	// Cancel Border
	m_btnSTOperate[index].SetColor(CButtonST::BTNST_COLOR_BK_OUT, crColorOut); 
	m_btnSTOperate[index].SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, crColorFocus);
	m_btnSTOperate[index].SetColor(CButtonST::BTNST_COLOR_BK_IN, crColorIn); 
	GetDlgItem(nID)->SetFont(&font);
}

最后贴一段等于函数的代码
太多了,不想贴,懒

void CCalculatorDialogDlg::OnOperatorEqual() 
{
	// TODO: Add your control notification handler code here
	RefreshShow();

	if (m_strNum2 == "")
	{
		return;
	}

	if (m_strOperator == '/' && m_strNum2 == "0")
	{
		MessageBox("除数不能为零");
	}
	double dblNum1 = atof(m_strNum1);
	double dblNum2 = atof(m_strNum2);
	char ch = m_strOperator[0];
	switch (ch)
	{
	case '+':
		m_strShow.Format("%lf", dblNum1 + dblNum2);
		break;
	case '-':
		m_strShow.Format("%lf", dblNum1 - dblNum2);
		break;
	case '*':
		m_strShow.Format("%lf", dblNum1 * dblNum2);
		break;
	case '/':
		m_strShow.Format("%lf", dblNum1 / dblNum2);
		break;
	}
	if (m_strShow.Find('.') != -1)
	{
		m_strShow.TrimRight("0"); // Remove the extra zero and point
		m_strShow.TrimRight("."); 
	}
	m_strAuxShow = m_strShow;
	m_strNum1 = m_strShow;
	m_strNum2 = "";
	m_strOperator = "";

	UpdateData(FALSE);
}

最后的最后
代码最后这几个函数是用于修改Windows计算机窗口
该例没有启用,不知道留着要干嘛
在这里插入图片描述

void CCalculatorDialogDlg::OnNcPaint() 
BOOL CCalculatorDialogDlg::OnNcActivate(BOOL bActive) 
DWORD CCalculatorDialogDlg::HitTest(CPoint pt)
void CCalculatorDialogDlg::OnNcLButtonDown(UINT nHitTest, CPoint point) 
void CCalculatorDialogDlg::OnLButtonUp(UINT nFlags, CPoint point) 
UINT CCalculatorDialogDlg::OnNcHitTest(CPoint point) 
//若想用只需将找到	//ON_WM_NCPAINT() 该注释,将注释取消掉即可

如果哪里写的不好求大佬带带在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值