mc9246-visual programming lab

68
1. SDK PROGRAM FOR CREATING SIMPLE WINDOW SOURCE CODE: #include<afxwin.h> class mywnd : public CFrameWnd { public: mywnd() { Create(0, "My window"); } }; class myapp: public CWinApp { public : BOOL InitInstance() { mywnd *p; p = new mywnd; m_pMainWnd = p ; // main thread p->ShowWindow(SW_SHOWNORMAL); return TRUE; } };

Upload: annamyem

Post on 08-Nov-2014

231 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: MC9246-Visual Programming Lab

1. SDK PROGRAM FOR CREATING SIMPLE WINDOW

SOURCE CODE:

#include<afxwin.h>

class mywnd : public CFrameWnd

{

public:

mywnd()

{

Create(0, "My window");

}

};

class myapp: public CWinApp

{

public :

BOOL InitInstance()

{

mywnd *p;

p = new mywnd;

m_pMainWnd = p ; // main thread

p->ShowWindow(SW_SHOWNORMAL);

return TRUE;

}

};

myapp A;

Page 2: MC9246-Visual Programming Lab

OUTPUT:

Page 3: MC9246-Visual Programming Lab

2. SDK PROGRAM FOR KEYBOARD AND MOUSE EVENTS

SOURCE CODE:

#include<windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)

{

static TCHAR szAppname[]=TEXT("Keyboard&mouse events--*umayal*");

HWND hwnd;

MSG msg;

WNDCLASS WC;

WC.style = CS_HREDRAW|CS_VREDRAW;

WC.lpfnWndProc=WndProc;

WC.cbClsExtra =0;

WC.cbWndExtra =0;

WC.hInstance =hInstance;

WC.hIcon=LoadIcon(NULL,IDI_APPLICATION);

WC.hCursor=LoadCursor(NULL,IDC_ARROW);

WC.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);

WC.lpszMenuName =NULL;

WC.lpszClassName=szAppname;

if(!RegisterClass(&WC))

{

MessageBox(NULL,TEXT("THIS PROGRAMREQ.WINDOWS NT"),

szAppname,MB_ICONERROR);

return 0;

}

Page 4: MC9246-Visual Programming Lab

hwnd=CreateWindow(szAppname,TEXT("Keyboard&mouse events...*umayal*"),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)

{

switch(msg)

{

case WM_LBUTTONDOWN:

MessageBox(GetFocus(),"you have pressed leftmouse button","MouseEvent...* *",

MB_OK|MB_ICONINFORMATION);

break;

case WM_RBUTTONDOWN:

MessageBox(GetFocus(),"you have pressed rightmouse button","MouseEvent...* *",

MB_OK|MB_ICONINFORMATION);

break;

case WM_KEYDOWN:

MessageBox(GetFocus(),"you have pressed keyboard button","keyboard...**",

MB_OK|MB_ICONINFORMATION);

Page 5: MC9246-Visual Programming Lab

break;

case WM_CLOSE:

DestroyWindow(hwnd);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd,msg,wParam,lParam);

}

return 0;

}

OUTPUT:

Page 6: MC9246-Visual Programming Lab
Page 7: MC9246-Visual Programming Lab
Page 8: MC9246-Visual Programming Lab

3. SDK TYPE PROGRAM FOR GDI OBJECTS.

SOURCE CODE:

#include<afxwin.h>

class myframe:public CFrameWnd

{

public:

myframe()

{

Create(0,"Line and Rectangle");

}

void OnPaint()

{

CPaintDC d(this);

CPen mypen(PS_SOLID,5,RGB(0,0,255));

CBrush mybrush(RGB(255,0,0));

d.SelectObject(&mypen);

d.SelectObject(&mybrush);

d.MoveTo(50,50);

d.LineTo(250,10);

d.Rectangle(50,100,250,200);

}

DECLARE_MESSAGE_MAP()

Page 9: MC9246-Visual Programming Lab

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_PAINT()

END_MESSAGE_MAP()

class myapp:public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p=new myframe;

p->ShowWindow (3);

m_pMainWnd=p;

return 1;

}

};

myapp a;

Page 10: MC9246-Visual Programming Lab

OUTPUT:

Page 11: MC9246-Visual Programming Lab

4. SIMPLE DIALOG BASED APPLICATION FOR CALCULATOR

DESIGN:

Page 12: MC9246-Visual Programming Lab

SOURCE CODE:

