MENU, ACCELARATOR, TOOLTIP, TOOLBAR | create toolbar and perform addition ,multiplication,division,and substraction using vc++

Friday, May 28, 2010

OBJECTIVE:
To create menu,accelerator,toolbars with the help of them the operation such as addition ,multiplication,division and subtraction are to be carried out in a dialog using various control with the help of vc++.
PROCEDURE:
1. Run MFCAPPwizard(exe) to generate c:\Menu.
2. In step1 select single document interface.
3. Deselect the print and printview option.
4. Select all the default settings and press ok button.
5. In the resource view we had acceleration menu etc.
6. By clicking on the menu(IDR_MAINFRAMES),create our need menu namely calculate then the submenu items are add,mul,div,sub.
7. Then by clicking on the toolbar we get icon create our needed icons such as + , - , / , *.
8. When we use right click on the created items, we get properties dialog boxes.
9. In the properties dialog box, change the ID names as follows.
ID_CALCULATE_ADD,ID_CALCULATE_DIV,ID_CALCULATE_SUB,ID_CALCULATE_MUL,add the tooltip text on the prompt box prefixed by.
10. Likewise also name the toolbar icons in the same manner with the same ID.
11. In the accelerator keys option(resource view) select your own combination of keys for the operation and also create the same ID for them.
12. Using the resource view,right click on the dialog and select the new dialog create three edit controls for the operations to be done in them,leavethe IDs as such.
13. View option -> classwizard -> add a new class namely CCalcDlg.
14. Create member variable for the edit controls are using add variables option,Edit1,Edit2,Edit3 having m_n1, m_n2, m_n3 respectively with float data type.
15. Add menu function for the edit controls by mapping all the IDs(ID_CALCULATE_ADD etc)to the message command.
16. In the class view include the header file #include”calcDlg.h” in the menu view .cpp file.
17. In the clacDlg.h add the following lines in the class
class CCalDlg : public CDialog
{
// Construction
public:
CCalDlg(CWnd* pParent = NULL); // standard constructor
void op(int a);
int m_opera;
18. In the calcDlg.cpp add the following lines in message handler code
void CCalDlg::op(int a)
{
m_opera=a;
}
19. In the Menuview.cpp add our command handler code as following,
void CMenuView::OnCalculateAdd()
{
CCalDlg Dlg;
Dlg.op(1);
Dlg.DoModal();
}
void CMenuView::OnCalculateDiv()
{
CCalDlg Dlg;
Dlg.op(2);
Dlg.DoModal();
}
void CMenuView::OnCalculateMul()
{
CCalDlg Dlg;
Dlg.op(3);
Dlg.DoModal();
}
void CMenuView::OnCalculateSub()
{
CCalDlg Dlg;
Dlg.op(4);
Dlg.DoModal();
}
20. In the classwizard map the message EN_KILLFOCUS to the IDC_EDIT2 control,since at the end of the edit 2 control,since at the end of the edit2 control,since at the end of the edit2 the result have to be displayed.
21. After mapping the message edit the code to the function in calDlg.cpp as ,
void CCalDlg::OnKillfocusEdit2()
{
switch(m_opera)
{
case 1:
UpdateData(true);
m_n3=m_n1+m_n2;
UpdateData(false);
break;
case 2:
UpdateData(true);
m_n3=m_n1-m_n2;
UpdateData(false);
break;
case 3:
UpdateData(true);
m_n3=m_n1*m_n2;
UpdateData(false);
break;
case 4:
UpdateData(true);
m_n3=m_n1/m_n2;
UpdateData(false);
break;
}
}
22. Compile and test the application.
PROGRAMS :
SOURCE FILES :

CalcDlg.cpp :
// CalDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Menu.h"
#include "CalDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCalDlg dialog
CCalDlg::CCalDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalDlg)
m_n1 = 0.0f;
m_n2 = 0.0f;
m_n3 = 0.0f;
//}}AFX_DATA_INIT
}
void CCalDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalDlg)
DDX_Text(pDX, IDC_EDIT1, m_n1);
DDX_Text(pDX, IDC_EDIT2, m_n2);
DDX_Text(pDX, IDC_EDIT3, m_n3);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalDlg, CDialog)
//{{AFX_MSG_MAP(CCalDlg)
ON_EN_KILLFOCUS(IDC_EDIT2, OnKillfocusEdit2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCalDlg message handlers
void CCalDlg::op(int a)
{
m_opera=a;
}
void CCalDlg::OnKillfocusEdit2()
{
switch(m_opera)
{
case 1:
UpdateData(true);
m_n3=m_n1+m_n2;
UpdateData(false);
break;
case 2:
UpdateData(true);
m_n3=m_n1-m_n2;
UpdateData(false);
break;
case 3:
UpdateData(true);
m_n3=m_n1*m_n2;
UpdateData(false);
break;
case 4:
UpdateData(true);
m_n3=m_n1/m_n2;
UpdateData(false);
break;
}
}

Menuview.cpp :
// MenuView.cpp : implementation of the CMenuView class
//
#include "stdafx.h"
#include "Menu.h"
#include "CalDlg.h"
#include "MenuDoc.h"
#include "MenuView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMenuView
IMPLEMENT_DYNCREATE(CMenuView, CView)
BEGIN_MESSAGE_MAP(CMenuView, CView)
//{{AFX_MSG_MAP(CMenuView)
ON_COMMAND(ID_CALCULATE_ADD, OnCalculateAdd)
ON_COMMAND(ID_CALCULATE_DIV, OnCalculateDiv)
ON_COMMAND(ID_CALCULATE_MUL, OnCalculateMul)
ON_COMMAND(ID_CALCULATE_SUB, OnCalculateSub)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMenuView construction/destruction
CMenuView::CMenuView()
{
}
CMenuView::~CMenuView()
{
}
BOOL CMenuView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMenuView drawing
void CMenuView::OnDraw(CDC* pDC)
{
CMenuDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMenuView diagnostics
#ifdef _DEBUG
void CMenuView::AssertValid() const
{
CView::AssertValid();
}
void CMenuView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMenuDoc* CMenuView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMenuDoc)));
return (CMenuDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMenuView message handlers
void CMenuView::OnCalculateAdd()
{
CCalDlg Dlg;
Dlg.op(1);
Dlg.DoModal();
}
void CMenuView::OnCalculateDiv()
{
CCalDlg Dlg;
Dlg.op(2);
Dlg.DoModal();

}
void CMenuView::OnCalculateMul()
{
CCalDlg Dlg;
Dlg.op(3);
Dlg.DoModal();
}
void CMenuView::OnCalculateSub()
{
CCalDlg Dlg;
Dlg.op(4);
Dlg.DoModal();

}

HEADER FILES :

CalcDlg.h :
#if !defined(AFX_CALDLG_H__39DF0A1F_7939_4778_98D8_F3C0F522D54F__INCLUDED_)
#define AFX_CALDLG_H__39DF0A1F_7939_4778_98D8_F3C0F522D54F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CalDlg.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CCalDlg dialog
class CCalDlg : public CDialog
{
// Construction
public:
CCalDlg(CWnd* pParent = NULL); // standard constructor
void op(int a);
int m_opera;
// Dialog Data
//{{AFX_DATA(CCalDlg)
enum { IDD = IDD_DIALOG1 };
float m_n1;
float m_n2;
float m_n3;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCalDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CCalDlg)
afx_msg void OnKillfocusEdit2();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CALDLG_H__39DF0A1F_7939_4778_98D8_F3C0F522D54F__INCLUDED_)

0 comments:

Post a Comment