vc++计算器源文件_第1页
vc++计算器源文件_第2页
vc++计算器源文件_第3页
vc++计算器源文件_第4页
vc++计算器源文件_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用 VC+实现计算器功能的部分代码/ calcdlg.cpp : implementation file / / This is a part of the Microsoft Foundation Classes C+ library. / Copyright (C) 1992-1998 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 “stdafx.h“ #include “mfccalc.h“ #include “calcdlg.h“ #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 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, “Accum“, GetAccum, SetAccum, VT_I4) DISP_PROPERTY_EX(CCalcDlg, “Operand“, GetOperand, SetOperand, VT_I4) DISP_PROPERTY_EX(CCalcDlg, “Operation“, GetOperation, SetOperation, VT_I2) DISP_PROPERTY_EX(CCalcDlg, “Visible“, GetVisible, SetVisible, VT_BOOL) DISP_FUNCTION(CCalcDlg, “Evaluate“, Evaluate, VT_BOOL, VTS_NONE) DISP_FUNCTION(CCalcDlg, “Clear“, Clear, VT_EMPTY, VTS_NONE) DISP_FUNCTION(CCalcDlg, “Display“, Display, VT_EMPTY, VTS_NONE) DISP_FUNCTION(CCalcDlg, “Close“, Close, VT_EMPTY, VTS_NONE) DISP_FUNCTION(CCalcDlg, “Button“, 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, “mfccalc.calculator“, 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; else m_accum /= m_operand; else if (m_operator = OpAdd) m_accum += m_operand; else if (m_operator = OpSubtract) m_accum -= m_operand; m_bOperandAvail = FALSE; UpdateDisplay(); void CCalcDlg:ClickedNumber(long l) if (m_errorState != ErrNone) return; if (!m_bOperandAvail) m_operand = 0L; SetOperand(m_operand*10+l); UpdateDisplay(); void CCalcDlg:UpdateDisplay() if (GetSafeHwnd() = NULL) return; CString str; if (m_errorState != ErrNone) str.LoadString(IDS_ERROR); else long lVal = (m_bOperandAvail) ? m_operand : m_accum; str.Format(_T(“%ld“), lVal); GetDlgItem(IDE_ACCUM)-SetWindowText(str); GetDlgItem(IDC_INVISIBLE_FOCUS)-SetFocus(); BEGIN_MESSAGE_MAP(CCalcDlg, CDialog) /AFX_MSG_MAP(CCalcDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_COMMAND_RANGE(IDB_0, IDB_9, OnClickedNumber) ON_BN_CLICKED(IDB_CLEAR, OnClickedClear) ON_BN_CLICKED(IDB_DIVIDE, OnClickedDivide) ON_BN_CLICKED(IDB_EQUAL, OnClickedEqual) ON_BN_CLICKED(IDB_MINUS, OnClickedMinus) ON_BN_CLICKED(IDB_PLUS, OnClickedPlus) ON_BN_CLICKED(IDB_TIMES, OnClickedTimes) ON_EN_SETFOCUS(IDE_ACCUM, OnSetFocusAccum) /AFX_MSG_MAP END_MESSAGE_MAP() / / CCalcDlg message handlers BOOL CCalcDlg:OnInitDialog() CDialog:OnInitDialog(); / Add “About.“ menu item to system menu. / IDM_ABOUTBOX must be in the system command range. ASSERT(IDM_ABOUTBOX ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR); pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); pSysMenu-RemoveMenu(SC_MAXIMIZE, MF_BYCOMMAND); pSysMenu-RemoveMenu(SC_SIZE, MF_BYCOMMAND); / want focus to stay on the dialog itself (until a button is clicked) SetFocus(); return FALSE; void CCalcDlg:OnSysCommand(UINT nID, LPARAM lParam) if (nID 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 CCalcDlg:OnPaint() if (!IsIconic() CDialog:OnPaint(); return; 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( int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; / Draw the icon dc.DrawIcon(x, y, m_hIcon); / The system calls this to obtain the cursor to display while the user drags / the minimized window. HCURSOR CCalcDlg:OnQueryDragIcon() return (HCURSOR)m_hIcon; void CCalcDlg:OnClickedNumber(UINT nID) ASSERT(nID = IDB_0 / / CCalcDlg automation BOOL CCalcDlg:RegisterActive() / attempt to register as the active object for the CCalcDlg CLSID return RegisterActiveObject(GetInterface( long CCalcDlg:GetAccum() return m_accum; void CCalcDlg:SetAccum(long nNewValue) m_accum = nNewValue; long CCalcDlg:GetOperand() return m_operand; void CCalcDlg:SetOperand(long nNewValue) m_operand = nNewValue; m_bOperandAvail = TRUE; short CCalcDlg:GetOperation() return m_operator; void CCalcDlg:SetOperation(short nNewValue) m_operator = (Operator)nNewValue; BOOL CCalcDlg:GetVisible() return m_hWnd != NULL void CCalcDlg:SetVisible(BOOL bNewValue) if (bNewValue = GetVisible() return; if (bNewValue) / create it if necessary if (m_hWnd = NULL / set up as the active window for the application

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论