void CCalcDlg::OnCompute() { UpdateData(TRUE);

switch(m_nOperation){case 0:

m_dResult=m_dLeft + m_dRight;break;

case 1:m_dResult=m_dLeft - m_dRight;break;

case 2:m_dResult=m_dLeft * m_dRight;break;

case 3:if(m_dRight!=0.0){

m_dResult=m_dLeft / m_dRight;}else{

AfxMessageBox("Divide by zero");m_dResult=0.0;

}

Page 13: MC9246-Visual Programming Lab

break; default:

TRACE("default:m_nOperation=%d\n",m_nOperation);}UpdateData(FALSE);

}

Page 14: MC9246-Visual Programming Lab

OUTPUT:

Page 15: MC9246-Visual Programming Lab

5. CREATING SINGLE DOCUMENT INTERFACE APPLICATION

SOURCE CODE:

void CSDIDrawView::OnDraw(CDC* pDC)

{ CSDIDrawDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

pDC->Rectangle(50,100,300,150);

pDC->MoveTo(220,90);

pDC->LineTo(90,90);

pDC->TextOut(330,70,"LINE");

pDC->Ellipse(250,80,60,50);

pDC->TextOut(330,40,"ELLIPSE");

pDC->TextOut(330,180,"ARC");

pDC->TextOut(330,210,"CIRCLE");

pDC->Arc(250,170,150,190,200,119,50,1220);

pDC->Ellipse(200,200,250,250);

pDC->Pie(300,500,100,50,75,250,175,300);

pDC->TextOut(330,270,"PIE");

pDC->Rectangle(100,375,200,475);

pDC->TextOut(330,400,"SQUARE");

pDC->RoundRect(530,100,450,50,30,100);

pDC->TextOut(600,70,"RoundRect");

pDC->Chord(530,125,460,200,210,410,10,140);

pDC->TextOut(550,150,"CHORD");

}

Page 16: MC9246-Visual Programming Lab

OUTPUT:

Page 17: MC9246-Visual Programming Lab

6. CREATING MULTIPLE DOCUMENT INTERFACE APPLICATION

SOURCE CODE:

void CEx1_mdiView::OnLButtonDblClk(UINT nFlags, CPoint point)

{

char str[20];

CTime tm=CTime::GetCurrentTime();

sprintf(str,"it's now%02d:%02d:%02d",tm.GetHour(),

tm.GetMinute(),tm.GetSecond());

MessageBox(str,"it is time to quit?",MB_OK);

CView::OnLButtonDblClk(nFlags, point);

}

Page 18: MC9246-Visual Programming Lab

OUTPUT:

Page 19: MC9246-Visual Programming Lab

7. CREATING MODAL DIALOG

OUTPUT:

Page 20: MC9246-Visual Programming Lab
Page 21: MC9246-Visual Programming Lab

8. CREATING MODELESS DIALOG

DESIGN :

Page 22: MC9246-Visual Programming Lab

SOURCE CODE:

void CModelessdiaDlg::OnRunPgm()

{

UpdateData(TRUE);

CString strPgmName;

strPgmName=m_strRunPgm;

strPgmName.MakeUpper();

if(strPgmName=="NOTEPAD")

WinExec("notepad.exe",SW_SHOW);

if(strPgmName=="MSPAINT")

WinExec("mspaint.exe",SW_SHOW);

if(strPgmName=="FREECALL")

WinExec("freecall.exe",SW_SHOW);

if(strPgmName=="CALCULATOR")

WinExec("calc.exe",SW_SHOW);

if(strPgmName=="EXPLORER")

WinExec("explorer.exe",SW_SHOW);

if(strPgmName=="COMMAND")

WinExec("cmd.exe",SW_SHOW);

if(strPgmName=="TOURSTART")

WinExec("tourstart.exe",SW_SHOW);

}

Page 23: MC9246-Visual Programming Lab

OUTPUT:

Page 24: MC9246-Visual Programming Lab

9. PROGRAMMING FOR READING AND WRITING INTO DOCUMENTS

Code for Cline class

CLine::CLine(CPoint sp, CPoint ep)

{

startP=sp;

endP=ep;

}

void CLine::Draw(CDC *pDC)

{

CPen lpen(PS_SOLID,5,RGB(150,000,100));

CPen* Open=pDC->SelectObject(&lpen);

pDC->MoveTo(startP);

pDC->LineTo(endP);

pDC->SelectObject(Open);

}

void CLine::serialize(CArchive &ar)

{

CObject::Serialize(ar);

if(ar.IsStoring())

ar<<startP<<endP;

else

ar>>startP>>endP;

}

