编写MFC计算器
时间: 2023-08-13 18:03:11 浏览: 90
以下是一个简单的 MFC 计算器的代码:
首先,可以使用 MFC 库中的 CWnd 类来创建窗口和处理事件。我们需要创建一个派生自 CWnd 的类,我们称之为 CCalculatorWnd。
CCalculatorWnd.h 文件:
```cpp
#pragma once
class CCalculatorWnd : public CWnd
{
public:
CCalculatorWnd();
protected:
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
private:
CString m_strDisplay;
double m_dResult;
CString m_strOperator;
BOOL m_bDecimal;
BOOL m_bNewNumber;
BOOL m_bError;
};
```
CCalculatorWnd.cpp 文件:
```cpp
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CCalculatorWnd::CCalculatorWnd()
{
m_strDisplay = _T("0");
m_dResult = 0.0;
m_strOperator = _T("");
m_bDecimal = FALSE;
m_bNewNumber = TRUE;
m_bError = FALSE;
}
BEGIN_MESSAGE_MAP(CCalculatorWnd, CWnd)
ON_WM_CHAR()
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
END_MESSAGE_MAP()
void CCalculatorWnd::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (m_bError)
{
m_strDisplay = _T("0");
m_dResult = 0.0;
m_strOperator = _T("");
m_bDecimal = FALSE;
m_bNewNumber = TRUE;
m_bError = FALSE;
}
switch (nChar)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (m_bNewNumber)
{
m_strDisplay = _T("");
m_bNewNumber = FALSE;
}
m_strDisplay += nChar;
break;
case '.':
if (m_bNewNumber)
{
m_strDisplay = _T("0");
m_bNewNumber = FALSE;
}
if (!m_bDecimal)
{
m_strDisplay += nChar;
m_bDecimal = TRUE;
}
break;
case '+':
case '-':
case '*':
case '/':
if (!m_strOperator.IsEmpty())
{
OnLButtonDown(0, CPoint(0, 0));
}
m_bNewNumber = TRUE;
m_bDecimal = FALSE;
m_strOperator = nChar;
break;
case '=':
OnLButtonDown(0, CPoint(0, 0));
m_strOperator = _T("");
break;
case '\b':
if (m_strDisplay.GetLength() > 1)
{
m_strDisplay = m_strDisplay.Left(m_strDisplay.GetLength() - 1);
}
else
{
m_strDisplay = _T("0");
m_bNewNumber = TRUE;
}
break;
case 'c':
case 'C':
m_strDisplay = _T("0");
m_dResult = 0.0;
m_strOperator = _T("");
m_bDecimal = FALSE;
m_bNewNumber = TRUE;
break;
}
Invalidate();
}
void CCalculatorWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
double dOperand = _wtof(m_strDisplay);
if (m_strOperator.IsEmpty())
{
m_dResult = dOperand;
}
else
{
switch (m_strOperator[0])
{
case '+':
m_dResult += dOperand;
break;
case '-':
m_dResult -= dOperand;
break;
case '*':
m_dResult *= dOperand;
break;
case '/':
if (dOperand == 0.0)
{
m_strDisplay = _T("Error");
m_bError = TRUE;
return;
}
else
{
m_dResult /= dOperand;
}
break;
}
}
CString strResult;
strResult.Format(_T("%g"), m_dResult);
m_strDisplay = strResult;
m_bNewNumber = TRUE;
m_bDecimal = FALSE;
}
void CCalculatorWnd::OnPaint()
{
CPaintDC dc(this);
CRect rcClient;
GetClientRect(&rcClient);
CBrush brush;
brush.CreateSolidBrush(RGB(255, 255, 255));
dc.FillRect(&rcClient, &brush);
CFont font;
font.CreateFont(20, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, _T("Arial"));
dc.SelectObject(&font);
rcClient.DeflateRect(5, 5);
dc.DrawText(m_strDisplay, &rcClient, DT_RIGHT | DT_SINGLELINE | DT_VCENTER);
}
```
最后,在主函数中创建窗口并运行:
```cpp
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
AfxWinInit(hInstance, NULL, ::GetCommandLine(), 0);
CCalculatorWnd wnd;
wnd.Create(NULL, _T("Calculator"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 200, 200));
wnd.ShowWindow(nCmdShow);
wnd.UpdateWindow();
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return (int)msg.wParam;
}
```
现在,你就可以编译和运行代码,得到一个简单的 MFC 计算器。
阅读全文