DOCUMENT/VIEW ARCHITECTURE SERIALIZATION IN A SINGLE DOUCUMENT INTERFACE USING VC++

Friday, May 28, 2010

OBJECTIVE :
To implement document/view architecture and serialization in single document interface.
PROCEDURE:
1. Run MFC Appwizard to generate c:\JKL
2. Select single document interface in step1 of AppWizard.
3. In step 3 deselect the Activex controls.
4. In step 4.deselect the clocking tool bars and printing and print preview.
5. Rest all select the default settings.
6. The wizard will create classes like CSDIDoc, CSDIApp, CSDIView and CMainFrame.
7. Create a generic class mytext which is derived from the base class CObject by right clicking on the SDI classes in the class view and selecting new class.
8. In the new class mytext the constructor and destructor will be created.
9. Create the variables in mytext.h
private:
CString textfacename;
COLORREF textcolor;
CPoint textpoint;
int textsize;
public:
mytext(CPoint point,CString facename,int size,COLORREF color);
void drawtext(CDC *p);
void Serialize(CArchive(&ar));
10. In mytext.cpp type the definition of the constructor with 4 arguments as follows.
mytext::mytext(CPoint point,CString facename,int size,COLORREF color)
{
textpoint=point;
textfacename=facename;
textsize=size;
textcolor=color;
}
11. Again in mytext.cpp type the following definition for the member function drawtext as
void mytext::drawtext(CDC *p)
{
CFont myfont;
myfont.CreatePointFont(textsize,textfacename,p);
p->SelectObject(myfont);
p->SetTextColor(textcolor);
p->SetBkMode(TRANSPARENT);
p->TextOut(textpoint.x,textpoint.y,"hello world");
}
12. Using class wizard on the CSDI view class map the message WM_BUTTONDOWN to the object CSDIView.on the onLButtonDown handler add the following text inSDIView.cpp
void CJKLView::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CString facename[9]={"Arial","Impact","Verdana","Termainal","ComicSans MS"};
COLORREF color=RGB(0,0,225);
CString face=facename[rand()%5];
int size=100;
CFont myfont;
myfont.CreatePointFont(size,face,&dc);
dc.SelectObject(myfont);
dc.SetTextColor(color);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(point.x,point.y,"hello world");
((CJKLDoc*)GetDocument())->addtext(point,face,size,color);
CView::OnLButtonDown(nFlags, point);
}
13. In SDIDoc.h include the following text
#include "mytext.h"
public: // Operations
CObArray textarr;
mytext* addtext(CPoint point,CString facename,int size,COLORREF color)
{
mytext *text;
text=new mytext(point,facename,size,color);
textarr,addtext;
SetModifiedFlag();
return text;
}
14. Edit the OnDraw function of SDIView.cpp as follows
void CJKLView::OnDraw(CDC* pDC)
{
CJKLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int count=pDoc->textarr.GetSize();
if(count)
{
for(int i=0;i<count;i++)
((mytext*)pDoc->textarr[i])->drawtext(pDC);
}
}
15. In SDIDoc.cpp type the text in the function
void CJKLDoc::Serialize(CArchive& ar)
{
textarr.Serialize(ar);
/*if (ar.IsStoring())
{
}
else
{
}*/
}
16. In mytext.CPP type the definition for the serialize function as follows
void mytext::Serialize(CArchive(&ar))
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<textpoint<<textfacename<<textsize<<textcolor;
else
ar>>textpoint>>textfacename>>textsize>>textcolor;
}
17. To make the serialization really work DECLARE_SERIAL in the mytext.h file this
private:
DECLARE_SERIAL(mytext);
18. Declare IMPLEMENT_SERIAL in the mytext.cpp file next to #ifdef & #endif.
IMPLEMENT_SERIAL(mytext,CObject,1);
19. Run and test the application.
PROGRAM:
SOURCE FILES:

Mytext.cpp :
// mytext.cpp: implementation of the mytext class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "JKL.h"
#include "mytext.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL(mytext,CObject,1);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
mytext::mytext()
{
}
mytext::mytext(CPoint point,CString facename,int size,COLORREF color)
{
textpoint=point;
textfacename=facename;
textsize=size;
textcolor=color;
}
mytext::~mytext()
{
}
void mytext::drawtext(CDC *p)
{
CFont myfont;
myfont.CreatePointFont(textsize,textfacename,p);
p->SelectObject(myfont);
p->SetTextColor(textcolor);
p->SetBkMode(TRANSPARENT);
p->TextOut(textpoint.x,textpoint.y,"hello world");
}
void mytext::Serialize(CArchive(&ar))
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<textpoint<<textfacename<<textsize<<textcolor;
else
ar>>textpoint>>textfacename>>textsize>>textcolor;
}

JKLview.cpp :7
// JKLView.cpp : implementation of the CJKLView class
#include "stdafx.h"
#include "JKL.h"
#include "JKLDoc.h"
#include "JKLView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJKLView
IMPLEMENT_DYNCREATE(CJKLView, CView)
BEGIN_MESSAGE_MAP(CJKLView, CView)
//{{AFX_MSG_MAP(CJKLView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJKLView construction/destruction
CJKLView::CJKLView()
{
}
CJKLView::~CJKLView()
{
}
BOOL CJKLView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CJKLView drawing
void CJKLView::OnDraw(CDC* pDC)
{
CJKLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int count=pDoc->textarr.GetSize();
if(count)
{
for(int i=0;i<count;i++)
((mytext*)pDoc->textarr[i])->drawtext(pDC);
}
}
/////////////////////////////////////////////////////////////////////////////
// CJKLView diagnostics
#ifdef _DEBUG
void CJKLView::AssertValid() const
{
CView::AssertValid();
}
void CJKLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CJKLDoc* CJKLView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJKLDoc)));
return (CJKLDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJKLView message handlers
void CJKLView::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CString facename[9]={"Arial","Impact","Verdana","Termainal","ComicSans MS"};
COLORREF color=RGB(0,0,225);
CString face=facename[rand()%5];
int size=100;
CFont myfont;
myfont.CreatePointFont(size,face,&dc);
dc.SelectObject(myfont);
dc.SetTextColor(color);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(point.x,point.y,"hello world");
((CJKLDoc*)GetDocument())->addtext(point,face,size,color);
CView::OnLButtonDown(nFlags, point);
}

