内容发布更新时间 : 2024/11/15 4:06:54星期一 下面是文章的全部内容请认真阅读。
用VC++实现计算器功能的部分代码 // calcdlg.cpp : implementation file // This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-2019 Microsoft Corporation // All rights reserved. // This source code is only intended as a supplement to the // Microsoft Foundation Classes Reference and related // electronic documentation provided with the library. // See these sources for detailed information regarding the // Microsoft Foundation Classes product. #include \ #include \ #include \ #ifdef _DEBUG #undef THIS_FILE static char BASED_CODE 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 // Implementation protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //{{AFX_MSG(CAboutDlg) virtual BOOL OnInitDialog(); //}}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 第 1 页
END_MESSAGE_MAP() // CAboutDlg message handlers BOOL CAboutDlg::OnInitDialog() CDialog::OnInitDialog(); // TODO: Add extra initialization here return TRUE; // CCalcDlg dialog IMPLEMENT_DYNCREATE(CCalcDlg, CDialog) BEGIN_DISPATCH_MAP(CCalcDlg, CDialog) //{{AFX_DISPATCH_MAP(CCalcDlg) DISP_PROPERTY_EX(CCalcDlg, \, GetAccum, SetAccum, VT_I4) DISP_PROPERTY_EX(CCalcDlg, \, GetOperand, SetOperand, VT_I4) DISP_PROPERTY_EX(CCalcDlg, \, GetOperation, SetOperation, VT_I2) DISP_PROPERTY_EX(CCalcDlg, \, GetVisible, SetVisible, VT_BOOL) DISP_FUNCTION(CCalcDlg, \, Evaluate, VT_BOOL, VTS_NONE) DISP_FUNCTION(CCalcDlg, \, Clear, VT_EMPTY, VTS_NONE) DISP_FUNCTION(CCalcDlg, \, Display, VT_EMPTY, VTS_NONE) DISP_FUNCTION(CCalcDlg, \, Close, VT_EMPTY, VTS_NONE) DISP_FUNCTION(CCalcDlg, \, Button, VT_BOOL, VTS_BSTR) //}}AFX_DISPATCH_MAP END_DISPATCH_MAP() #ifndef IMPLEMENT_OLECREATE_SINGLE // MFC will provide this macro in the future. For now, we define it. #define IMPLEMENT_OLECREATE_SINGLE(class_name, external_name, \\ l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \\ AFX_DATADEF COleObjectFactory class_name::factory(class_name::guid, \\ RUNTIME_CLASS(class_name), TRUE, _T(external_name)); \\ const AFX_DATADEF GUID class_name::guid = \\ { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }; #endif // {62C4DD10-F45E-11cd-8C3D-00AA004BB3B7} IMPLEMENT_OLECREATE_SINGLE(CCalcDlg, \, 第 2 页
0x62c4dd10, 0xf45e, 0x11cd, 0x8c, 0x3d, 0x0, 0xaa, 0x0, 0x4b, 0xb3, 0xb7); CCalcDlg::CCalcDlg(CWnd* pParent /*=NULL*/) : CDialog(CCalcDlg::IDD, pParent) m_bAutoDelete = TRUE; // default to auto-delete m_dwRegister = 0; // not registered as active by default //{{AFX_DATA_INIT(CCalcDlg) // 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); // Note that LoadAccelerator does not require DestroyAcceleratorTable m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD)); // clear the contents of the calculator and reset state OnClickedClear(); // enable this object for OLE automation EnableAutomation(); CCalcDlg::~CCalcDlg() if (m_dwRegister != 0) RevokeActiveObject(m_dwRegister, NULL); void CCalcDlg::DoDataExchange(CDataExchange* pDX) CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCalcDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP // CCalcDlg implementation void CCalcDlg::PerformOperation() if (m_errorState != ErrNone) return; if (m_bOperandAvail) if (m_operator == OpNone) m_accum = m_operand; else if (m_operator == OpMultiply) m_accum *= m_operand; else if (m_operator == OpDivide) if (m_operand == 0) m_errorState = ErrDivideByZero; 第 3 页