Page 25: MC9246-Visual Programming Lab

Code for CSerializeDoc class

CLine* CSerializeDoc::AddLine(CPoint sp, CPoint ep)

{

CLine*tLine=new CLine(sp,ep);

Lines.Add(tLine);

return tLine;

}

CLine* CSerializeDoc::GetLine(int nIndex)

{

return (CLine*)Lines[nIndex];

}

int CSerializeDoc::GetLineCount()

{

return Lines.GetSize();

}

void CSerializeDoc::DeleteContents()

{

int lcount=Lines.GetSize();

if(lcount)

{

int lposition;

for(lposition=0;lposition<lcount;lposition++)

{

Page 26: MC9246-Visual Programming Lab

delete Lines[lposition];

}

Lines.RemoveAll();

}

CDocument::DeleteContents();

}

void CSerializeView::OnLButtonDown(UINT nFlags, CPoint point)

{ SetCapture();

prev=point;

CView::OnLButtonDown(nFlags, point); }

void CSerializeView::OnLButtonUp(UINT nFlags, CPoint point)

{ if(GetCapture()==this)

ReleaseCapture();

CView::OnLButtonUp(nFlags, point); }

void CSerializeView::OnMouseMove(UINT nFlags, CPoint point)

{ if((nFlags&MK_LBUTTON)==MK_LBUTTON)

{ if(GetCapture()==this)

{ CClientDC dc(this);

CLine*tLine=GetDocument()->AddLine(prev,point);

tLine->Draw(&dc);

prev=point; } }

CView::OnMouseMove(nFlags, point); }

Page 27: MC9246-Visual Programming Lab

OUTPUT:

Page 28: MC9246-Visual Programming Lab

10. DYNAMIC CONTROL FOR SLIDER CONTROL

SOURCE CODE:

#include<afxwin.h>

#include<afxcmn.h>

class myframe:public CFrameWnd

{

private:

CSliderCtrl sli;

CButton gr;

public:

myframe()

{

CString mywindowclass;

mywindowclass=AfxRegisterWndClass(

CS_VREDRAW|CS_HREDRAW,0,

(HBRUSH)::GetStockObject(LTGRAY_BRUSH),0);

Create(mywindowclass,"Slider Control");

}

int OnCreate(LPCREATESTRUCT I)

{

CFrameWnd::OnCreate(I);

gr.Create("SLIDER DISPLAY",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,CRect(30,30,310,100),this,1);

sli.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ|TBS_AUTOTICKS|TBS_BOTTOM|TBS_ENABLESELRANGE,CRect(35,50,305,90),this,2);

sli.SetRange(0,8);

Page 29: MC9246-Visual Programming Lab

sli.SetPos(2);

sli.SetSelection(0,2);

sli.SetPageSize(3);

return 0;

}

void OnHScroll(UINT code,UINT pos,CScrollBar *scroll)

{

switch(code)

{

case TB_LINEUP:

case TB_LINEDOWN:

case TB_PAGEUP:

case TB_PAGEDOWN:

case TB_TOP:

case TB_BOTTOM:

pos=sli.GetPos();

sli.SetSelection(0,pos);

sli.SetTic(pos);

break;

case TB_THUMBPOSITION:

sli.SetSelection(0,pos);

sli.SetTic(pos);

break;

case TB_THUMBTRACK:

sli.SetSelection(0,pos);

Page 30: MC9246-Visual Programming Lab

sli.SetTic(pos);

break;

}

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_CREATE()

ON_WM_HSCROLL()

END_MESSAGE_MAP()

class myapp:public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p=new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

}; myapp a;

Page 31: MC9246-Visual Programming Lab

OUTPUT:

Page 32: MC9246-Visual Programming Lab

11. DYNAMIC CONTROL FOR TREE VIEW CREATION

SOURCE CODE:

#include<afxwin.h>

#include<afxcmn.h>

class myframe:public CFrameWnd