JKLdoc.cpp :
// JKLDoc.cpp : implementation of the CJKLDoc class
#include "stdafx.h"
#include "JKL.h"
#include "JKLDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc
IMPLEMENT_DYNCREATE(CJKLDoc, CDocument)
BEGIN_MESSAGE_MAP(CJKLDoc, CDocument)
//{{AFX_MSG_MAP(CJKLDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc construction/destruction
CJKLDoc::CJKLDoc()
{
}
CJKLDoc::~CJKLDoc()
{
}
BOOL CJKLDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc serialization
void CJKLDoc::Serialize(CArchive& ar)
{
textarr.Serialize(ar);
/*if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}*/
}
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc diagnostics
#ifdef _DEBUG
void CJKLDoc::AssertValid() const
{
CDocument::AssertValid();
}

void CJKLDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJKLDoc commands


HEADER FILES:

Mytext.h :
// mytext.h: interface for the mytext class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYTEXT_H__BDBE8C8D_F625_4985_9EAF_860124733BA0__INCLUDED_)
#define AFX_MYTEXT_H__BDBE8C8D_F625_4985_9EAF_860124733BA0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class mytext : public CObject
{
private:
DECLARE_SERIAL(mytext);
CString textfacename;
COLORREF textcolor;
CPoint textpoint;
int textsize;
public:
mytext();
mytext(CPoint point,CString facename,int size,COLORREF color);
void drawtext(CDC *p);
void Serialize(CArchive(&ar));
virtual ~mytext();

};
#endif // !defined(AFX_MYTEXT_H__BDBE8C8D_F625_4985_9EAF_860124733BA0__INCLUDED_)

