Solved

calculator program

Posted on 2004-04-23
18
340 Views
Last Modified: 2013-11-20
I have a couple of questions regading a little program I'm working on. You can find it here www.justinblunk.com. My first real attempt at MFC. It's just a little calculator program. Here are my questions:

1. I sent this exe file to someone using a Mac and it told them there were no default app for this aplication. Is there something I can do when building this that will fix that?

2. Currently it has two edit fields to receive the numbers. I would like to change this so there are buttons 0-9, like a real calculator, but I need some help with how that is done.

3. Instead of putting in the numbers and then hitting the +,-, etc. buttons to see the answer I would like to be able to push 1 + 1, etc. and then = to display the answer.

4. How can I make the answer text in the disabled edit box be black on display instead of that gray color?

thanks, justin
0
Comment
Question by:jandhb
  • 7
  • 7
  • 4
18 Comments
 
LVL 13

Expert Comment

by:SteH
Comment Utility
2) Is it a dialog based app? Just add buttons to the dialog and make their caption the numbers.
Add handlers for button clicks which append the digits to a string until an operator button is pressed.

3) Add buttons for +,-,=, etc
Inside handlers of onclick events store current string as first argument if operator was pressed and store operator.
When = was pressed store current string a second operator and perform calculation.

4)In a dialog based app I have black on white as default. Are you using a edit control or a static control? latter displays it black on gray.
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
1) What should be a default application for an exe? An executable can be executed so it does not need another application. But if compiled for Intel it won't run on a Mac.
0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
2. It is a dialog based app. You can find it here - www.justinblunk.com. I know how to  add the buttons, change their caption, and add handlers. Help me to do "append the digits to a string until an operator button is pressed", please.

3. Can you give me an example here?

4. I just used a edit box and set its properties as "disabled". It has a gray background that displays gray text which is a little hard to see. How do I change it?

1. Well, that is the message they are getting on the Mac. I sent them the icon that looks like threee blocks, blue MFC blocks, and it has type application. That is the right one, right? Maybe I need to build it another way?? I'm just using the release instead of debug.
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Source code would help more than an executable. Don't want to run an exe from somewhere.

But add three CString variables to your dlg class (for each argument one and one temporary). In the on click handler you just add the digit to the existing string:

m_csTemp += _T ("0");  // for the 0 button handler.

Easiest way to store operator would be to declare an enum with all operations you want to support
kOperatorNone = 0, kOperatorAdd, ...
and add an int member var to store the operator

+ handler:

m_csArg1 = m_csTemp; // copy temp object to operator string. Check for consistency of input would be very wise here.
m_csTemp = _T (""); // clear temp
m_iOperator = kOperatorAdd;

And = handler:
m_csArg2 = m_csTemp; // check again.
double dArg1 = atof (m_csArg1);
double dArg2 = atof (m_csArg2);
double dResult;
switch (m_iOperator) {
   case kOperatorAdd:
         dResult = dArg1 + dArg2;
   break;
   case kOperatorSub:
         dResult = dArg1 - dArg2;
   break;
   ...
}
m_csResult.Format ("%f", dResult); // change format to your liking.

0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
Here is what I have so far...Need some help :), but I'll get there...This is my attempt at this...so, please be patient. This code is in the CAboutDlg( ).

// buttondemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "buttondemo.h"
#include "buttondemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char 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

      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CAboutDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      //{{AFX_MSG(CAboutDlg)
      //}}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()

/////////////////////////////////////////////////////////////////////////////
// CButtondemoDlg dialog