{

private:

CTreeCtrl tree;

public:

myframe()

{

Create(0,"Tree view control");

Page 33: MC9246-Visual Programming Lab

}

int OnCreate(LPCREATESTRUCT I)

{

HTREEITEM lang,opersys,c,cpp,java;

CFrameWnd::OnCreate(I);

tree.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS|TVS_SHOWSELALWAYS,CRect(30,30,300,350),this,1);

lang=tree.InsertItem("Computer languages",TVI_ROOT,TVI_SORT);

c=tree.InsertItem("C",lang,TVI_SORT);

tree.InsertItem("TC",c);

tree.InsertItem("QC",c);

tree.InsertItem("MSC",c);

cpp=tree.InsertItem("c++",lang,TVI_SORT);

tree.InsertItem("VC++",cpp);

tree.InsertItem("Borland c++",cpp);

java=tree.InsertItem("java",lang,TVI_SORT);

tree.InsertItem("VJ++",java);

tree.InsertItem("Symantec cafe",java);

tree.InsertItem("Sun JDK",java);

opersys=tree.InsertItem("operating systems",TVI_ROOT,TVI_SORT);

tree.InsertItem("win 3.1",opersys);

tree.InsertItem("win 95",opersys);

tree.InsertItem("win NT",opersys);

return 0;

}

Page 34: MC9246-Visual Programming Lab

int OnNotify(WPARAM w,LPARAM I,LRESULT *r)

{

HTREEITEM h;

CString str;

CWnd::OnNotify(w,I,r);

NM_TREEVIEW *p=(NM_TREEVIEW *)I;

if(p->hdr.code==TVN_SELCHANGED)

{

h=tree.GetSelectedItem();

str=tree.GetItemText(h);

MessageBox(str,"you have selected");

}

return 1;

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_CREATE()

END_MESSAGE_MAP()

class myapp:public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p=new myframe;

Page 35: MC9246-Visual Programming Lab

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

}; myapp a;

Page 36: MC9246-Visual Programming Lab

OUTPUT:

Page 37: MC9246-Visual Programming Lab

12. CREATING AND DYNAMIC SPLITTER WINDOW

SOURCE CODE:

BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/,

CCreateContext* pContext)

{

return m_wndSplitter.Create( this,

2, 2, // TODO: adjust the number of rows, columns

CSize(10, 10), // TODO: adjust the minimum pane size

pContext);

}

1. Build and run the project.

Page 38: MC9246-Visual Programming Lab

OUTPUT:

Page 39: MC9246-Visual Programming Lab

13.CREATING DLLS AND USING THEM.

// RegDLL.cpp : Defines the initialization routines for the DLL.

#include "stdafx.h"#include "RegDLL.h"

#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = _ _FILE_ _;#endif

(generated comment lines omitted)

//////////////////////////////////////////////////////////////////////// CRegDLLApp

BEGIN_MESSAGE_MAP(CRegDLLApp, CWinApp) //{{AFX_MSG_MAP(CRegDLLApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAPEND_MESSAGE_MAP()

// CRegDLLApp construction

CRegDLLApp::CRegDLLApp(){ // TODO: add construction code here, // Place all significant initialization in InitInstance}

// The one and only CRegDLLApp object

CRegDLLApp theApp;

extern "C" __declspec(dllexport) long RegDLLFactorial(long f){ long fact=1; AFX_MANAGE_STATE(AfxGetStaticModuleState()); TRACE("Entering RegDLLSquareRoot\n");

while (f>0){

fact=fact*f; f--;

}

Page 40: MC9246-Visual Programming Lab

return (fact);}

To create a test client application :

void CRegDLLTestDialog::OnFact() {

UpdateData(TRUE);m_dOutput=RegDLLFactorial(m_dInput);UpdateData(FALSE);

}

You'll have to declare the RegDLLFactorial function as an imported function. Add the following line to the RegDLLTestDialog.h file:

extern "C" __declspec(dllimport) long RegDLLFactorial(long f)

void CRegDLLTestView::OnTestRegdll() { CRegDLLTestDialog dlg; dlg.DoModal();}

Page 41: MC9246-Visual Programming Lab

OUTPUT:

Page 42: MC9246-Visual Programming Lab
Page 43: MC9246-Visual Programming Lab

14. WINSOCK AND WININET & INTERNET EXPLORER COMMON CONTROLS.

Page 44: MC9246-Visual Programming Lab

15. DATA ACCESS THROUGH ODBC – CDATABASE, CRECORDSET.

DESIGN:

Page 45: MC9246-Visual Programming Lab

SOURCE CODE:

BOOL CDatabaseView::OnMove(UINT nIDMoveCommand)

{

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;

}

case ID_RECORD_LAST:

m_pSet->MoveLast();

break;

default:

Page 46: MC9246-Visual Programming Lab

ASSERT(FALSE);

}

UpdateData(FALSE);

return TRUE;

}

void CDatabaseView::OnAddRecord()

