Procedure:
1. Run AppWizard(exe)—select SDI
2. Insert Resource Dialog IDD_DIALOG1
3. Modify the ThreadDialog and ThreadView files
4. Build and test the application.
ThreadDialog.h
#define WM_THREADFINISHED WM_USER+5
public:
int m_nTimer;
enum {nMaxCount=10000};
protected:
afx_msg LRESULT OnThreadFinished(WPARAM wParam,LPARAM lParam);
ThreadDialog.cpp
int g_nCount=0;
UINT ComputeThreadProc(LPVOID pParam)
{
volatile int nTemp;
for (g_nCount=0;g_nCount< CThreadDialog::nMaxCount;::InterlockedIncrement((long*)&g_nCount))
{
for (nTemp=0;nTemp<100000;nTemp++)
{
}
::PostMessage((HWND)pParam,WM_TIMER,0,0);
}
::PostMessage((HWND)pParam,WM_THREADFINISHED,0,0);
g_nCount=0;
return 0;
}
BEGIN_MESSAGE_MAP(CThreadDialog, CDialog)
ON_MESSAGE(WM_THREADFINISHED,OnThreadFinished)
END_MESSAGE_MAP()
void CThreadDialog::OnStart()
{ m_nTimer=SetTimer(1,10000,NULL);
ASSERT(m_nTimer != 0);
GetDlgItem(ID_START)->EnableWindow(FALSE);
AfxBeginThread(ComputeThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}
void CThreadDialog::OnCancel()
{
if (g_nCount ==0)
CDialog::OnCancel();
else
g_nCount=nMaxCount;
}
LRESULT CThreadDialog::OnThreadFinished(WPARAM wParam,LPARAM lParam)
{
CDialog::OnOK();
return 0;
}
void CThreadDialog::OnTimer(UINT nIDEvent)
{
CProgressCtrl* pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
pBar->SetPos(g_nCount*100/nMaxCount);
}
ThreadView.cpp
#include “ThreadDialog.h”
void CThreadView::OnLButtonDown(UINT nflags, CPOINT point)
{
CThreadDialog dlg;
dlg.DoModal();
}
Monday, April 19, 2010
ODBC
Procedure:
1. Run MFCAppWizard(exe)
Step 1: Choose single document
Step 2: Select Database View without file support option also click Data
source button and choose ODBC data source name(student registration)
and select Dynaset as the record type, then select the table(student)
2. Add controls to the form template.
3. Using class wizardMember variable tabAdd variable buttonchoose the ID corresponding to the edit box for each variable.
4. Add the following items to the IDR_MAINFRAME menu
- Add record, Clear fields, Delete record, Update record
5. Use class wizard to map the menu command id’s with the corresponding
command handlers.
ID_RECORD_ADD OnRecordAdd
ID_RECORD_CLEARFIELDS OnRecordClearfields
ID_RECORD_DELETE OnRecordDelete
ID_RECORD_UPADTE OnRecordUpdate
6. Edit the menu command handlers under View.cpp file
7. Build and test the application
SOURCE CODE:
void CEx31bView::OnRecordAdd()
{
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 CEx31bView::OnRecordClearFields()
{
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
}
void CEx31bView::OnRecordDelete()
{
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
e->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 CEx31bView::OnUpdateRecordDelete(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSet->IsEOF());
}
void CEx31bView::OnRecordUpdate()
{
m_pSet->Edit();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void CEx31bView::OnUpdateRecordUpdate(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSet->IsEOF());
}
1. Run MFCAppWizard(exe)
Step 1: Choose single document
Step 2: Select Database View without file support option also click Data
source button and choose ODBC data source name(student registration)
and select Dynaset as the record type, then select the table(student)
2. Add controls to the form template.
3. Using class wizardMember variable tabAdd variable buttonchoose the ID corresponding to the edit box for each variable.
4. Add the following items to the IDR_MAINFRAME menu
- Add record, Clear fields, Delete record, Update record
5. Use class wizard to map the menu command id’s with the corresponding
command handlers.
ID_RECORD_ADD OnRecordAdd
ID_RECORD_CLEARFIELDS OnRecordClearfields
ID_RECORD_DELETE OnRecordDelete
ID_RECORD_UPADTE OnRecordUpdate
6. Edit the menu command handlers under View.cpp file
7. Build and test the application
SOURCE CODE:
void CEx31bView::OnRecordAdd()
{
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 CEx31bView::OnRecordClearFields()
{
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
}
void CEx31bView::OnRecordDelete()
{
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
e->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 CEx31bView::OnUpdateRecordDelete(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSet->IsEOF());
}
void CEx31bView::OnRecordUpdate()
{
m_pSet->Edit();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void CEx31bView::OnUpdateRecordUpdate(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSet->IsEOF());
}
activex
Procedure:
1. Run AppWizard(exe) . Select SDI and check ActiveXControls
2. ProjectsAdd to Project-Components and controls-Registered Activex controls select calendar control 8.0.
3. Insert Resource Dialog-IDD_DIALOG1(add calendar control 8.0)
4. Create a class CActiveXDialog for the dialog resource.
5. Modify the ActiveXDialog.cpp file and ProjectView.cpp.
6. Build and tes
7. tActiveXDialog.cpp
//BN_CLICKED
void CActiveXDialogl::OnSelectdate()
{
CDataExchange dx(this,TRUE);
DDX_Text(&dx, IDC_DAY,m_day);
DDX_Text(&dx, IDC_MONTH,m_month);
DDX_Text(&dx, IDC_YEAR,m_year);
m_calendar.SetDay(m_day);
m_calendar.SetMonth(m_month);
m_calendar.SetYear(m_year);
}
//BN_CLICKED
void CActiveXDialog::OnNextweek()
{
m_calendar.NextWeek();
}
//BN_CLICKED
void CActiveXDialog::OnNextday()
{
m_calendar.NextDay();
}
// calendar - Click event
void CActiveXDialog::OnClickCalendar1()
{
COleDateTime dt(m_calendar.GetValue());
SetWindowText(dt.Format(" date chosen %#x"));
}
//calendar - NewMonth
void CActiveXDialog::OnNewMonthCalendar1()
{
AfxMessageBox("EVENT: New Month ");
}
//Dialog - WM_LBUTTONDOWN
void CActiveXDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
COleDateTime today=COleDateTime::GetCurrentTime();
dlg.m_varvalue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0,0);
COleDateTime date(dlg.m_varvalue);
MessageBox(date.Format("%B,%d,%y"));
}
PROJVIEW.cpp
#include "ActiveXDialog.h"
//WM_LBUTTONDOWN
void CProjView::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
dlg.DoModal();
}
1. Run AppWizard(exe) . Select SDI and check ActiveXControls
2. ProjectsAdd to Project-Components and controls-Registered Activex controls select calendar control 8.0.
3. Insert Resource Dialog-IDD_DIALOG1(add calendar control 8.0)
4. Create a class CActiveXDialog for the dialog resource.
5. Modify the ActiveXDialog.cpp file and ProjectView.cpp.
6. Build and tes
7. tActiveXDialog.cpp
//BN_CLICKED
void CActiveXDialogl::OnSelectdate()
{
CDataExchange dx(this,TRUE);
DDX_Text(&dx, IDC_DAY,m_day);
DDX_Text(&dx, IDC_MONTH,m_month);
DDX_Text(&dx, IDC_YEAR,m_year);
m_calendar.SetDay(m_day);
m_calendar.SetMonth(m_month);
m_calendar.SetYear(m_year);
}
//BN_CLICKED
void CActiveXDialog::OnNextweek()
{
m_calendar.NextWeek();
}
//BN_CLICKED
void CActiveXDialog::OnNextday()
{
m_calendar.NextDay();
}
// calendar - Click event
void CActiveXDialog::OnClickCalendar1()
{
COleDateTime dt(m_calendar.GetValue());
SetWindowText(dt.Format(" date chosen %#x"));
}
//calendar - NewMonth
void CActiveXDialog::OnNewMonthCalendar1()
{
AfxMessageBox("EVENT: New Month ");
}
//Dialog - WM_LBUTTONDOWN
void CActiveXDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
COleDateTime today=COleDateTime::GetCurrentTime();
dlg.m_varvalue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0,0);
COleDateTime date(dlg.m_varvalue);
MessageBox(date.Format("%B,%d,%y"));
}
PROJVIEW.cpp
#include "ActiveXDialog.h"
//WM_LBUTTONDOWN
void CProjView::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
dlg.DoModal();
}
DLL
DLL
DLL:
1.Run MFC AppWizard (dll)– check SDI and accept “Regular DD Using Shared MFC DLL” from the AppWizard page.
2.Add the code for the exported function DLLSquareRoot().
3.Build the project to create DLL.
Test:
1.Run MFC AppWizard(exe) – check SDI
3.InsertResourceDialog-IDD_DIALOG1.
4.Use class wizard to add data members and to create a test dialog class.
5.Define OnSqrt() to call the DLL’s exported function and declare.
6.Call DoModal() from view to open the dialog.
7.Project Settings Link TabObject/Library – specify the path to dllsquare.lib file.
8.copy the dllsquare.dll file from “dllsquare” project to the “test” project folder.
Build and test.
Source code:
a) Dllsquare Project:
dllsquare.cpp
#include “math.h”
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (d>=0.0)
return sqrt(d);
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);
TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}
a) Dllsquare Project:
dllsquare.cpp
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (d>=0.0)
return d*d;
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);
TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}
DLL:
1.Run MFC AppWizard (dll)– check SDI and accept “Regular DD Using Shared MFC DLL” from the AppWizard page.
2.Add the code for the exported function DLLSquareRoot().
3.Build the project to create DLL.
Test:
1.Run MFC AppWizard(exe) – check SDI
3.InsertResourceDialog-IDD_DIALOG1.
4.Use class wizard to add data members and to create a test dialog class.
5.Define OnSqrt() to call the DLL’s exported function and declare.
6.Call DoModal() from view to open the dialog.
7.Project Settings Link TabObject/Library – specify the path to dllsquare.lib file.
8.copy the dllsquare.dll file from “dllsquare” project to the “test” project folder.
Build and test.
Source code:
a) Dllsquare Project:
dllsquare.cpp
#include “math.h”
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (d>=0.0)
return sqrt(d);
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);
TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}
a) Dllsquare Project:
dllsquare.cpp
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (d>=0.0)
return d*d;
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);
TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}
MDI
MDI
1.Run AppWizard(.exe),check MDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar< else
ar>>m_strName>>(int&)m_grade;
}
#endif
MDIDoc.h
#include "student.h"
public:
student m_student;
MDIDoc.cpp
CMdiDoc::CMdiDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CMdiDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CMdiDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CMdiDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
MDIView.h
public:
void UpdateControlsFromdoc();
MDIView.cpp
void CMdiView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CMdiView::OnEnter()
{
CMdiDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CMdiView::UpdateControlsFromdoc()
{
CMdiDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);}
1.Run AppWizard(.exe),check MDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar<
ar>>m_strName>>(int&)m_grade;
}
#endif
MDIDoc.h
#include "student.h"
public:
student m_student;
MDIDoc.cpp
CMdiDoc::CMdiDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CMdiDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CMdiDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CMdiDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
MDIView.h
public:
void UpdateControlsFromdoc();
MDIView.cpp
void CMdiView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CMdiView::OnEnter()
{
CMdiDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CMdiView::UpdateControlsFromdoc()
{
CMdiDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);}
SDI
SDI
1.Run AppWizard(.exe),check SDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar< else
ar>>m_strName>>(int&)m_grade;
}
#endif
SDIDoc.h
#include "student.h"
public:
student m_student;
SDIDoc.cpp
CSDIDoc::CSDIDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CSDIDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CSDIDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CSDIDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
SDIView.h
public:
void UpdateControlsFromdoc();
SDIView.cpp
void CSDIView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CSDIView::OnEnter()
{
CSDIDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CSDIView::UpdateControlsFromdoc()
{
CSDIDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);
}
1.Run AppWizard(.exe),check SDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar<
ar>>m_strName>>(int&)m_grade;
}
#endif
SDIDoc.h
#include "student.h"
public:
student m_student;
SDIDoc.cpp
CSDIDoc::CSDIDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CSDIDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CSDIDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CSDIDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
SDIView.h
public:
void UpdateControlsFromdoc();
SDIView.cpp
void CSDIView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CSDIView::OnEnter()
{
CSDIDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CSDIView::UpdateControlsFromdoc()
{
CSDIDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);
}
menutoolbar
Menutoolbar
1) Run application wizard and choose single document
2) Create a Menu bar IDR_MAINFRAME and Toolbar IDR_MAINFRAME in Resource tab
3) Define a Rich Edit Control to transfer and store data
4) In the Doc class
Define the functions
i)OnNewDocument
ii)OnEditClearall
4)In the View class
Define the functions
i)OnDrawSquare
ii)OnDrawCircle
iii)OnDrawPattern
iv)OnUpdateDrawSquare
v)OnUpdateDrawCircle
vi)OnUpdateDrawPattern
vii)OnTransferGetData
viii)OnTransferStoreData
ix)OnCreate
x)OnSize
b)Build and Execute the functions
CODE
Menudoc.h
Public:
CString m_strText;
Menudoc.cpp
BOOL CMenuDoc::OnNewDocument()
{
If(!CDocument::OnNewDocument())
return FALSE;
m_strText=”Hello(from CMenuDoc::OnNewDocument)”
return TRUE;
}
Void CMenuDoc::OnEditClearall(CCmdUI *pCmdUI)
{
pCmdUI->Enable(!m_strText.IsEmpty());
}
Menuview.h
Public:
CRichEditCtrl m_rich;
Private:
CRect m_rect;
BOOL m_bCircle;
BOOL m_bPattern;
Menuview.cpp
CMenuView::CMenuView():m_rect(0,0,100,100)
{
m_bCircle=TRUE;
m_bPattern=FALSE;
}
Void CMenuView::OnDraw(CDC *pDC)
{
CBrush brush(HS_BDIAGONAL,0L);
If(m_bPattern)
{
pDC->SelectObject(&brush);
}
else
{
pDC->SelectStockObject(WHITE_BRUSH);
}
If(m_bCircle)
{
pDC->Ellipse(m_rect);
ValidateRect(m_rect);
}
else
{
pDC->Rectangle(m_rect);
ValidateRect(m_rect);
}
pDC->SelectStockObject(WHITE_BRUSH);
}
Void CMenuView::OnTransferGetData()
{
CMenuDoc *pDoc=GetDocument();
m_rich.SetWindowText(pDC->m_strText);
m_rich.SetModify(FALSE);
}
Void CMenuView::OnTransferStoreData()
{
CMenuDoc *pDoc=GetDocument();
m_rich.GetWindowText(pDC->m_strText);
m_rich.SetModify(FALSE);
}
Int CMenuView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CRect rect(0,0,0,0);
If(CView::OnCreate(lpCreateStruct==-1)
return -1;
m_rich.Create(ES_MULTILINE|ES_WANTRETURN|WS_CHILD|WS_VISIBLE|WS_VSCROLL,rect,this,1);
return0;
}
Void CMenuView::OnSize(UINT nType,int cx,int cy)
{
CView::OnSize(nType,cx,cy);
CRect rect;
GetClientRect(rect);
M_rich.SetWindowPos(&wndTop,0,0,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW);
}
void CMenuView::OnDrawCircle()
{
m_bCircle=TRUE;
m_rect+=CPoint(25,25);
InvalidateRect(m_rect);
}
Void CMenuView::OnDrawPattern()
{
m_bPattern^=1;
}
void CMenuView::OnDrawSquare()
{
m_bCircle=FALSE;
m_rect+=CPoint(25,25);
InvalidateRect(m_rect);
}
void CMenuView::OnUpdateDrawCircle(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bCircle);
}
void CMenuView::OnUpdateDrawPattern(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bPattern);
}
void CMenuView::OnUpdateDrawSquare(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bCircle);
}
1) Run application wizard and choose single document
2) Create a Menu bar IDR_MAINFRAME and Toolbar IDR_MAINFRAME in Resource tab
3) Define a Rich Edit Control to transfer and store data
4) In the Doc class
Define the functions
i)OnNewDocument
ii)OnEditClearall
4)In the View class
Define the functions
i)OnDrawSquare
ii)OnDrawCircle
iii)OnDrawPattern
iv)OnUpdateDrawSquare
v)OnUpdateDrawCircle
vi)OnUpdateDrawPattern
vii)OnTransferGetData
viii)OnTransferStoreData
ix)OnCreate
x)OnSize
b)Build and Execute the functions
CODE
Menudoc.h
Public:
CString m_strText;
Menudoc.cpp
BOOL CMenuDoc::OnNewDocument()
{
If(!CDocument::OnNewDocument())
return FALSE;
m_strText=”Hello(from CMenuDoc::OnNewDocument)”
return TRUE;
}
Void CMenuDoc::OnEditClearall(CCmdUI *pCmdUI)
{
pCmdUI->Enable(!m_strText.IsEmpty());
}
Menuview.h
Public:
CRichEditCtrl m_rich;
Private:
CRect m_rect;
BOOL m_bCircle;
BOOL m_bPattern;
Menuview.cpp
CMenuView::CMenuView():m_rect(0,0,100,100)
{
m_bCircle=TRUE;
m_bPattern=FALSE;
}
Void CMenuView::OnDraw(CDC *pDC)
{
CBrush brush(HS_BDIAGONAL,0L);
If(m_bPattern)
{
pDC->SelectObject(&brush);
}
else
{
pDC->SelectStockObject(WHITE_BRUSH);
}
If(m_bCircle)
{
pDC->Ellipse(m_rect);
ValidateRect(m_rect);
}
else
{
pDC->Rectangle(m_rect);
ValidateRect(m_rect);
}
pDC->SelectStockObject(WHITE_BRUSH);
}
Void CMenuView::OnTransferGetData()
{
CMenuDoc *pDoc=GetDocument();
m_rich.SetWindowText(pDC->m_strText);
m_rich.SetModify(FALSE);
}
Void CMenuView::OnTransferStoreData()
{
CMenuDoc *pDoc=GetDocument();
m_rich.GetWindowText(pDC->m_strText);
m_rich.SetModify(FALSE);
}
Int CMenuView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CRect rect(0,0,0,0);
If(CView::OnCreate(lpCreateStruct==-1)
return -1;
m_rich.Create(ES_MULTILINE|ES_WANTRETURN|WS_CHILD|WS_VISIBLE|WS_VSCROLL,rect,this,1);
return0;
}
Void CMenuView::OnSize(UINT nType,int cx,int cy)
{
CView::OnSize(nType,cx,cy);
CRect rect;
GetClientRect(rect);
M_rich.SetWindowPos(&wndTop,0,0,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW);
}
void CMenuView::OnDrawCircle()
{
m_bCircle=TRUE;
m_rect+=CPoint(25,25);
InvalidateRect(m_rect);
}
Void CMenuView::OnDrawPattern()
{
m_bPattern^=1;
}
void CMenuView::OnDrawSquare()
{
m_bCircle=FALSE;
m_rect+=CPoint(25,25);
InvalidateRect(m_rect);
}
void CMenuView::OnUpdateDrawCircle(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bCircle);
}
void CMenuView::OnUpdateDrawPattern(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bPattern);
}
void CMenuView::OnUpdateDrawSquare(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bCircle);
}
Subscribe to:
Posts (Atom)