CButtondemoDlg::CButtondemoDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CButtondemoDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CButtondemoDlg)
      m_nAnswer = 0;
      //}}AFX_DATA_INIT
      // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CButtondemoDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CButtondemoDlg)
      DDX_Control(pDX, IDC_BUTTONEQUAL, m_nEQUAL);
      DDX_Control(pDX, IDC_BUTTON0, m_n0);
      DDX_Control(pDX, IDC_BUTTON01, m_n01);
      DDX_Control(pDX, IDC_BUTTON02, m_n02);
      DDX_Control(pDX, IDC_BUTTON03, m_n03);
      DDX_Control(pDX, IDC_BUTTON04, m_n04);
      DDX_Control(pDX, IDC_BUTTON05, m_n05);
      DDX_Control(pDX, IDC_BUTTON06, m_n06);
      DDX_Control(pDX, IDC_BUTTON07, m_n07);
      DDX_Control(pDX, IDC_BUTTON08, m_n08);
      DDX_Control(pDX, IDC_BUTTON09, m_n09);
      DDX_Text(pDX, IDC_EDIT_ANSWER, m_nAnswer);
      //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CButtondemoDlg, CDialog)
      //{{AFX_MSG_MAP(CButtondemoDlg)
      ON_WM_SYSCOMMAND()
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
      ON_BN_CLICKED(IDC_BUTTON_MINUS, OnButtonMinus)
      ON_BN_CLICKED(IDC_BUTTON_DIVIDE, OnButtonDivide)
      ON_BN_CLICKED(IDC_BUTTON_MULTIPLY, OnButtonMultiply)
      ON_BN_CLICKED(IDC_BUTTON0, OnButton0)
      ON_BN_CLICKED(IDC_BUTTON01, OnButton01)
      ON_BN_CLICKED(IDC_BUTTON02, OnButton02)
      ON_BN_CLICKED(IDC_BUTTON03, OnButton03)
      ON_BN_CLICKED(IDC_BUTTON04, OnButton04)
      ON_BN_CLICKED(IDC_BUTTON05, OnButton05)
      ON_BN_CLICKED(IDC_BUTTON06, OnButton06)
      ON_BN_CLICKED(IDC_BUTTON07, OnButton07)
      ON_BN_CLICKED(IDC_BUTTON08, OnButton08)
      ON_BN_CLICKED(IDC_BUTTON09, OnButton09)
      ON_BN_CLICKED(IDC_BUTTONEQUAL, OnButtonequal)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CButtondemoDlg message handlers

BOOL CButtondemoDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      // Add "About..." menu item to system menu.

      // IDM_ABOUTBOX must be in the system command range.
      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);

      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
      {
            CString strAboutMenu;
            strAboutMenu.LoadString(IDS_ABOUTBOX);
            if (!strAboutMenu.IsEmpty())
            {
                  pSysMenu->AppendMenu(MF_SEPARATOR);
                  pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
            }
      }

      // 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
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}

void CButtondemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
      if ((nID & 0xFFF0) == IDM_ABOUTBOX)
      {
            CAboutDlg dlgAbout;
            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 CButtondemoDlg::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 CButtondemoDlg::OnQueryDragIcon()
{
      return (HCURSOR) m_hIcon;
}

void CButtondemoDlg::OnButtonAdd()
{
      // TODO: Add your control notification handler code here

      //UpdateData (TRUE);
      //m_nAnswer = m_nX + m_nY;
      //UpdateData (FALSE);
      m_csArg1 = m_csTemp; // copy temp object to operator string. Check for consistency of input would be very wise here.
      m_csTemp = _T (""); // clear temp
      m_iOperator = kOperatorAdd;


}

void CButtondemoDlg::OnButtonMinus()
{
      // TODO: Add your control notification handler code here
      //UpdateData (TRUE);
      //m_nAnswer = m_nX - m_nY;
      //UpdateData (FALSE);
}

void CButtondemoDlg::OnButtonDivide()
{
      
      //UpdateData (TRUE);
      //m_nAnswer = m_nX / m_nY;
      //UpdateData (FALSE);
      
}

void CButtondemoDlg::OnButtonMultiply()
{

      //UpdateData (TRUE);
      //m_nAnswer = m_nX * m_nY;
      //UpdateData (FALSE);      
}

void CButtondemoDlg::OnButton0()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("0");
}

void CButtondemoDlg::OnButton01()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("1");
}

void CButtondemoDlg::OnButton02()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("2");
}

void CButtondemoDlg::OnButton03()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("3");
}

void CButtondemoDlg::OnButton04()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("4");
}

void CButtondemoDlg::OnButton05()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("5");
}

void CButtondemoDlg::OnButton06()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("6");
}

void CButtondemoDlg::OnButton07()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("7");
}

void CButtondemoDlg::OnButton08()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("8");
}