{

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 CDatabaseView::OnClearfieldsRecord()

{

m_pSet->SetFieldNull(NULL);

Page 47: MC9246-Visual Programming Lab

UpdateData(FALSE);

}

void CDatabaseView::OnDeleteRecord()

{

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();

}

Page 48: MC9246-Visual Programming Lab

UpdateData(FALSE);

}

void CDatabaseView::OnUpdateDeleteRecord(CCmdUI* pCmdUI)

{

pCmdUI->Enable(!m_pSet->IsEOF());

}

void CDatabaseView::OnUpdateRecord()

{

m_pSet->Edit();

UpdateData(TRUE);

if(m_pSet->CanUpdate())

{

m_pSet->Update();

}

}

void CDatabaseView::OnUpdateUpdateRecord(CCmdUI* pCmdUI)

{

pCmdUI->Enable(!m_pSet->IsEOF());

}

Page 49: MC9246-Visual Programming Lab

OUTPUT:

Page 50: MC9246-Visual Programming Lab

16. CREATING ACTIVEX CONTROL AND USING IT.

PROCEDURE:

Steps for building the calendar application:

1. Verify that the Calendar control is registered.

If the control does not appear in the Visual C++ Gallery's Registered ActiveX Controls page, copy the files MSCal.ocx, MSCal.hlp, and MSCal.cnt to your system directory and register the control by running the REGCOMP program.

2. Run AppWizard to produce \vcpp32\ ActiveX.

a. Select Single Document in Step 1b. In step3, make sure the ActiveX Controls option is selected.c. Deselect Printing and Print Preview in step 4 of 6. d. In the AppWizard Step 3 dialog, make sure the ActiveX Controls option is

selected.

3. Install the Calendar control in the Calendar project.

a. Choose Add To Project from Visual C++'s Project menu, and then choose Components And Controls.

b. Choose Registered ActiveX Controls, and then choose Calendar Control 8.0.c. ClassWizard generates two classes in the ActiveX directory, as shown here.

4. Edit the Calendar control class to handle help messages.

Page 51: MC9246-Visual Programming Lab

a. Add Calendar.cpp to the following message map code:

BEGIN_MESSAGE_MAP(CCalendar, CWnd) ON_WM_HELPINFO()END_MESSAGE_MAP()

b. In the same file, add the OnHelpInfo function:

BOOL CCalendar::OnHelpInfo(HELPINFO* pHelpInfo) { // Edit the following string for your system

::WinHelp(GetSafeHwnd(),

"c:\\winnt\\system32\\mscal.hlp",HELP_FINDER, 0);

return FALSE;}

c. In Calendar.h, add the function prototype and declare the message map:

protected:

afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo);

DECLARE_MESSAGE_MAP()

The OnHelpInfo function is called if the user presses the F1 key when the Calendar control has the input focus. We have to add the message map code by hand because ClassWizard doesn't modify generated ActiveX classes.

5. Use the dialog editor to create a new dialog resource.

a. Choose Resource from Visual C++'s Insert menu, and then choose Dialog.

b. The dialog editor assigns the ID IDD_DIALOG1 to the new dialog.

c. Next change the ID to IDD_ACTIVEXDIALOG, change the dialog caption to

ActiveX Dialog, and set the dialog's Context Help property (on the More Styles

page).

d. Accept the default OK and Cancel buttons with the IDs IDOK and IDCANCEL,

and then add the other controls.

e. Then add the other controls as shown below:

Page 52: MC9246-Visual Programming Lab

f. Make the Select Date button as the default button.

g. Drag the Calendar control from the control palette.

h. Then set an appropriate tab order.

i. Assign control IDs as shown in the following table.

Control ID

Calendar control IDC_CALENDAR1

Select Date button IDC_SELECTDATE

Edit control IDC_DAY

Edit control IDC_MONTH

Edit control IDC_YEAR

Next Week button IDC_NEXTWEEK

Page 53: MC9246-Visual Programming Lab

6. Use ClassWizard to create the CActiveXDialog class.

a. If you run ClassWizard directly from the dialog editor window, it will know that

you want to create a CDialog-derived class based on the

IDD_ACTIVEXDIALOG template.

b. Simply accept the default options, and name the class CActiveXDialog.

c. Click on the ClassWizard Message Maps tab, and then add the message handler

functions shown in the table below.

d. To add a message handler function, click on an object ID, click on a message,

and click the Add Function button.

e. If the Add Member Function dialog box appears, type the function name and

click the OK button.

Object ID Message Member Function

CActiveXDialog WM_INITDIALOG OnInitDialog (virtual function)

IDC_CALENDAR1 NewMonth (event) OnNewMonthCalendar1

