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







Screen shot output


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

Related post