void CButtondemoDlg::OnButton09()
{
      // TODO: Add your control notification handler code here
      m_csTemp += _T ("9");
}

void CButtondemoDlg::OnButtonequal()
{
      // TODO: Add your control notification handler code here
      m_csArg2 = m_csTemp; // check again.
      double dArg1 = atof (m_csArg1);
      double dArg2 = atof (m_csArg2);
      double dResult;
      switch (m_iOperator) {
   case kOperatorAdd:
         dResult = dArg1 + dArg2;
   break;
   case kOperatorSub:
         dResult = dArg1 - dArg2;
   break;
}
0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
are you still able to help now?
0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
where/what data type do I delclare the m_csArg1 and m_csTemp?
0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
m_csTemp = _T ("");

On this line I get an erro saying, cannot convert from char [1] to int.
0
 
LVL 14

Expert Comment

by:wayside
Comment Utility
> where/what data type do I delclare the m_csArg1 and m_csTemp?

Put these in the header file for your class:

CString m_csArg1;
CString m_csTemp;

Initialize them in your constructor.

> On this line I get an erro saying, cannot convert from char [1] to int.

This error should go away once you add declarations for the variables.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:wayside
Comment Utility
As far as the disabled edit box - if you want a regular-looking edit box but don't want to allow the user to type in it, do the following:

-remember what is in there somewhere, like in a class member variable
-add a message handler for EN_CHANGE for the edit box, and blast in the value you have stored. This makes it appear that the user can't type in there.
0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
wayside, would you mind taking a look at what I have so far? Can i send you the files via email? This is my first attempt at MFC and really need some help.

I appreciate the help. I know this is a simple calculator, but when its your first time at this it can be difficult. You know what I mean?

BTW - what header file is it. I have buttondemo.h and buttondemoDlg.h.

Just let me know if I can send them to you or tallk via msn.

Thanks.
0
 
LVL 14

Accepted Solution

by:
wayside earned 50 total points
Comment Utility
> Just let me know if I can send them to you or tallk via msn.

I don't do IM, but you can email me stuff at cwaycwaycway@yahoo.com, I will try to take a look.

Before you do, though, you should write down exactly what the flow of how calculator will look and work. For example:

-The calculator will have 10 buttons with the digits 0-9
-It will have 4 buttons for operations +-/*
-It will have an = button
-It will have a text box to display the current number and the answer
-User enters the first number by pushing the digit buttons, the number is shown in the edit box
-When the user presses an operator, save the first number in a subtotal and remember the operator that was pressed.
-When user enters second number, show it in edit box.
-If user presses equal, perform subtotal lastoperator secondnumber and display in text box, then clean subtotal
-If user presses another operation, perform subtotal lastoperator secondnumber and save in subtotal. Remember operator.

etc.

Only when you have a clear statement of how your program will operate, can you code it up.

0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Sorry was away for some days. Back to help now.

The handler for Add looks OK as the ones for digits seem to be OK. Missing points are the handlers for the other operations. And in the headre file you need to declare m_csTemp, m_csArg1, m_csArg2 and the enum with the supported operators.
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Instead of putting the compiled code to you webspace you could put there the sources. This would allow all experts to have a look at the code. Using private mail is not allowed since only the one expert who got the e-mail can help from thereon. If you don't want all you sources publically available yo can put it into a directory which is not linked from anywhere in your homepage. Publish the links only here on experts exchange.
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Was the answer really the accepted comment or some help using private email? (latter would violate membership agreements.)
0
 
LVL 14

Expert Comment

by:wayside
Comment Utility
Yes, I traded a few e-mails with the OP.

At the time I offered help via email, I did not know that this was against the rules. The "membership agreement" you cite( http://www.experts-exchange.com/memberAgreement.jsp ) has nothing in it that I see that relates to this, except perhaps for a nebulous, non-specific mention of "Misusing the Experts Exchange point system". It did not occur to me at the time that trading emails might constitute an "abuse" of the point system.

I did later find an explicit mention about this on the Help page, about 50 screens down from the top, so clearly I am in the wrong.

So I apologize for my ignorance of the rules, and for not reading every line of every page on this site regarding its operation.

If you feel strongly enough about it, I urge you to contact the page mod or an administrator, and file a complaint. Should the powers that be decide to revoke the points, I will not protest.

Regards,

-Charlie




0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
No I don't mind. But the problem why this rule exist is explained:
One expert with e-mail contact has better chances to give the answer.

But more important I find that the now closed Q is stored in a database. When you later search for it and see the comments you won't find the real answer. That's why I complain. I don't see how the selected comment answers his problems. It would be nice (from the author) to explain how the problem was fixed to allow others (who pay for the search facility) to profit from the solution as well.
0
 
LVL 1

Author Comment

by:jandhb
Comment Utility
Steh,

Hi. I was the author of this post.

1. Sorry for emailing with Charlie regarding this. I also did not realize the aspect and impact of searching for the answer like your saying. I apologize.

2. The answer that was selected is not exactly what "fixed" the problem, but it did provide a logical path that made sense to me. In fact, the reason why I closed this question was because I went to post another one regading a completely different topic and was told I needed to take care of the open questions I had. If someone did not tell me that I would have probably left it open. No big deal because the original post has been addressed by a resource a part from the boards here. With that said here is the code that I currently have. It is not completed yet. Here though are some suggestions to make it better (per Charlie via email):

- create a Calculator class, which is responsible for handling all of the work of the calculator. Add member functions to handle the operation of the calculator, like...

KeyPressed(KeyValue key)  // <- KeyValue would be an enumerated type of all possible keys
GetCurrentDisplayValue() // <- returns what shows in the display
etc.

Then all the UI has to do is toss key presses to the Calculator, and after every key press get the value to display and display it.

- don't use globals. Make them members of the class.
 
- take advantage of the ClassWizard and create variables for things like the edit box.
--------------------------------
Here is the existing code:

// CalcDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calc.h"
#include "CalcDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// Defines

#define NONE            0
#define NUMBER            1
#define OPERATION   2
#define      EQUALS            3
#define CLEAR            4

#define OP_NONE            0
#define OP_MINUS      1
#define OP_PLUS            2
#define OP_MULT            3

/////////////////////////////////////////////////////////////////////////////
// Globals

long int g_GlobalValue = 0;
long int g_ModifyValue = 0;

short int g_LastOperation = OP_NONE;
short int g_LastEvent = NUMBER;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
      CAboutDlg();

// Dialog Data
      //{{AFX_DATA(CAboutDlg)
      enum { IDD = IDD_ABOUTBOX };
      //}}AFX_DATA

      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CAboutDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      //{{AFX_MSG(CAboutDlg)
      //}}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()

/////////////////////////////////////////////////////////////////////////////
// CCalcDlg dialog

CCalcDlg::CCalcDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CCalcDlg::IDD, pParent)
{
      //{{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);
}

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
}

BEGIN_MESSAGE_MAP(CCalcDlg, CDialog)
      //{{AFX_MSG_MAP(CCalcDlg)
      ON_WM_SYSCOMMAND()
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_BN_CLICKED(IDC_VALUE1, OnValue1)
      ON_BN_CLICKED(IDC_VALUE2, OnValue2)
      ON_BN_CLICKED(IDC_VALUE3, OnValue3)
      ON_BN_CLICKED(IDC_VALUE4, OnValue4)
      ON_BN_CLICKED(IDC_VALUE5, OnValue5)
      ON_BN_CLICKED(IDC_VALUE6, OnValue6)
      ON_BN_CLICKED(IDC_VALUE7, OnValue7)
      ON_BN_CLICKED(IDC_VALUE8, OnValue8)
      ON_BN_CLICKED(IDC_VALUE9, OnValue9)
      ON_BN_CLICKED(IDC_ADD, OnAdd)
      ON_BN_CLICKED(IDC_EQUALS, OnEquals)
      ON_BN_CLICKED(IDC_SUB, OnSub)
      ON_BN_CLICKED(IDC_MULT, OnMult)
      //}}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 & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);

      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
      {
            CString strAboutMenu;
            strAboutMenu.LoadString(IDS_ABOUTBOX);
            if (!strAboutMenu.IsEmpty())
            {
                  pSysMenu->AppendMenu(MF_SEPARATOR);
                  pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
            }
      }

      // 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
      
      // show initial value
      UpdateTotal();
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCalcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
      if ((nID & 0xFFF0) == IDM_ABOUTBOX)
      {
            CAboutDlg dlgAbout;
            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())
      {
            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 CCalcDlg::OnQueryDragIcon()
{
      return (HCURSOR) m_hIcon;
}

// Updates the Total box with the current value
void CCalcDlg::UpdateTotal()
{
      char Buffer[30];
      sprintf(Buffer,"%d",g_GlobalValue);
      SetDlgItemText(IDC_EDITTOTAL,Buffer);
}

// Updates the Total box with modify value
void CCalcDlg::UpdateModify()
{
      char Buffer[30];
      sprintf(Buffer,"%d",g_ModifyValue);
      SetDlgItemText(IDC_EDITTOTAL,Buffer);
}

void CCalcDlg::OnValue1()
{
      NumberPressed(1);
}

void CCalcDlg::OnValue2()
{
      NumberPressed(2);
}

void CCalcDlg::OnValue3()
{
      NumberPressed(3);
}

void CCalcDlg::OnValue4()
{
      NumberPressed(4);
}

void CCalcDlg::OnValue5()
{
      NumberPressed(5);
}

void CCalcDlg::OnValue6()
{
      NumberPressed(6);
}

void CCalcDlg::OnValue7()
{
      NumberPressed(7);
}

void CCalcDlg::OnValue8()
{
      NumberPressed(8);
}

void CCalcDlg::OnValue9()
{
      NumberPressed(9);
}

void CCalcDlg::OnAdd()
{
  if (g_LastEvent == NUMBER) {
    switch (g_LastOperation) {
      case OP_PLUS:  g_GlobalValue += g_ModifyValue;
                     break;
      case OP_MINUS: g_GlobalValue -= g_ModifyValue;
                     break;
      case OP_MULT:  g_GlobalValue *= g_ModifyValue;
                     break;
      case OP_NONE:  g_GlobalValue =  g_ModifyValue;
                     break;

    }
  }

      g_LastOperation = OP_PLUS;
      g_LastEvent = OPERATION;
//      g_GlobalValue = g_ModifyValue;
      g_ModifyValue = 0;
      UpdateTotal();      
}

void CCalcDlg::NumberPressed(int Number)
{
      switch(g_LastEvent)
      {
      case NUMBER:
            {

            }
      case OPERATION:
            {
                  
            }
      case EQUALS:
            {
      if (g_LastEvent == EQUALS) {
        g_GlobalValue = 0;
      }
                  g_ModifyValue*=10;
                  g_ModifyValue+=Number;                        
            }break;
      case CLEAR:
            {
                  
            }break;

      default: break;
      }

      UpdateModify();
      g_LastEvent = NUMBER;
}

void CCalcDlg::OnEquals()
{
      switch(g_LastEvent)
      {
      case NUMBER:
            {
                  if(g_LastOperation == OP_PLUS)
                        g_GlobalValue+=g_ModifyValue;
                  else if (g_LastOperation == OP_MINUS)
                        g_GlobalValue-=g_ModifyValue;
                  else if (g_LastOperation == OP_MULT)
                        g_GlobalValue*=g_ModifyValue;

                  g_ModifyValue = 0;
            }break;
      case OPERATION:
            {
                  
            }break;
      case EQUALS:
            {
            
            }break;
      case CLEAR:
            {
                  
            }break;

      default: break;
      }

      UpdateTotal();
      g_LastEvent = EQUALS;
//      g_GlobalValue = 0;
}

void CCalcDlg::OnSub()
{
      g_GlobalValue=g_ModifyValue;
      g_ModifyValue = 0;
      g_LastOperation = OP_MINUS;
      g_LastEvent = OPERATION;
      UpdateTotal();
}

void CCalcDlg::OnMult()
{
      g_GlobalValue=g_ModifyValue;
      g_ModifyValue = 0;
      g_LastOperation = OP_MULT;
      g_LastEvent = OPERATION;
      UpdateTotal();
      
}



0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 52
Smart Camera scanning and reading information 3 86
lucky13 challenge 11 111
Change to event 1 72
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now