IDC_SELECTDATE BN_CLICKED OnSelectDate

IDC_NEXTWEEK BN_CLICKED OnNextWeek

IDOK BN_CLICKED OnOK (virtual function)

Page 54: MC9246-Visual Programming Lab

7. Use ClassWizard to add data members to the CActiveXDialog class.

a. Click on the Member Variables tab, and then add the data members as shown in the illustration below.

b. You might think that the ClassWizard ActiveX Events tab is for mapping ActiveX control events in a container.

c. That's not true: it's for ActiveX control developers who are defining events for a control.

8. Edit the CActiveXDialog class.

a. Add the m_varValue and m_BackColor data members.

b. Edit the code for the five handler functions OnInitDialog, nNewMonthCalendar1,

OnSelectDate, OnNextWeek, and OnOK.

9. Add the following boldface code in the ACTIVEXDIALOG.H

Public:

COleVariant m_varValue;

unsigned long m_BackColor;

Page 55: MC9246-Visual Programming Lab

Note: declare these 2 variables at the end of the Dialog Data section.

10. Edit the ACTIVEXDIALOG.CPP

a. Add the following boldface code.

CActiveXDialog::CActiveXDialog(CWnd* pParent /*=NULL*/)

: CDialog(CActiveXDialog::IDD, pParent){ // {{AFX_DATA_INIT (CActiveXDialog)

m_sDay = 0;

m_sMonth = 0;

m_sYear = 0;

//}}AFX_DATA_INIT

m_BackColor = 0x8000000F;}

void CActiveXDialog::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CActiveXDialog)

DDX_Control(pDX, IDC_CALENDAR1, m_calendar);

DDX_Text(pDX, IDC_DAY, m_sDay);

DDX_Text(pDX, IDC_MONTH, m_sMonth);

DDX_Text(pDX, IDC_YEAR, m_sYear);

//}}AFX_DATA_MAP

DDX_OCColor(pDX, IDC_CALENDAR1, DISPID_BACKCOLOR,

m_BackColor);}BOOL CActiveXDialog::OnInitDialog()

{

CDialog::OnInitDialog();

m_calendar.SetValue(m_varValue); // no DDX for VARIANTs

return TRUE; // return TRUE unless you set the focus to a control

// EXCEPTION: OCX Property Pages should return FALSE

Page 56: MC9246-Visual Programming Lab

}

void CActiveXDialog::OnNewMonthCalendar1()

{

AfxMessageBox("EVENT: CActiveXDialog::OnNewMonthCalendar1");

}

void CActiveXDialog::OnSelectDate()

{

CDataExchange dx(this, TRUE);

DDX_Text(&dx, IDC_DAY, m_sDay);

DDX_Text(&dx, IDC_MONTH, m_sMonth);

DDX_Text(&dx, IDC_YEAR, m_sYear);

m_calendar.SetDay(m_sDay);

m_calendar.SetMonth(m_sMonth);

m_calendar.SetYear(m_sYear);

}

void CActiveXDialog::OnNextWeek()

{

m_calendar.NextWeek();

}

void CActiveXDialog::OnOK()

{

CDialog::OnOK();

m_varValue = m_calendar.GetValue(); // no DDX for VARIANTs

}

11. The OnSelectDate function is called when the user clicks the Select Date button. The

function gets the day, month, and year values from the three edit controls and

transfers them to the control's properties. ClassWizard can't add DDX code for the

BackColor property, so you must add it by hand. In addition, there's no DDX code for

VARIANT types, so you must add code to the OnInitDialog and OnOK functions to

set and retrieve the date with the control's Value property.

Page 57: MC9246-Visual Programming Lab

12. Connect the dialog to the view.

a. Use ClassWizard to map the WM_LBUTTONDOWN message, and then edit the handler function as follows:

void ActiveXView::OnLButtonDown(UINT nFlags, CPoint point)

{

CActiveXDialog dlg;

dlg.m_BackColor = RGB(255, 251, 240); // light yellow

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"));

}

}

The code sets the background color to light yellow and the date to today's date, displays the modal dialog, and reports the date returned by the Calendar control.

Then need to include the following in ActiveXView.cpp.

#include “ActiveXDialog.h”

13. Edit the virtual OnDraw function in the file ActiveXView.cpp.

a. To prompt the user to press the left mouse button, replace the code in the view class OnDraw function with this single line:

pDC->TextOut(0, 0, "Press the left mouse button here);

Page 58: MC9246-Visual Programming Lab

OUTPUT :