JKL view.h :
// JKLView.h : interface of the CJKLView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_JKLVIEW_H__4DD10721_ABB2_4BF3_BBB8_919056627B1C__INCLUDED_)
#define AFX_JKLVIEW_H__4DD10721_ABB2_4BF3_BBB8_919056627B1C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CJKLView : public CView
{
protected: // create from serialization only
CJKLView();
DECLARE_DYNCREATE(CJKLView)

// Attributes
public:
CJKLDoc* GetDocument();

// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CJKLView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CJKLView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CJKLView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in JKLView.cpp
inline CJKLDoc* CJKLView::GetDocument()
{ return (CJKLDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_JKLVIEW_H__4DD10721_ABB2_4BF3_BBB8_919056627B1C__INCLUDED_)

JKLdoc.h :
// JKLDoc.h : interface of the CJKLDoc class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_JKLDOC_H__999C7E76_241F_4FA3_9B31_80F7CD48AAD9__INCLUDED_)
#define AFX_JKLDOC_H__999C7E76_241F_4FA3_9B31_80F7CD48AAD9__INCLUDED_
#include "mytext.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CJKLDoc : public CDocument
{
protected: // create from serialization only
CJKLDoc();
DECLARE_DYNCREATE(CJKLDoc)
// Attributes
public: // Operations
CObArray textarr;
mytext* addtext(CPoint point,CString facename,int size,COLORREF color)
{
mytext *text;
text=new mytext(point,facename,size,color);
textarr,addtext;
SetModifiedFlag();
return text;
} // Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CJKLDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CJKLDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CJKLDoc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_JKLDOC_H__999C7E76_241F_4FA3_9B31_80F7CD48AAD9__INCLUDED_)

READ MORE - DOCUMENT/VIEW ARCHITECTURE SERIALIZATION IN A SINGLE DOUCUMENT INTERFACE USING VC++

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

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_)

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

Vc ++ and ODBC (data base connection) | Create database in MS Access link to the database with vc++ through ODBS and perform ADDNEW, MODIFY, DELETE

OBJECTIVE :
To create a database in MS ACCESS and to link the database with VC++ through ODBC and to perform ADDNEW, MODIFY, DELETE records from a table in the database.
PROCEDURE:
PROCEDURE TO CREATE A DATABASE:
1) Start -> programs -> Microsoft office -> Microsoft Access.
2) Select new -> blank database ->type the name of the database as COLLEGE -> click create.
3) Double click on select create table in design view type the following field names .
Name text
Branch text
Age number
( select the data type by clicking on the cell )
4) Close the table with a table name student.
5) Give some sample data in the table student.
PROCEDURE TO CREATE ODBC CONNECTIVITY:
6) Start -> settings -> control panel ->administrative tools -> data sources (ODBC).
7) Double click on Data sources (ODBC) -> user DSN -> add.
8) Double click drive to Microsoft Access (mdb) -> ODBC Microsoft Access setup dialog will appear.
9) Type the data source name -> DNS COLLEGE.
10) Click select … select the college mdb (database) into its path and press ok.
11) Press ok and close the ODBC dialog now the ODBC connection has been established.
PROCEDURE TO CREATE THE PROGRAM:
12) Run the MFC AppWizard (exe) to generate c:\odbc.
13) In the step1 select single document interface.
14) In step 2 select database view without file support.
15) Select data source -> database options window will appear.
16) Select ODBC ->DNSCOLLEGE , record set type -> DYNASET ->ok.
17) Select student table.
18) Deselect printing and print preview, press FINISH.
19) Select -> resource view -> IDR_MAINFRAME
20) Add three submenus in record add new(ID_RECORD_ADDNEW), modify record(ID_RECORD_MODIFY), delete record (ID_RECORD_DELETE),
21) Select -> dialog -> IDD_ODBC_FORM.
22) Insert 3 edit controls on the dialogand a command ( with caption clear field )
23) Using class wizard map the command message of the CODBC view class with all the submenus ID_RECORD_ADDNEW, ID_RECORD_MODIFY, ID_RECORD_DELETE, map the BN_CLICKED with IDC_BUTTON1 and map On Move with CODBC View.
24) Using class wizard create member variable for IDC_EDIT1, IDC_EDIT2, IDC_EDIT3 by selecting the variable names from the popup m_pset->m_name, m_pset->m_branch, m_pset->m_age, with suitable data type CString, CString, long.
25) Now the data form the table will be linked with edit controls of the dialog.
26) Edit the CODBCView.cpp with the following information in OnMove, On Record Delete, On Record Modify, On Button1 functions.
void COdbc2View::OnRecordAddnew()
{
// TODO: Add your command handler code here
m_pSet->AddNew();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
if(!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
m_pSet->Requery();
UpdateData(false);
}
void COdbc2View::OnRecordDelete()
{
// TODO: Add your command handler code here
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException *c)
{
AfxMessageBox(c->m_strError);
c->Delete();
m_pSet->MoveFirst();
UpdateData(false);
return;
}
m_pSet->GetStatus(status);
if(status.m_lCurrentRecord==0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(false);
}
void COdbc2View::OnRecordModify()
{
// TODO: Add your command handler code here
m_pSet->Edit();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void COdbc2View::OnButton1()
{
// TODO: Add your control notification handler code here
m_pSet->SetFieldNull(NULL);
UpdateData(false);
}
BOOL COdbc2View::OnMove(UINT nIDMoveCommand)
{
// TODO: Add your specialized code here and/or call the base class
switch(nIDMoveCommand)
{
case ID_RECORD_PREV:
m_pSet->MovePrev();
if(!m_pSet->IsBOF())
break;
case ID_RECORD_FIRST:
m_pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
m_pSet->MoveNext();
if(!m_pSet->IsEOF())
break;
if(!m_pSet->CanScroll())
{
m_pSet->SetFieldNull(NULL);
break;
}
break;
case ID_RECORD_LAST:
m_pSet->MoveLast();
break;
default:
ASSERT(false);
}
UpdateData(false);
return true;
return CRecordView::OnMove(nIDMoveCommand);
}
27) Compile, run and test the applications.
PROGRAM :
SOURCE FILES :

Odbcview.cpp :
// odbc2View.cpp : implementation of the COdbc2View class
//
#include "stdafx.h"
#include "odbc2.h"
#include "odbc2Set.h"
#include "odbc2Doc.h"
#include "odbc2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COdbc2View
IMPLEMENT_DYNCREATE(COdbc2View, CRecordView)
BEGIN_MESSAGE_MAP(COdbc2View, CRecordView)
//{{AFX_MSG_MAP(COdbc2View)
ON_COMMAND(ID_RECORD_ADDNEW, OnRecordAddnew)
ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
ON_COMMAND(ID_RECORD_MODIFY, OnRecordModify)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COdbc2View construction/destruction
COdbc2View::COdbc2View()
: CRecordView(COdbc2View::IDD)
{
//{{AFX_DATA_INIT(COdbc2View)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
COdbc2View::~COdbc2View()
{
}
void COdbc2View::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COdbc2View)
DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_name, m_pSet);
DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_age, m_pSet);
DDX_FieldText(pDX, IDC_EDIT3, m_pSet->m_branch, m_pSet);
//}}AFX_DATA_MAP
}
BOOL COdbc2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void COdbc2View::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_odbc2Set;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// COdbc2View diagnostics
#ifdef _DEBUG
void COdbc2View::AssertValid() const
{
CRecordView::AssertValid();
}
void COdbc2View::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
COdbc2Doc* COdbc2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COdbc2Doc)));
return (COdbc2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COdbc2View database support
CRecordset* COdbc2View::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// COdbc2View message handlers
void COdbc2View::OnRecordAddnew()
{
// TODO: Add your command handler code here
m_pSet->AddNew();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
if(!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
m_pSet->Requery();
UpdateData(false);
}
void COdbc2View::OnRecordDelete()
{
// TODO: Add your command handler code here
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException *c)
{
AfxMessageBox(c->m_strError);
c->Delete();
m_pSet->MoveFirst();
UpdateData(false);
return;
}
m_pSet->GetStatus(status);
if(status.m_lCurrentRecord==0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(false);
}
void COdbc2View::OnRecordModify()
{
// TODO: Add your command handler code here
m_pSet->Edit();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void COdbc2View::OnButton1()
{
// TODO: Add your control notification handler code here
m_pSet->SetFieldNull(NULL);
UpdateData(false);
}
BOOL COdbc2View::OnMove(UINT nIDMoveCommand)
{
// TODO: Add your specialized code here and/or call the base class
switch(nIDMoveCommand)
{
case ID_RECORD_PREV:
m_pSet->MovePrev();
if(!m_pSet->IsBOF())
break;
case ID_RECORD_FIRST:
m_pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
m_pSet->MoveNext();
if(!m_pSet->IsEOF())
break;
if(!m_pSet->CanScroll())
{
m_pSet->SetFieldNull(NULL);
break;
}
break;
case ID_RECORD_LAST:
m_pSet->MoveLast();
break;
default:
ASSERT(false);
}
UpdateData(false);
return true;
return CRecordView::OnMove(nIDMoveCommand);
}

Odbcdoc.cpp :
// odbc2Doc.cpp : implementation of the COdbc2Doc class
//
#include "stdafx.h"
#include "odbc2.h"
#include "odbc2Set.h"
#include "odbc2Doc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc
IMPLEMENT_DYNCREATE(COdbc2Doc, CDocument)
BEGIN_MESSAGE_MAP(COdbc2Doc, CDocument)
//{{AFX_MSG_MAP(COdbc2Doc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc construction/destruction
COdbc2Doc::COdbc2Doc()
{
// TODO: add one-time construction code here
}
COdbc2Doc::~COdbc2Doc()
{
}
BOOL COdbc2Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc diagnostics
#ifdef _DEBUG
void COdbc2Doc::AssertValid() const
{
CDocument::AssertValid();
}
void COdbc2Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc commands

HEADER FILES :

Odbcview.h :
// odbc2View.h : interface of the COdbc2View class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_ODBC2VIEW_H__C865C844_0839_466B_B4AA_484940B18BE2__INCLUDED_)
#define AFX_ODBC2VIEW_H__C865C844_0839_466B_B4AA_484940B18BE2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class COdbc2Set;
class COdbc2View : public CRecordView
{
protected: // create from serialization only
COdbc2View();
DECLARE_DYNCREATE(COdbc2View)
public:
//{{AFX_DATA(COdbc2View)
enum { IDD = IDD_ODBC2_FORM };
COdbc2Set* m_pSet;
//}}AFX_DATA
// Attributes
public:
COdbc2Doc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COdbc2View)
public:
virtual CRecordset* OnGetRecordset();
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual BOOL OnMove(UINT nIDMoveCommand);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate(); // called first time after construct
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~COdbc2View();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(COdbc2View)
afx_msg void OnRecordAddnew();
afx_msg void OnRecordDelete();
afx_msg void OnRecordModify();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in odbc2View.cpp
inline COdbc2Doc* COdbc2View::GetDocument()
{ return (COdbc2Doc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ODBC2VIEW_H__C865C844_0839_466B_B4AA_484940B18BE2__INCLUDED_)

Odbdoc.h :
// odbc2Doc.h : interface of the COdbc2Doc class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_ODBC2DOC_H__6FE5F421_C65C_44B3_818A_CD4C12718C8E__INCLUDED_)
#define AFX_ODBC2DOC_H__6FE5F421_C65C_44B3_818A_CD4C12718C8E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "odbc2Set.h"
class COdbc2Doc : public CDocument
{
protected: // create from serialization only
COdbc2Doc();
DECLARE_DYNCREATE(COdbc2Doc)
// Attributes
public:
COdbc2Set m_odbc2Set;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COdbc2Doc)
public:
virtual BOOL OnNewDocument();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~COdbc2Doc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(COdbc2Doc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ODBC2DOC_H__6FE5F421_C65C_44B3_818A_CD4C12718C8E__INCLUDED_)

READ MORE - Vc ++ and ODBC (data base connection) | Create database in MS Access link to the database with vc++ through ODBS and perform ADDNEW, MODIFY, DELETE

ACTIVEX CONTROL |Create Activex control dialog container using a calendar control using Vc++

OBJECTIVE :
To create an Activex control dialog container using a calendar control.
PROCEDURE:
1. Run MFC AppWizard(exe) to generate the application C:\Active.
2. Select single document in step1 of AppWizard and click next.
3. Check whether Activex control is select or not in step4 if not select the Activex control and click finish.
4. Select the project menu click add to project and then select components and control.
Project->Add to project->Components and control.
5. Choose registered Activex control in the gallery.
6. Choose calendar control 8.0 or 10.0
7. Using the dialog editor create a new dialog resource.
8. Drag the calendar control from the control palette & place the dialog box.
9. Place three edit control in the dialog.
10. And then drag two command button for Date & the another button for “Next Week”.
11. Choose claender control ID ad IDC_Calender select the date button ID as IDC_SELECTDATE. Next week button ID as IDC_NEXTWEEK.
12. Edit control day ID as IDC_DAY, Month ID as IDC_MONTH, Year ID as IDC_YEAR.
13. Change the dialog box ID as IDD_ACTIVEXDIALOG.
14. Right click on the dialog and select class wizard. Choose create new class and give the mane as CActivexDialog.
15. Give the member variable and select type of the IDControl
Contol ID Type Member variable
IDC_CALENDER CCalender m_calender
IDC_DAY Short m_sday
IDC_YEAR Short m_syear
IDC_MONTH Short m_smonth
16. In the ActivexDialog.h header file add two variables m_varValue and m_BackColor class CActiveXDialog : public CDialog
class CActiveXDialog1 : public CDialog
{
// Construction
public:
CActiveXDialog1(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CActiveXDialog1)
enum { IDD = IDD_DIALOG1 };
short m_sDay;
short m_sMonth;
short m_sYear;
CCalendar m_Calendar;
//}}AFX_DATA
COleVariant m_varValue;
unsigned long m_BackColor;
17. WM_InitDialog message was select and ID CActiveXDialog a notification handler is creaed with the name OnInitDialog(Virtual function)
BOOL CActiveXDialog1::OnInitDialog()
{
CDialog::OnInitDialog();
m_Calendar.SetValue(m_varValue);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
18. New month message select in Id IDC_CALENDER with the name on new month calendar.
void CActiveXDialog1::OnNewMonthCalendar1()
{
AfxMessageBox("EVENT:CActiveXDialog1::OnNewMonthCalendar1");
// TODO: Add your control notification handler code here
}
19. In ActivexDialog.cpp right click on mouse button. Select the class wizard Id as IDC_SELECTDATE, IDC_NEXTWEEK,ID OK Select. Message is BN_CLICKED notification handler are created with the name OnSelectdate, OnNextweek, OnOK (Virtual Funtion)
void CActiveXDialog1::OnSelectdate()
{
CDataExchange dx(this,true);
DDX_Text(&dx, IDC_EDIT1, m_sDay);
DDX_Text(&dx, IDC_EDIT2, m_sMonth);
DDX_Text(&dx, IDC_EDIT3, m_sYear);
m_Calendar.SetDay(m_sDay);
m_Calendar.SetMonth(m_sMonth);
m_Calendar.SetYear(m_sYear);
}
void CActiveXDialog1::OnNextweek()
{
m_Calendar.NextWeek(); // TODO: Add your control notification handler code here
}
BOOL CActiveXDialog1::OnInitDialog()
{
CDialog::OnInitDialog();
m_Calendar.SetValue(m_varValue);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CActiveXDialog1::OnOk()
{
CDialog::OnOK();
m_varValue=m_Calendar.GetValue();
// TODO: Add your control notification handler code here
}
20. Connect the dialog to the view. Use the class wizard to map the WM_LBUTTONDOWN message and then edit the handler functions as follows
void CActiveView::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog1 dlg;
dlg.m_BackColor=RGB(255,250,240);
COleDateTime today=COleDateTime::GetCurrentTime();
dlg.m_varValue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0,0);
if(dlg.DoModal()==IDOK)
{
COleDateTime date(dlg.m_varValue);
AfxMessageBox(date.Format("%B %d %y"));
CView::OnLButtonDown(nFlags, point);
}
21. Edit the virtual OnDraw function in the fileActivex.cpp
void CActiveView::OnDraw(CDC* pDC)
{
CActiveDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(0,0,"Press the left mouse button here");
}
22. Compile and run the application.
PROGRAM:
SOURCE FILES:
ActiveXDialog1.cpp :
// ActiveXDialog1.cpp : implementation file

#include "stdafx.h"
#include "active.h"
#include "ActiveXDialog1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CActiveXDialog1 dialog
CActiveXDialog1::CActiveXDialog1(CWnd* pParent /*=NULL*/)
: CDialog(CActiveXDialog1::IDD, pParent)
{
//{{AFX_DATA_INIT(CActiveXDialog1)
m_sDay = 0;
m_sMonth = 0;
m_sYear = 0;
//}}AFX_DATA_INIT
}
void CActiveXDialog1::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CActiveXDialog1)
DDX_Text(pDX, IDC_EDIT1, m_sDay);
DDX_Text(pDX, IDC_EDIT2, m_sMonth);
DDX_Text(pDX, IDC_EDIT3, m_sYear);
DDX_Control(pDX, IDC_CALENDAR1, m_Calendar);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CActiveXDialog1, CDialog)
//{{AFX_MSG_MAP(CActiveXDialog1)
ON_BN_CLICKED(IDC_SELECTDATE, OnSelectdate)
ON_BN_CLICKED(IDC_NEXTWEEK, OnNextweek)
ON_BN_CLICKED(IDC_OK, OnOk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CActiveXDialog1 message handlers
void CActiveXDialog1::OnSelectdate()
{
// TODO: Add your control notification handler code here
CDataExchange dx(this,true);
DDX_Text(&dx, IDC_EDIT1, m_sDay);
DDX_Text(&dx, IDC_EDIT2, m_sMonth);
DDX_Text(&dx, IDC_EDIT3, m_sYear);
m_Calendar.SetDay(m_sDay);
m_Calendar.SetMonth(m_sMonth);
m_Calendar.SetYear(m_sYear);
}

void CActiveXDialog1::OnNextweek()
{
m_Calendar.NextWeek(); // TODO: Add your control notification handler code here
}
BOOL CActiveXDialog1::OnInitDialog()
{
CDialog::OnInitDialog();
m_Calendar.SetValue(m_varValue);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_EVENTSINK_MAP(CActiveXDialog1, CDialog)
//{{AFX_EVENTSINK_MAP(CActiveXDialog1)
ON_EVENT(CActiveXDialog1, IDC_CALENDAR1, 3 /* NewMonth */, OnNewMonthCalendar1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CActiveXDialog1::OnNewMonthCalendar1()
{
AfxMessageBox("EVENT:CActiveXDialog1::OnNewMonthCalendar1");
// TODO: Add your control notification handler code here

}

void CActiveXDialog1::OnOk()
{
CDialog::OnOK();
m_varValue=m_Calendar.GetValue();
// TODO: Add your control notification handler code here

}
activeView.cpp :
// activeView.cpp : implementation of the CActiveView class
//
#include "stdafx.h"
#include "active.h"
#include"ActiveXDialog1.h"
#include "activeDoc.h"
#include "activeView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CActiveView

IMPLEMENT_DYNCREATE(CActiveView, CView)

BEGIN_MESSAGE_MAP(CActiveView, CView)
//{{AFX_MSG_MAP(CActiveView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CActiveView construction/destruction

CActiveView::CActiveView()
{
// TODO: add construction code here
}
CActiveView::~CActiveView()
{
}
BOOL CActiveView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CActiveView drawing

void CActiveView::OnDraw(CDC* pDC)
{
CActiveDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(0,0,"Press the left mouse button here");
}

/////////////////////////////////////////////////////////////////////////////
// CActiveView printing

BOOL CActiveView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CActiveView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CActiveView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CActiveView diagnostics

#ifdef _DEBUG
void CActiveView::AssertValid() const
{
CView::AssertValid();
}

void CActiveView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CActiveDoc* CActiveView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CActiveDoc)));
return (CActiveDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CActiveView message handlers
void CActiveView::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog1 dlg;
dlg.m_BackColor=RGB(255,250,240);
COleDateTime today=COleDateTime::GetCurrentTime();
dlg.m_varValue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0,0);
if(dlg.DoModal()==IDOK)
{
COleDateTime date(dlg.m_varValue);
AfxMessageBox(date.Format("%B %d %y"));
}
CView::OnLButtonDown(nFlags, point);

}

HEADER FILES:

ActiveXDialog1.h :
//{{AFX_INCLUDES()
#include "calendar.h"
//}}AFX_INCLUDES
#if !defined(AFX_ACTIVEXDIALOG1_H__2881107D_C9D5_4022_B251_F894CF033B04__INCLUDED_)
#define AFX_ACTIVEXDIALOG1_H__2881107D_C9D5_4022_B251_F894CF033B04__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ActiveXDialog1.h : header file
/////////////////////////////////////////////////////////////////////////////
// CActiveXDialog1 dialog
class CActiveXDialog1 : public CDialog
{
// Construction
public:
CActiveXDialog1(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CActiveXDialog1)
enum { IDD = IDD_DIALOG1 };
short m_sDay;
short m_sMonth;
short m_sYear;
CCalendar m_Calendar;
//}}AFX_DATA

COleVariant m_varValue;
unsigned long m_BackColor;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CActiveXDialog1)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CActiveXDialog1)
afx_msg void OnSelectdate();
afx_msg void OnNextweek();
afx_msg void OnButton1();
virtual BOOL OnInitDialog();
afx_msg void OnNewMonthCalendar1();
afx_msg void OnOk();
DECLARE_EVENTSINK_MAP()
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ACTIVEXDIALOG1_H__2881107D_C9D5_4022_B251_F894CF033B04__INCLUDED_)

READ MORE - ACTIVEX CONTROL |Create Activex control dialog container using a calendar control using Vc++

KEYBOARD AND MOUSE EVENTS | Create SDI PROGRAM CREATING WINODOW AND WORK on keyboard and mouse events using VC++.

OBJECTIVE:
To write a SDI program for creating a window and work on keyboard and mouse events.
PROCEDURE:
A square should be drawn on the window during creation. During every mouse click a square should be displayed every time horizontal line.
1. Click->start->program->MS visual 6.0->MS visual c++
2. Create a new workspace by clicking File->new->win32 application
3. Then give the name of the file selecting file->new->c++ source program.
4. Include the header file windows.h
5. Declare the window procedure
6. Create a object for window class
7. In win main function create the object to handle window for message
8. Create a object foR handle window or message
9. Define the style background of the window with class and assign the class name and lpsz wndporc as “wndproc” while is declared as already and is defined by step 14-19
10. Then register the class name using window class object
11. Create the window using show window create window function and display it using show window function
12. Construct the message loop to get the message
13. Then return the value for main function
14. Define the window procedure with parameter of HWND to handle the window UINT to get the message of window WPARAM and LPARAM
15. Create the object for context text, rectangle and paint struct
16. In WM_PAINT which is used to get the message while pressing the left button of mouse
17. WM_LBUTTONDOWN is used to get the message while pressing the left button of mouse.
18. WM_KEYDOWN is used to get the message while any key is pressed and keyboard in which created nested is 0X09 code for table key they displays the message.
19. Then return the default values program and build it.
20. Then execute the program.
PROGRAM:

#include "stdafx.h"
#include "dialog.h"
#include "dialogDlg.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,PSTR SzCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("ex 10");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbWndExtra = 0;
wndclass.cbClsExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("this program requires windows NT"),szAppName,MB_ICONERROR);
}
hWnd=CreateWindow(szAppName,TEXT("MOUSE&KEYBOARDEVENTS"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static int x1=0,y1=0,x2=0,y2=0;
RECT rect;
HBRUSH hBrush;
PAINTSTRUCT ps;
{
switch(msg)
{
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
DrawText(hdc,TEXT("press left mouse button or esc key or tab key"),1,&rect,DT_SINGLELINEDT_CENTERDT_TOP);
x1=y1=x2=y2;
SetRect(&rect,100+x1,100+y1,200+x2,200+y2);
x1+=50;y1+=50;
x2+=50;y2+=50;
hBrush=CreateHatchBrush(HS_BDIAGONAL,RGB(255,0,0));
FillRect(hdc,&rect,hBrush);
EndPaint(hWnd,&ps);
return 0;
break;
case WM_LBUTTONDOWN:
hdc=GetDC(hWnd);
GetClientRect(hWnd,&rect);
SetRect(&rect,100+x1,100+y1,200+x2,200+y2);
x1+=50;y1+=50;
x2+=50;y2+=50;
hBrush=CreateHatchBrush(HS_BDIAGONAL,RGB(255,0,0));
FillRect(hdc,&rect,hBrush);
ReleaseDC(hWnd,hdc);
return 0;
break;
case WM_CHAR:
hdc=GetDC(hWnd);
switch(wParam)
{
case 0x09:
SetTextColor(hdc,RGB(0,255,0));
TextOut(hdc,0,10,"tabkey pressed",15);
return 0;
break;
case 0x1B:
SetTextColor(hdc,RGB(255,0,0));
TextOut(hdc,0,10,"esckey pressed",15);
return 0;
break;
ReleaseDC(hWnd,hdc);
}
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
}

READ MORE - KEYBOARD AND MOUSE EVENTS | Create SDI PROGRAM CREATING WINODOW AND WORK on keyboard and mouse events using VC++.

DIALOG BASED APPLICATION | CREATE DIALOG BASED APPLICATION WITH VARIOUS CONTROL USING VC++

OBJECTIVE:
To create a dialog based application with various control using VC++.
PROCEDURE:
1. Run the MFC AppWizard(exe) to generate dialog
2. In step1 dialog based application select the default settings and press OK button.
3. In resource view click on the dialog and select the IDD_DIALOG1
4. Design the dialog using the dialog editor
5. Use edit controls to get the name, age combo boxes for date for birth, edit box to read the parents name, monthly income, list box, to list the education
6. Right click on the monthly income select class wizard->member variables
7. In edit4 (monthly income) select the variable namaes m_sal and select the data type as int and click OK.
8. In date of birth using combo boxes for day ,month, year
9. Right click on the list box select properties and give the ID name as IDC_EDU
10. Radio button yes, no to check the transfer certificate availability
11. Radio button yes, no to check the birth certificate availability
12. One command button for selection and another to quit the project
13. In dialogdlg.h declare 2 variables
Public:
CString SelString1;
CListBox *m_fath_edu;
14. In the Dialog Dlg.cpp add the following code OnInit Dialog function
CListBox *PLB=(CListBox *)GetDlgItem(IDC_EDU);
PLB->InsertString(-1,"Graduate");
PLB->InsertString(-1,"Post Graduate");
PLB->InsertString(-1,"High School");
PLB->InsertString(-1,"Professional");
PLB->InsertString(-1,"Illiterate");
15. Using the class wizard map the object Id as IDC_EDU with the message LBN_selchange notification handler is created with this OnSelChangeEdu()
void CDialogDlg::OnSelchangeEdu()
{
// TODO: Add your control notification handler code here
int SelIndex1;
m_fath_edu=(CListBox*)GetDlgItem(IDC_EDU);
SelIndex1=m_fath_edu->GetCurSel();
m_fath_edu->GetText(SelIndex1,SelString1);
}
16. Using the class wizard map the object Id as IDC_BUTTON1 with the message BN_CLICKED a notification handler is created with this OnButton1()
void CDialogDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if((m_sal*12>10000)&&(SelString1!="Illiterate"))
MessageBox("Student Selected");
else
MessageBox("Student not selected");
}
17. Using the class wizard map the object Id as IDC_BUTTON1 with the message BN_ CLICKED a notification handleris created with the OnButton2()
void CDialogDlg::OnButton2()
{
PostQuitMessage(0);
}
18. Run and test the application
PROGRAM:
SOURCE FILES:
Dialogdlg.cpp:

 

#include "stdafx.h"
#include "dialog.h"
#include "dialogDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CDialogDlg dialog
CDialogDlg::CDialogDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDialogDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDialogDlg)
// 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 CDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDialogDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDialogDlg, CDialog)
//{{AFX_MSG_MAP(CDialogDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_LBN_SELCHANGE(IDC_EDU, OnSelchangeEdu)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDialogDlg message handlers
BOOL CDialogDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CListBox *PLB=(CListBox *)GetDlgItem(IDC_EDU);
PLB->InsertString(-1,"Graduate");
PLB->InsertString(-1,"Post Graduate");
PLB->InsertString(-1,"High School");
PLB->InsertString(-1,"Professional");
PLB->InsertString(-1,"Illiterate");
// 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 < psysmenu =" GetSystemMenu(FALSE);">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 CDialogDlg::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 CDialogDlg::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 CDialogDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDialogDlg::OnSelchangeEdu()
{
// TODO: Add your control notification handler code here
int SelIndex1;
m_fath_edu=(CListBox*)GetDlgItem(IDC_EDU);
SelIndex1=m_fath_edu->GetCurSel();
m_fath_edu->GetText(SelIndex1,SelString1);
}
void CDialogDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if((m_sal*12>10000)&&(SelString1!="Illiterate"))
MessageBox("Student Selected");
else
MessageBox("Student not selected");

}
void CDialogDlg::OnButton2()
{
// TODO: Add your control notification handler code here
PostQuitMessage(0);
}
Header Files:
DialogDlg.h:
#if !defined(AFX_DIALOGDLG_H__013D32FD_7256_4F10_88B6_12F86426ED57__INCLUDED_)
#define AFX_DIALOGDLG_H__013D32FD_7256_4F10_88B6_12F86426ED57__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CDialogDlg dialog
class CDialogDlg : public CDialog
{
// Construction
public:
CDialogDlg(CWnd* pParent = NULL); // standard constructor
CString SelString1;
CListBox *m_fath_edu;
// Dialog Data
//{{AFX_DATA(CDialogDlg)
enum { IDD = IDD_DIALOG_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDialogDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
protected:
HICON m_hIcon;
// Generated message map functions //{{AFX_MSG(CDialogDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnSelchangeEdu();
afx_msg void OnButton1();
afx_msg void OnButton2();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DIALOGDLG_H__013D32FD_7256_4F10_88B6_12F86426ED57__INCLUDED_)


READ MORE - DIALOG BASED APPLICATION | CREATE DIALOG BASED APPLICATION WITH VARIOUS CONTROL USING VC++

THREAD | TO CREATE AND IMPLEMENT THE USE OF THEREAD IN VC++

OBJECTIVE:
To implement the use of threads in vc++.
PROCEDURE:
1. Run the MFC appwizard(dll) to generate the application c:\thread
2. Select the ingle document in step 1 of appwizard
3. Select all the default settings except printing and print preview
4. Using the resource view, right click on the dialog and select the new dialog
5. Create new dialog box named IDC_PROGRESS1
6. Name the new dialog on IDD_COMPUTE using properties
7. The default OK button can be changed to IDC_START
8. Leave the default cancel button as such
9. By clicking on the dialog IDD_COMPUTE the system will automatically ask for a class name ive the classname as CComputeDlg
10. Using the class wizard map the message BN_CLICKED to the IDC_START, IDC_CANCEL objects
11. Declare the following in the compute DLG.H file next to #ENDIF
#define WM_THREADFINISHED
class CComputeDlg : public CDialog
{
LRESULT OnThreadFinished(WPARAM wparam,LPARAM lparam);
private:
int m_nTimer;
public:
enum{nMaxCount=10000};
12. Using class wizard map the message WM_TIMER to the CComputeDlg.cpp files as follows
void CComputeDlg::OnTimer(UINT nIDEvent)
{
CProgressCtrl*pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
pBar->SetPos(g_nCount*100/nMaxCount);
CDialog::OnTimer(nIDEvent);
}
13. Type the following in compute Dlg.cpp type the definition of the thread before the constructor next to #endif
int g_nCount=0;
UINT ComputeThreadProc(LPVOID pParam)
{
volatile int nTemp;
for(g_nCount=0;g_nCountEnableWindow(FALSE);
AfxBeginThread(ComputeThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}
void CComputeDlg::OnCancel()
{
if(g_nCount==0)
{
CDialog::OnCancel();
}
else
{
g_nCount=nMaxCount;
}
}
15. Type the OnThreadFinished definition in the end of ComputeDlg.cpp file
LRESULT CComputeDlg::OnThreadFinished(WPARAM wparam,LPARAM lparam)
{
CDialog::OnOK();
return 0;
}
16. Using the class wizard map the CThreadView class an edit the following
void CThreadView::OnLButtonDown(UINT nFlags, CPoint point)
{
dlg.DoModal();
CView::OnLButtonDown(nFlags, point);
}
17. And in OnDraw function of CThreadView.cpp type the following
pDC->TextOut(0,0,"press the left mouse button here");
18. Include the header file of ComputeDlg in CThreadView.cpp
#include "ComputeDlg.h"
19. Compile and run and test the application
PROGRAM:
SOURCE FILES:
Computedlg.cpp :

 

#include "stdafx.h"
#include "thread.h"
#include "ComputeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComputeDlg dialog
int g_nCount=0;
UINT ComputeThreadProc(LPVOID pParam)
{
volatile int nTemp;
for(g_nCount=0;g_nCountEnableWindow(FALSE);
AfxBeginThread(ComputeThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}
void CComputeDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(g_nCount==0)
{
CDialog::OnCancel();
}
else
{
g_nCount=nMaxCount;
}
}
void CComputeDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CProgressCtrl*pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
pBar->SetPos(g_nCount*100/nMaxCount);
CDialog::OnTimer(nIDEvent);
}
LRESULT CComputeDlg::OnThreadFinished(WPARAM wparam,LPARAM lparam)
{
CDialog::OnOK();
return 0;
}
Threadview.cpp :
// threadView.cpp : implementation of the CThreadView class
#include "stdafx.h"
#include "thread.h"
#include "threadDoc.h"
#include "threadView.h"
#include "ComputeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CThreadView
IMPLEMENT_DYNCREATE(CThreadView, CView)

BEGIN_MESSAGE_MAP(CThreadView, CView)
//{{AFX_MSG_MAP(CThreadView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CThreadView construction/destruction

CThreadView::CThreadView()
{

}
CThreadView::~CThreadView()
{
}
BOOL CThreadView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CThreadView drawing

void CThreadView::OnDraw(CDC* pDC)
{
CThreadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->TextOut(0,0,"press the left mouse button here");
}
/////////////////////////////////////////////////////////////////////////////
// CThreadView diagnostics
#ifdef _DEBUG
void CThreadView::AssertValid() const
{
CView::AssertValid();
}

void CThreadView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CThreadDoc* CThreadView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CThreadDoc)));
return (CThreadDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CThreadView message handlers
void CThreadView::OnLButtonDown(UINT nFlags, CPoint point)
{
dlg.DoModal();
CView::OnLButtonDown(nFlags, point);
}
HEADER FILES:

Computedlg.h :
#if !defined(AFX_COMPUTEDLG_H__62A874DD_70BF_4129_BE3F_1BD11DCF341D__INCLUDED_)
#define AFX_COMPUTEDLG_H__62A874DD_70BF_4129_BE3F_1BD11DCF341D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ComputeDlg.h : header file
//
#define WM_THREADFINISHED WM_USER +5
UINT ComputeThreadProc(LPVOID pParam);
/////////////////////////////////////////////////////////////////////////////
// CComputeDlg dialog
class CComputeDlg : public CDialog
{
// Construction
LRESULT OnThreadFinished(WPARAM wparam,LPARAM lparam);
private:
int m_nTimer;
public:
enum{nMaxCount=10000};
CComputeDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CComputeDlg)
enum { IDD = IDD_COMPUTE };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComputeDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
protected:

// Generated message map functions
//{{AFX_MSG(CComputeDlg)
afx_msg void OnStart();
virtual void OnCancel();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_COMPUTEDLG_H__62A874DD_70BF_4129_BE3F_1BD11DCF341D__INCLUDED_)

READ MORE - THREAD | TO CREATE AND IMPLEMENT THE USE OF THEREAD IN VC++