Você está na página 1de 91

1

EX.NO:1

TO CREATE A SIMPLE WINDOW

AIM:
To write a program in VC++ to create a simple window.
ALGORITHM:
1. Select Microsoft Visual C++ 6.0 compiler from the start menu.
2. From the Visual studio select New from the file menu.
3. In the project selection dialog select Win32 Application as the project type
and then enter the name of the project and click next.
4. In the next dialog select the radio button with label An empty Project and
click the finish button.
5. Select New from Visual Studio file menu and select the files tab on the dialog
and select C++ source file. Enter the file name and the name of the project
then click OK.
6. Enter the code in the editor and save. The file is automatically added to the
project or by the selection of the radio box labeled Add to project.
7. Open C++ file. Enter the VC++ source code.
8. Select settings from the project menu and set Use MFC in a Static Library
and clicking the right button provided in the Microsoft Foundation Classes
bar.
9. Then Build the program by clicking build option from build menu.
10. Then Compile and Run the program.

2
PROGRAM:
#include<afxwin.h>
class frame1:public CFrameWnd
{
public:
frame1()
{
Create(0,"window");
}
};
class window1:CWinApp
{
public:
BOOL InitInstance()
{
frame1 *obj1;
obj1=new frame1;
obj1->ShowWindow(true);
m_pMainWnd=obj1;
return(TRUE);
}
};
window1 obj;
OUTPUT:

RESULT:
Thus the simple window created by using VC++.

3
EX.NO:2

TO DRAW A RECTANGLE

AIM:
To draw a rectangle by using VC++ program.
ALGORITHM:
1. Select Microsoft Visual C++ 6.0 compiler from the start menu.
2. From the Visual studio select New from the file menu.
3. In the project selection dialog select Win32 Application as the project type
and then enter the name of the project and click next.
4. In the next dialog select the radio button with label An empty Project and
click the finish button.
5. Select New from Visual Studio file menu and select the files tab on the dialog
and select C++ source file. Enter the file name and the name of the project
then click OK.
6. Enter the code in the editor and save. The file is automatically added to the
project or by the selection of the radio box labeled Add to project.
7. Select project settings option from the project menu and set Use MFC in a
Shared DLL and click OK.
8. Then Build and Run the program.

4
PROGRAM:
#include<afxwin.h>
class Owntools:public CFrameWnd
{
public:
Owntools()
{
Create(0,"My pen and brush in window");
}
void OnPaint()
{
CPaintDC cp(this);
CPen p(PS_SOLID,1,RGB(0,60,255));
CBrush b(RGB(55,220,0));
cp.SelectObject(&p);
cp.SelectObject(&b);
//cp.MoveTo(50,60);
cp.Rectangle(50,100,250,200);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(Owntools,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class window:public CWinApp
{
public:int InitInstance()
{
Owntools *win;
win=new Owntools;
win->ShowWindow(1);
m_pMainWnd=win;
return(1);
}
};
window a;

5
OUTPUT:

RESULT:
Thus the rectangle was drawn by using VC++.

6
EX.NO:3

TO CREATE A HELLOWORLD WINDOW

AIM:
To create a HELLOWORLD window by using VC++ program.
ALGORITHM:
1. Select Microsoft Visual C++ 6.0 compiler from the start menu.
2. From the Visual studio select New from the file menu.
3. In the project selection dialog select Win32 Application as the project type
and then enter the name of the project and click next.
4. In the next dialog select the radio button with label An empty Project and
click the finish button.
5. Select New from Visual Studio file menu and select the files tab on the dialog
and select C++ source file. Enter the file name and the name of the project
then click OK.
6. Enter the code in the editor and save. The file is automatically added to the
project or by the selection of the radio box labeled Add to project.
7. Select project settings option from the project menu and set Use MFC in a
Shared DLL and click OK.
8. Then Build and Run the program.

7
PROGRAM:
#include<afxwin.h>
class CHelloWindow:public CFrameWnd
{
public:
CHelloWindow();
~CHelloWindow();
CStatic *cst;
};
class CHelloApp:public CWinApp
{
public:
virtual BOOL InitInstance()
{
m_pMainWnd=new CHelloWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
};
CHelloWindow::CHelloWindow()
{
Create(NULL,"HelloMFC
World",WS_OVERLAPPEDWINDOW,CRect(50,50,300,300));
cst=new CStatic();
cst->Create("Hello World",WS_CHILD|
WS_VISIBLE,CRect(10,10,170,170),this);
}
CHelloWindow::~CHelloWindow()
{
delete cst;
}
CHelloApp HelloApp;

8
OUTPUT:

RESULT:
Thus the hello world window was created successfully.

9
EX.NO:4

TO DRAW A FREEHAND DRAWING USING


MOUSE EVENTS

AIM:
To draw a freehand drawing using mouse events in VC++.
ALGORITHM:
1. From the Visual studio select New from the file menu.
2. In the project selection dialog select Win32 Application as the project type
and then enter the name of the project and click next.
3. In the next dialog select the radio button with label An empty Project and
click the finish button.
4. Select New from Visual Studio file menu and select the files tab on the dialog
and select C++ source file. Enter the file name and the name of the project
then click OK.
5. Enter the code in the editor and save. The file is automatically added to the
project or by the selection of the radio box labeled Add to project.
6. Select project settings option from the project menu and set Use MFC in a
Shared DLL and click OK.
7. Then Build and Run the program.

10
PROGRAM:
#include<afxwin.h>
WNDCLASS a;
int X1,X2,Y1,Y2,f=0;
long _stdcall Myfunc(HWND,UINT,UINT,long);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int I)
{
HWND h;
MSG m;
a.lpfnWndProc=Myfunc;
a.hInstance=i;
a.lpszClassName="My";
a.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
RegisterClass(&a);
h=CreateWindow("MY","First
Class",WS_OVERLAPPEDWINDOW,10,10,150,200,0,0,i,0);
ShowWindow(h,3);
while(GetMessage(&m,0,0,0))
DispatchMessage(&m);
return 0;
}
long _stdcall Myfunc(HWND W,UINT X,UINT Y,long Z)
{
HDC d;
switch(X)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
if(f==0)
{
X1=LOWORD(Z);
Y1=HIWORD(Z);
f=1;
}
break;
case WM_MOUSEMOVE:
if(f==1)
{
X2=LOWORD(Z);
Y2=HIWORD(Z);
d=GetDC(W);
MoveToEx(d,Y1,X1,0);

11
LineTo(d,Y2,Y2);
ReleaseDC(W,d);
X1=X2;
Y1=Y2;
}
break;
case WM_LBUTTONUP:
f=0;
break;
default:
return DefWindowProc(W,X,Y,Z);
}
return 0L;
}

OUTPUT:

RESULT:
Thus the freehand drawing was drawn by using mouse events in VC++.

12

TO PERFORM AN ARITHMETIC OPERATIONS


USING KEYBOARD EVENTS

EX.NO:5

AIM:
To perform arithmetic operations using keyboard events in VC++.
ALGORITHM:
1. From the Visual studio select New from the file menu.
2. In the project selection dialog select MFC Appwizard(exe) as the project
type and then enter the name of the project and click next.
3. In the next dialog select the radio button with label dialog based and click
the finish button and OK button.
4. Insert the Static, Edit, Buttons controls in the dialog window.
5. Choose the Edit control and click right side then choose the class wizard
from the menu bar.
6. Then Choose the Edit ID from that and click member variable from the
dialog box and select Add variable then add the member variable then
choose the data type and click OK.
7. Then choose the Button control
(i)And click right side then choose the properties from the menu bar and
change the caption in button dialog box.
(ii)Double click the button, and then type the member function name in
the dialog box then click OK.
8. Then Choose the Static control, click right side then choose the properties
and change the caption.
9. Enter the code in the corresponding controls in the editor and save.
10. Build and run the program.

13
PROGRAM:
// ex5Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ex5.h"
#include "ex5Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int m_text;
/////////////////////////////////////////////////////////////////////////////
// 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

14
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx5Dlg dialog
CEx5Dlg::CEx5Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CEx5Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEx5Dlg)
m_text = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEx5Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx5Dlg)
DDX_Text(pDX, IDC_EDIT1, m_text);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEx5Dlg, CDialog)
//{{AFX_MSG_MAP(CEx5Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, On1)
ON_BN_CLICKED(IDC_BUTTON2, On2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON11, Onplus)
ON_BN_CLICKED(IDC_BUTTON4, On4)
ON_BN_CLICKED(IDC_BUTTON5, On5)
ON_BN_CLICKED(IDC_BUTTON6, On6)
ON_BN_CLICKED(IDC_BUTTON7, On7)
ON_BN_CLICKED(IDC_BUTTON8, On8)
ON_BN_CLICKED(IDC_BUTTON9, On9)
ON_BN_CLICKED(IDC_BUTTON10, Onzero)
ON_BN_CLICKED(IDC_BUTTON12, Onminus)
ON_BN_CLICKED(IDC_BUTTON13, Onmul)
ON_BN_CLICKED(IDC_BUTTON14, Ondiv)
ON_BN_CLICKED(IDC_BUTTON15, Onequal)
ON_BN_CLICKED(IDC_BUTTON16, OnC)
//}}AFX_MSG_MAP

15
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx5Dlg message handlers
BOOL CEx5Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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 < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->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 CEx5Dlg::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 CEx5Dlg::OnPaint()
{

16
int m_text=0;
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 CEx5Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
double op1,op2;
char opr;
int n;
void CEx5Dlg::On1()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=1;
else
m_text=m_text*10+1;
UpdateData(0);
}
void CEx5Dlg::On2()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=2;

17
else
m_text=m_text*10+2;
UpdateData(0);
}
void CEx5Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=3;
else
m_text=m_text*10+3;
UpdateData(0);
}
void CEx5Dlg::On4()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=4;
else
m_text=m_text*10+4;
UpdateData(0);
}
void CEx5Dlg::On5()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=5;
else
m_text=m_text*10+5;
UpdateData(0);
}
void CEx5Dlg::On6()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=6;
else
m_text=m_text*10+6;
UpdateData(0);
}
void CEx5Dlg::On7()
{

18
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=7;
else
m_text=m_text*10+7;
UpdateData(0);
}
void CEx5Dlg::On8()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=8;
else
m_text=m_text*10+8;
UpdateData(0);
}
void CEx5Dlg::On9()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=9;
else
m_text=m_text*10+9;
UpdateData(0);
}
void CEx5Dlg::Onzero()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_text==0)
m_text=0;
else
m_text=m_text*10;
UpdateData(0);
}
void CEx5Dlg::Onplus()
{
// TODO: Add your control notification handler code here
UpdateData(1);
opr='+';
n=m_text;
m_text=0;
UpdateData(0);

19
}
void CEx5Dlg::Onminus()
{
// TODO: Add your control notification handler code here
UpdateData(1);
opr='-';
n=m_text;
m_text=0;
UpdateData(0);
}
void CEx5Dlg::Onmul()
{
// TODO: Add your control notification handler code here
UpdateData(1);
opr='*';
n=m_text;
m_text=0;
UpdateData(0);
}
void CEx5Dlg::Ondiv()
{
// TODO: Add your control notification handler code here
UpdateData(1);
opr='/';
n=m_text;
m_text=0;
UpdateData(0);
}
void CEx5Dlg::Onequal()
{
// TODO: Add your control notification handler code here
UpdateData(1);
switch(opr)
{
case'+': m_text=m_text+n;
break;
case'-': m_text=m_text-n;
break;
case'*': m_text=m_text*n;
break;
case'/':m_text=m_text/n;
break;
}
UpdateData(0);
}

20

void CEx5Dlg::OnC()
{
// TODO: Add your control notification handler code here
m_text=0;
UpdateData(0);
}
OUTPUT:

RESULT:
Thus the arithmetic operations were performed using keyboard events in
VC++.

21
EX.NO:6

TO PERFORM DIALOG BASED APPLICATIONS

AIM:
To perform the following Dialog based applications in VC++ (i)File
Open(ii)Set Color(iii)Set Font (iv) Quit Application.
ALGORITHM:
1. Start the program.
2. Select the File->New->Win 32 Application and enter the project name and
click ok.
3. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
4. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
5. Enter the source code.
6. Choose insert menu from the menu bar, select and click the resources.
7. Select the resources item Menu and press new button.
8. The menu properties shown. Enter the ID value, those are simple integers
like IDD_OPEN_FILE,IDD_SET_COLOR, etc., and enter the caption.
9. Create a dialog resource using the resource editor from the insert menu, and
set the properties to dialog applications like open file, set color, set font, etc.,
10. Save the script1.rc file to the project.
11. Select the file view from the workspace and click at right in the resource files.
12. Choose add files to folder then the resource file (script1.rc) is selected and
click ok.
13. Finally save this file, then Build and Run the program.

22
PROGRAM:
#include<afxwin.h>
#include<afxdlgs.h>
#include<stdio.h>
#include"resource.h"
#define IDC_FONT_STATIC 111
#define IDC_COLOR_STATIC 222
afx_msg UINT APIENTRY hook_proc(HWND hdlg,UINT uiMsg,WPARAM
w_Param,LPARAM lParam);
class CcomApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CcomWindow:public CFrameWnd
{
CMenu *menu;
CStatic *cft;
CStatic *cct;
public:
CcomWindow();
void OpenFile();
void SetColor();
void SetFont();
void QuitApp();
~CcomWindow();
DECLARE_MESSAGE_MAP()
};
CcomApp comApp;
BEGIN_MESSAGE_MAP(CcomWindow,CFrameWnd)
ON_COMMAND(IDD_OPEN_FILE,OpenFile)
ON_COMMAND(IDD_SET_COLOR,SetColor)
ON_COMMAND(IDD_SET_FONT,SetFont)
ON_COMMAND(IDD_QUIT_APP,QuitApp)
END_MESSAGE_MAP()
CcomWindow::CcomWindow()
{
Create(NULL,"CCommanDialog
Demo",WS_OVERLAPPEDWINDOW,CRect(50,50,550,350));
menu=new CMenu();
menu->LoadMenu(IDR_MENU1);
SetMenu(menu);
cft=new CStatic;

23
cct=new CStatic();
cft->Create("Font Dispaly:",WS_VISIBLE|
SS_SIMPLE,CRect(20,100,120,120),this,IDC_FONT_STATIC);
cct->Create("Color Dispaly:",WS_VISIBLE|
SS_SIMPLE,CRect(20,130,120,150),this,IDC_COLOR_STATIC);
}
CcomWindow::~CcomWindow()
{
delete cft;
delete cct;
delete menu;
}
BOOL CcomApp::InitInstance()
{
m_pMainWnd=new CcomWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return(TRUE);
}
void CcomWindow::OpenFile()
{
CString file_name;
char cmd[72];
static char BASED_CODE szFilter[]="TextFiles(*.txt)|*.txt|C+
+Files(*.cpp)*.cpp||";
CFileDialog dlg(TRUE,"txt",NULL,OFN_OVERWRITEPROMPT|
OFN_CREATEPROMPT,szFilter,NULL);
strcpy(dlg.m_ofn.lpstrFile,"file1");
if(dlg.DoModal()==IDOK)
{
file_name=dlg.GetPathName();
sprintf(cmd,"notepad%s",(LPCTSTR)file_name);
WinExec(cmd,SW_NORMAL);
}
}
void CcomWindow::SetColor()
{
COLORREF clr;
CColorDialog dlg(RGB(150,200,200),CC_FULLOPEN|CC_SHOWHELP|
CC_ENABLEHOOK,this);
dlg.m_cc.lpfnHook=&hook_proc;
if(dlg.DoModal()==IDOK)
{
clr=dlg.GetColor();
CClientDC dc(this);
dc.SetTextColor(clr);

24
dc.ExtTextOut(130,130,0,CRect(130,100,230,120),"Selected Color
Diaplay",22,NULL);
}
}
afx_msg UINT APIENTRY hook_proc(HWND hdlg,UINT uiMsg,WPARAM
w_Param,LPARAM lParam)
{
MessageBeep(MB_OK);
return 0;
}
void CcomWindow::SetFont()
{
LOGFONT *lpf;
CFont fnt;
CFontDialog dlg(NULL,CF_EFFECTS|CF_SCREENFONTS|
CF_APPLY,NULL,this);
if(dlg.DoModal()==IDOK)
{
lpf=dlg.m_cf.lpLogFont;
fnt.CreateFontIndirect(lpf);
CClientDC dc(this);
dc.SelectObject(&fnt);
dc.ExtTextOut(130,100,0,CRect(130,100,230,120),"Selected Font
Diaplay",22,NULL);
}
}
void CcomWindow::QuitApp()
{
DestroyWindow();
}

OUTPUT:

25

FILE OPEN

SET COLOR

SET FONT

26

RESULT:
Thus the dialog based applications was performed.

EX.NO:7

TO CREATE MDI

27

(MULTIPLE DOCUMENT INTERFACE)


AIM:
To create a (MDI) Multiple Document Interface by using VC++ program.
ALGORITHM:
1. Start the program
2. Choose File->New->MFC AppWizard(exe)
3. Choose Multiple Document then click ok.
4. From the Class view ,right click the Mdiview,add the char message handler
and add the virtual function OnUpdate member function
5. Implement the source code.
6. Bulid and execute the program
7. Stop the program

28
PROGRAM:
mdiDoc.cpp :
// mdiDoc.cpp : implementation of the CMdiDoc class
//
#include "stdafx.h"
#include "mdi.h"
#include "mdiDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMdiDoc
IMPLEMENT_DYNCREATE(CMdiDoc, CDocument)
BEGIN_MESSAGE_MAP(CMdiDoc, CDocument)
//{{AFX_MSG_MAP(CMdiDoc)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMdiDoc construction/destruction
CMdiDoc::CMdiDoc()
{
// TODO: add one-time construction code here
number_lines=0;
}
CMdiDoc::~CMdiDoc()
{
}
BOOL CMdiDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMdiDoc serialization
void CMdiDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())

29
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CMdiDoc diagnostics
#ifdef _DEBUG
void CMdiDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMdiDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMdiDoc commands
mdiView.cpp :
// mdiView.cpp : implementation of the CMdiView class
//
#include "stdafx.h"
#include "mdi.h"
#include "mdiDoc.h"
#include "mdiView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMdiView
IMPLEMENT_DYNCREATE(CMdiView, CView)
BEGIN_MESSAGE_MAP(CMdiView, CView)
//{{AFX_MSG_MAP(CMdiView)
ON_WM_CHAR()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)

30
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMdiView construction/destruction
CMdiView::CMdiView()
{
// TODO: add construction code here
}
CMdiView::~CMdiView()
{
}
BOOL CMdiView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMdiView drawing
void CMdiView::OnDraw(CDC* pDC)
{
CMdiDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
for(int loop_index=0;loop_index<=pDoc->number_lines;loop_index++)
{
pDC->TextOut(0,loop_index *tm.tmHeight,pDoc->text[loop_index]);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMdiView printing
BOOL CMdiView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMdiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMdiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

31
/////////////////////////////////////////////////////////////////////////////
// CMdiView diagnostics
#ifdef _DEBUG
void CMdiView::AssertValid() const
{
CView::AssertValid();
}
void CMdiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMdiDoc* CMdiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMdiDoc)));
return (CMdiDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMdiView message handlers
void CMdiView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CMdiDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(nChar!='\r')
{
pDoc->text[pDoc->number_lines]+=nChar;
Invalidate();
}
else
{
pDoc->number_lines++;
}
pDoc->UpdateAllViews(this,(long)pDoc->number_lines,NULL);
pDoc->SetModifiedFlag();
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CMdiView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
CClientDC dc(this);
TEXTMETRIC tm;
CMdiDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
dc.GetTextMetrics(&tm);
dc.TextOut(0,(int)lHint*tm.tmHeight,pDoc->text[lHint]);

32
}
mdiDoc.h :
// mdiDoc.h : interface of the CMdiDoc class
//
/////////////////////////////////////////////////////////////////////////////
#if !
defined(AFX_MDIDOC_H__192C200A_BD00_444E_B65C_15510CAEF056__INCLU
DED_)
#define
AFX_MDIDOC_H__192C200A_BD00_444E_B65C_15510CAEF056__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
const MAX_LINES=1000;
class CMdiDoc : public CDocument
{
protected: // create from serialization only
CMdiDoc();
DECLARE_DYNCREATE(CMdiDoc)
// Attributes
public:
int number_lines;
CString text[MAX_LINES];
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMdiDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
char Onupdate();
virtual ~CMdiDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:

33
//{{AFX_MSG(CMdiDoc)
// 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_MDIDOC_H__192C200A_BD00_444E_B65C_15510CAEF056__INCLU
DED_)

OUTPUT:

RESULT:
Thus the Multiple Document Interface was created.

34

TO CREATE AN USER INTERFACE THREAD

EX: NO: 8
AIM:

To write a VC++ program to create an user interface thread.


ALGORITHM:
1. Select File->New->MFC Appwizard(exe)->type a file name then click next
and select a dialog based application then press ok.
2. Modify the dialog template by removing the static text and creating button
control.
3. Select the button & click right mouse button the menu will be shown
In that menu select properties & set the UIThread
Select the event then choose the Add handler & set member function
then click OK
Use class wizard and implement a message handler for the button
4. Choose class wizard from the view create a new class CUIAPP derived from
CWinthread
5. Add the code to CUIAPP class.
6. Modify the InitInstance function of the CUIAPP class to create and display
the frame window.
7. Modify the message handler for button dialog implementation file.
8. Include #include UIAPP.h file in the dialogs.cpp file.
9. Build and run the program

35

PROGRAM:
UIApp.cpp :
// UIApp.cpp : implementation file
//
#include "stdafx.h"
#include "UIthread.h"
#include "UIApp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUIApp
IMPLEMENT_DYNCREATE(CUIApp, CWinThread)
class CWindow:public CFrameWnd
{
CStatic *cs;
public:
CWindow();
~CWindow();
};
CWindow::~CWindow()
{
delete cs;
}
CWindow::CWindow()
{
Create(NULL,"Thread
window",WS_OVERLAPPEDWINDOW,CRect(0,0,300,300),this);
cs=new CStatic();
cs->Create("I am a UI thread",WS_CHILD|WS_VISIBLE|
SS_CENTER,CRect(50,100,200,120),this);
}
CUIApp::CUIApp()
{
}
CUIApp::~CUIApp()
{
}

36

BOOL CUIApp::InitInstance()
{
// TODO: perform and per-thread initialization here
m_pMainWnd=new CWindow();
m_pMainWnd->ShowWindow(SW_SHOWNORMAL);
m_pMainWnd->UpdateWindow();
return TRUE;
}
int CUIApp::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CUIApp, CWinThread)
//{{AFX_MSG_MAP(CUIApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUIApp message handlers

UIthreadDlg.cpp :
// UIthreadDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UIthread.h"
#include "UIthreadDlg.h"
#include"UIApp.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)

37
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()
/////////////////////////////////////////////////////////////////////////////
// CUIthreadDlg dialog
CUIthreadDlg::CUIthreadDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUIthreadDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUIthreadDlg)
// 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 CUIthreadDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUIthreadDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP

38
}
BEGIN_MESSAGE_MAP(CUIthreadDlg, CDialog)
//{{AFX_MSG_MAP(CUIthreadDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnUIThread)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUIthreadDlg message handlers
BOOL CUIthreadDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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 < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->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 CUIthreadDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else

39
{
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 CUIthreadDlg::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 CUIthreadDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUIthreadDlg::OnUIThread()
{
// TODO: Add your control notification handler code here
AfxBeginThread(RUNTIME_CLASS(CUIApp),THREAD_PRIORITY_NORMAL,
0,0);
}

40

OUTPUT:

RESULT:
Thus the user interface thread was created in VC++

41

EX.NO:9

TO DOCUMENT VIEW ARCHITECTURE

AIM:
To create a rectangle by using the document view architecture in VC++.
ALGORITHM:
1. Select File->New->MFC AppWizard (exe) and give the file name and click ok
then click single document then click ok.
2. Click the class view tab, it display the class view in the project workspace
window at the left of the screen.
3. Right click on the CE11Doc class and choose Add member variable from the
shortcut menu that appears.
4. Fill in the Add member Variable dialog box. For Variable Type, enter
CPoint, Variable name, enter m_points[100].,then click ok.
5. Again right click on the CE11Doc class and choose Add member variable.
6. Fill in the Add member Variable dialog box, In Variable type, enter
UINT,Variable Name, enter m_pointIndex, then click ok.
7. Choose Vie->ClassWizard,the dialog box will appear.
8. In the dialog box select the CE11View class name and object ID boxes,then
click WM_LBUTTONDOWN,then click add function and click ok.
9. Enter the source code and save it.
10. Build and execute the program.

42

PROGRAM:

Doc.Cpp
// ex20Doc.cpp : implementation of the CEx20Doc class
//
#include "stdafx.h"
#include "ex20.h"
#include "ex20Doc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx20Doc
IMPLEMENT_DYNCREATE(CEx20Doc, CDocument)
BEGIN_MESSAGE_MAP(CEx20Doc, CDocument)
//{{AFX_MSG_MAP(CEx20Doc)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CEx20Doc construction/destruction
CEx20Doc::CEx20Doc()
{
// TODO: add one-time construction code here
}
CEx20Doc::~CEx20Doc()
{
}
BOOL CEx20Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)

m_pointIndex=0;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CEx20Doc serialization
void CEx20Doc::Serialize(CArchive& ar)
{

43
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CEx20Doc diagnostics
#ifdef _DEBUG
void CEx20Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CEx20Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx20Doc commands

View.cpp
// ex20View.cpp : implementation of the CEx20View class
//
#include "stdafx.h"
#include "ex20.h"
#include "ex20Doc.h"
#include "ex20View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx20View
IMPLEMENT_DYNCREATE(CEx20View, CView)
BEGIN_MESSAGE_MAP(CEx20View, CView)
//{{AFX_MSG_MAP(CEx20View)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands

44
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()
/////////////////////////////////////////////////////////////////////////////
// CEx20View construction/destruction
CEx20View::CEx20View()
{
// TODO: add construction code here
}
CEx20View::~CEx20View()
{
}
BOOL CEx20View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx20View drawing
void CEx20View::OnDraw(CDC* pDC)
{
CEx20Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

UINT pointIndex=pDoc->m_pointIndex;
for(UINT i=0;i<pointIndex;++i)
{
UINT x=pDoc->m_points[i].x;
UINT y=pDoc->m_points[i].y;
pDC->Rectangle(x,y,x+20,y+20);
}
}
/////////////////////////////////////////////////////////////////////////////
// CEx20View printing
BOOL CEx20View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx20View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

45
void CEx20View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx20View diagnostics
#ifdef _DEBUG
void CEx20View::AssertValid() const
{
CView::AssertValid();
}
void CEx20View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEx20Doc* CEx20View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx20Doc)));
return (CEx20Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx20View message handlers
void CEx20View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CEx20Doc *pDoc=GetDocument();
if(pDoc->m_pointIndex==100)
return;
pDoc->m_points[pDoc->m_pointIndex]=point;
pDoc->m_pointIndex++;
pDoc->SetModifiedFlag();
Invalidate();
CView::OnLButtonDown(nFlags, point);
}

46

OUTPUT:

RESULT:
Thus the rectangle was displayed by using document view
architecture in VC++.

47

TO SERIALIZATION IN A NONDOCUMENT/VIEW APPLICATION

EX.NO:10

AIM:
To create a serialization process in a non-document/view application
by using VC++.
ALGORITHM:
1. Start the program.
2. Select the File->New->Win 32 Application and enter the project name and
click ok.
3. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
4. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
5. Enter the source code.
6. Choose insert menu from the menu bar, select and click the resources.
7. Select the resources item Menu and press new button.
8. The menu properties shown. Enter the ID value, those are simple integers
like 101,102, etc., and enter the caption.
9. Save the script1.rc file to the project.
10. Select the file view from the workspace and click at right in the resource files.
11. Choose add files to folder then the resource file (script1.rc) is selected and
click ok.
12. Finally save this file, then Build and Run the program.

48

PROGRAM:
#include<afxwin.h>
#include<afxext.h>
#include"resource.h"
class date:public CObject
{
DECLARE_SERIAL(date)
private:
int mm,dd,yy;
public:
date()
{
dd=0;
mm=0;
yy=0;
}
void setdate(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
void Serialize(CArchive&ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<dd<<mm<<yy;
else
ar>>dd>>mm>>yy;
}
void displaydate(CDC *pdc)
{
if(dd=0&&mm==0&&yy==0)
return;
char str[30];
sprintf(str,"%d%d%d",dd,mm,yy);
pdc->TextOut(10,10,str);
}
};
IMPLEMENT_SERIAL(date,CObject,1)
class myframe:public CFrameWnd
{
private:

49
CFile fp;
date mydate;
public:
myframe()
{
Create(0,"Serialize",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEIN
TRESOURCE(IDR_MENU1));
fp.Open("Test.txt",CFile::modeCreate|
CFile::modeReadWrite);
}
void readdata()
{
CClientDC d(this);
fp.SeekToBegin();
CArchive ar(&fp,CArchive::load);
mydate.Serialize(ar);
mydate.displaydate(&d);
}
void writedata()
{
currentdate();
fp.SeekToBegin();
CArchive ar(&fp,CArchive::store);
mydate.Serialize(ar);
}
void currentdate()
{
CTime t;
int dd,mm,yy;
t=CTime::GetCurrentTime();
dd=t.GetDay();
mm=t.GetMonth();
yy=t.GetYear();
mydate.setdate(dd,mm,yy);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND(301,writedata)
ON_COMMAND(401,readdata)
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:

50
int InitInstance()
{
myframe *b;
b=new myframe;
b->ShowWindow(SW_SHOWMAXIMIZED);
m_pMainWnd=b;
return 1;
}
};
myapp a;
OUTPUT:

51

RESULT:
Thus the serialization process in a non-document/view application was created.
EX.NO:11

TO DISPLAY THE CURRENT DATE & TIME USING


DYNAMIC CONTROLS

AIM:
To write a program to display the current date and time using dynamic
controls.
ALGORITHM:
1. Start the program.
2. Select the File->New->Win 32 Application and enter the project name and
click ok.
3. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
4. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
5. Enter the source code.
6. Finally save this file, then Build and Run the program.

52

PROGRAM:
#include<afxwin.h>
#include<afxext.h>
class myframe:public CFrameWnd
{
private:
CStatusBar s;
unsigned int indicators[3];
public:
myframe()
{
Create(0,"Current Time and Date");
}
int OnCreate(LPCREATESTRUCT I)
{
CFrameWnd::OnCreate(I);
s.Create(this);
indicators[0]=0;
indicators[1]=0;
indicators[2]=0;
s.SetIndicators(indicators,3);
SetTimer(1,100,NULL);
return 0;
}
void OnTimer(int n)
{
CTime t;
int hh,mm,ss,dd,yy;
t=CTime::GetCurrentTime();
hh=t.GetHour()%12;
mm=t.GetMinute();
ss=t.GetSecond();
char str[30];
if(t.GetHour()>=12)
sprintf(str,"Time:%0.2d:%0.2d:%0.2d
PM",hh,mm,ss);
else
sprintf(str,"Time:%0.2d:%0.2d:%0.2d
AM",hh,mm,ss);
s.SetPaneText(1,str);

53
dd=t.GetDay();
mm=t.GetMonth();
yy=t.GetYear();
sprintf(str,"Date:%0.2d:%0.2d:%0.2d",dd,mm,yy);
s.SetPaneText(2,str);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_CREATE()
ON_WM_TIMER()
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *p;
p=new myframe;
p->ShowWindow(1);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:

54
RESULT:
Thus the current date and time were displayed by using dynamic
controls.
EX.NO:12

TO CREATE A MENU

AIM:
To create a menu item using resource in VC++.
ALGORITHM:
1. Start the program.
2. Select the File->New->Win 32 Application and enter the project name and
click ok.
3. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
4. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
5. Enter the source code.
6. Choose insert menu from the menu bar, select and click the resources.
7. Select the resources item Menu and press new button.
8. The menu properties shown. Enter the ID value, those are simple integers
like 101,102, etc., and enter the caption.
9. Save the script1.rc file to the project.
10. Select the file view from the workspace and click at right in the resource files.
11. Choose add files to folder then the resource file (script1.rc) is selected and
click ok.
12. Finally save this file, then Build and Run the program.

55

PROGRAM:
#include<afxwin.h>
#include"resource.h"
class frame1:public CFrameWnd
{
public:
frame1()
{
Create(0,"menu",WS_OVERLAPPED|WS_CAPTION|
WS_SYSMENU|WS_MINIMIZEBOX|WS_HSCROLL|
WS_VSCROLL,CRect(10,15,300,200),0,MAKEINTRESOURCE(IDR_MENU1));
}
};
class MENU1:public CWinApp
{
public:
BOOL InitInstance()
{
frame1 *obj1=new frame1;
obj1->ShowWindow(true);
m_pMainWnd=obj1;
return(true);
}
};
MENU1 win;
OUTPUT:

56

RESULT:
Thus the resource menu was created.

TO CREATE A KEYBOARD ACCELARATOR

EX.NO:13
AIM:

To write a VC++ program to create a keyboard accelerator by using


resource.
ALGORITHM:
1. Select the File->New->Win 32 Application and enter the project name and
click ok.
2. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
3. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
4. Enter the source code.
5. Choose insert menu from the menu bar, select and click the resources.
6. Select the Resources item Accelarator and press new button
7. The Accelarator

properties shown.Enter the ID value,those are simple

integers like 101,102,103,etc.and enter the caption.


8. Save the script1.rc file to the project.
9. Select the file view from the workspace and click at right in the resource files.
10. Choose add files to folder then the resource file (script1.rc) is selected and
click ok.
11. Finally save this file, then Build and Run the program.

57

PROGRAM:
#include<afxwin.h>
#include"resource.h"
class myframe:public CFrameWnd
{
public:
myframe()
{
LoadAccelTable(MAKEINTRESOURCE(IDR_ACCELERATOR1));
Create(0,"Accelerator",WS_OVERLAPPEDWINDOW,rectDefault,0,MAK
EINTRESOURCE(IDR_MENU1));
}
void fun1()
{
MessageBox("Reached here to draw a line","title");
}
void fun2()
{
MessageBox("Reached here to draw a Rectangle","title");
}
void fun3()
{
MessageBox("Reached here to draw a Circle","title");
}
void fun4()
{
MessageBox("Reached here to draw a Triangle","title");
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND(101,fun1)
ON_COMMAND(102,fun2)
ON_COMMAND(103,fun3)
ON_COMMAND(104,fun4)
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *p;
p=new myframe;

58
p->ShowWindow(3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:

59

RESULT:
Thus the keyboard accelerator resources was created and verified.

60
EX.NO:14

TO CREATE A TOOLTIP CONTROL

AIM:
To create a Tool tip control using VC++.
ALGORITHM:
1. Start the program.
2. Select the File->New->Win 32 Application and enter the project name and
click ok.
3. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
4. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
5. Enter the source code.
6. Finally save this file, then Build and Run the program.

61
PROGRAM:
#include<afxwin.h>
#include<afxcmn.h>
class mytooltipctrl:public CToolTipCtrl
{
public:
int addtool(CWnd *pwnd,LPCTSTR text)
{
TOOLINFO ti;
ti.cbSize=sizeof(TOOLINFO);
ti.lpszText=(LPTSTR)text;
ti.hinst=AfxGetInstanceHandle();
ti.hwnd=pwnd->GetParent()->GetSafeHwnd();
ti.uFlags=TTF_SUBCLASS|TTF_IDISHWND;
ti.uId=(UINT)pwnd->GetSafeHwnd();
return(int)SendMessage(TTM_ADDTOOL,0,(LPARAM)&ti);
}
};
class myframe:public CFrameWnd
{
private:
CSliderCtrl sli;
CButton b;
mytooltipctrl tip;
public:
myframe()
{
CString mywindowclass;
mywindowclass=AfxRegisterWndClass(CS_HREDRAW|
CS_VREDRAW,0,(HBRUSH)::GetStockObject(LTGRAY_BRUSH),0);
Create(mywindowclass,"tooltip Control");
}
int OnCreate(LPCREATESTRUCT I)
{
CFrameWnd::OnCreate(I);
sli.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ|
TBS_AUTOTICKS|TBS_BOTTOM|
TBS_ENABLESELRANGE,CRect(35,50,305,90),this,1);
sli.SetRange(0,8);
b.Create("Button",WS_CHILD|WS_VISIBLE|
BS_PUSHBUTTON,CRect(50,100,150,130),this,2);
tip.Create(this);
tip.addtool(&sli,"This is a Slider Control");
tip.addtool(&b,"This is the Button Control");
return 0;

62
}
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;
p->ShowWindow(3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:

RESULT:
Thus the tool tip control was created.
EX.NO:15

TO CREATE A TOOLBAR CONTROL

63
AIM:
To write a VC++ program to create a toolbar.
ALGORITHM:
1. Select the File->New->Win 32 Application and enter the project name and
click ok.
2. Choose the File->New->C++ Source File and enter the file name with .cpp
extension and click OK.
3. Select the project menu and set the project setting MFC in shared DLL
and click Ok.
4. Enter the source code.
5. Choose insert menu from the menu bar, select and click the resources.
6. Select the Resources item Toolbar and press new button
7. The new screen will be appear, in that screen to create a four buttons
8. Again select the resource from the insert menu then choose the bitmap menu
resource. Then draw the bitmaps.
9. Save the toolbar controls and bitmaps in script1.rc file.
10. Select the file view from the workspace and click at right in the resource files.
11. Choose add files to folder then the resource file (script1.rc) is selected and
click ok.
12. Finally save this file, then Build and Run the program.

PROGRAM:

64
#include<afxwin.h>
#include<afxext.h>
#include"resource.h"
class myframe:public CFrameWnd
{
private:
CToolBar t;
public:
myframe()
{
Create(0,"Create a Toolbar");
}
int OnCreate(LPCREATESTRUCT I)
{
unsigned int buttonarray[4];
t.Create(this,WS_CHILD|WS_VISIBLE|CBRS_TOP|
CBRS_BORDER_LEFT|CBRS_BORDER_RIGHT|CBRS_BORDER_TOP|
CBRS_BORDER_BOTTOM);
int i;
for(i=0;i<=3;i++)
buttonarray[i]=101+i;
t.LoadBitmap(IDB_BITMAP1);
t.SetButtons(buttonarray,4);
return 0;
}
void drawline()
{
MessageBox("line","drawline");
}
void drawrectangle()
{
MessageBox("Recatangle","drawrectangle");
}
void drawtriangle()
{
MessageBox("Triangle","drawtriangle");
}
void drawcircle()
{
MessageBox("Circle","drawcircle");
}
DECLARE_MESSAGE_MAP()
};

65
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(101,drawline)
ON_COMMAND(102,drawrectangle)
ON_COMMAND(103,drawtriangle)
ON_COMMAND(104,drawcircle)
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *p;
p=new myframe;
p->ShowWindow(3);
m_pMainWnd=p;
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:

66

RESULT:
Thus the toolbar control was created in VC++.

67
EX: NO: 16

DLL (DYNAMIC LINK LIBRARY) CREATION

AIM:
To write a program to create a dynamic link library
Mydll
ALGORITHM:
1. Select->File->New->MFC AppWizard (dll) ->Type a project name (mydll),
then click ok.
2. Accept the default settings.
3. Choose classview from the workspace.
4. In that Classview select new class by right clicking the class and create new
class myclass with the option generic class.
5. Again right click on the myclass, then choose Add member function and type
Variable type as int and variable name as interest.
6. Write the coding for the function and also write expected function in myclass
header.
7. Build the dll program.
TEST DLL
ALGORITHM:
1. Select File->New->MFCApplicationWizard(exe)->Type a project name.then click
Ok
2. Select dialog based application option.then click Ok.
3. Modify the dialog template by memorying the static text and create static,edit
buttons to calculate interest by using all function.
4.

Use

ClassWizard

to

add

member

variables

for

edit

and

create

m_price,m_year,m_rate
And click Ok.
5. Write coding on OnOk Button to access all the functions by creating the object of
myclass

68
6. Include .myd\myclass.h in TestDll.h and also declare the object of dll class
7. Choose the object Project->Settings->Linktab->type .\mydll\Debug\mydll.lib
in the object library modules.
8. Copy the mydll.dll file in mydll Debug and paste it in that test dll Debug by
following way, choose My Computer->c:\->mydll->debug->mydll.dll
9. Build the program
PROGRAM:
myclass.cpp:
// myclass.cpp: implementation of the myclass class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "mydll.h"
#include "myclass.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
myclass::myclass()
{
}
myclass::~myclass()
{
}
int myclass::interest(int p,int n,int r)
{
int interest;
interest=p*n*r/100;
return interest;
}
myclass.h:
// myclass.h: interface for the myclass class.
//
//////////////////////////////////////////////////////////////////////

69
#if !
defined(AFX_MYCLASS_H__8F7793EE_0083_4A9B_A6E6_79E27D6E265B__INCL
UDED_)
#define
AFX_MYCLASS_H__8F7793EE_0083_4A9B_A6E6_79E27D6E265B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class myclass
{
public:
_declspec(dllexport)int interest(int p,int n,int r);
_declspec(dllexport)myclass();
_declspec(dllexport)virtual ~myclass();
};
#endif // !
defined(AFX_MYCLASS_H__8F7793EE_0083_4A9B_A6E6_79E27D6E265B__INCL
UDED_)

testdDlg.cpp
// testdDlg.cpp : implementation file
//
#include"..\mydll\Debug\mydll.lib"
#include "stdafx.h"
#include "testd.h"
#include "testdDlg.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

70
//{{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()
/////////////////////////////////////////////////////////////////////////////
// CTestdDlg dialog
CTestdDlg::CTestdDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestdDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestdDlg)
m_prin = 0;
m_year = 0;
m_rate = 0;
m_interest = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestdDlg)
DDX_Text(pDX, IDC_EDIT1, m_prin);

71
DDX_Text(pDX, IDC_EDIT2, m_year);
DDX_Text(pDX, IDC_EDIT3, m_rate);
DDX_Text(pDX, IDC_EDIT4, m_interest);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestdDlg, CDialog)
//{{AFX_MSG_MAP(CTestdDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestdDlg message handlers
BOOL CTestdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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 < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->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 CTestdDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();

72
}
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 CTestdDlg::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 CTestdDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestdDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(true);
myclass ob;
m_interest=ob.interest(m_prin,m_year,m_rate);
UpdateData(false);
//CDialog::OnOK();
}

73

testdDlg.h :
// testdDlg.h : header file
//
#include"..\mydll\myclass.h"
#if!
defined(AFX_TESTDDLG_H__59FBD31C_5253_450A_891B_56631CEBD9E2__INC
LUDED_)
#define
FX_TESTDDLG_H__59FBD31C_5253_450A_891B_56631CEBD9E2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CTestdDlg dialog
class CTestdDlg : public CDialog
{
// Construction
public:
CTestdDlg(CWnd* pParent = NULL);
// standard constructor
// Dialog Data
//{{AFX_DATA(CTestdDlg)
enum { IDD = IDD_TESTD_DIALOG };
int
m_prin;
int
m_year;
int
m_rate;
int
m_interest;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestdDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;

// DDX/DDV support

// Generated message map functions


//{{AFX_MSG(CTestdDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();

74
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the
previous line.
#endif // !
defined(AFX_TESTDDLG_H__59FBD31C_5253_450A_891B_56631CEBD9E2__INC
LUDED_)
OUTPUT

RESULT:
Thus the DLL was created in VC++.

75

EX: NO: 17

EMPLOYEE DATABASE ACCESS USING ODBC

AIM:
To write a program in VC++ to create and access the employee database.
ALGORITHM:
1. Select Start->Settings->Control Panel->Administrative Tools->Data Source
(ODBC)
2. Add the data source and data source driver and click finish button.
3. Select the datasource by clicking the select button and name your Data
source
4. Select File->New->MFC AppWizard (exe) and then type Project Name.
5. In step2 choose the option labeled Database View with File Support and
also select Data Source to popup the data source selection dialog.
6. In the data source selection dialog select the database you created and click
Dynaset.
7. Modify the dialog template in the dialog editor.
8. Use classwizard to link the edit controls to the record set data members.To
add a data member click on the member variable tab and choose the ID
responding to the edit box for each variable.
9. Add the new popup menus in the IDR_MAINFRAME Edit menu also use
classwizard to map the commands to the specified Cdataemployeeview class
members.
Menu Command

Command ID

Add Record

ID_RECORD_ADD

Delete Record

ID_RECORD_DELETE

Clear Record

ID_RECORD_CLAEAR

Update Record

ID_RECORD_UPDATE

10. Edit the menu command handlers with the suitable coding.
11. Build and Execute the Program.

76

PROGRAM:
empdatabaseView.cpp
// empdatabaseView.cpp : implementation of the CEmpdatabaseView class
//
#include "stdafx.h"
#include "empdatabase.h"
#include "empdatabaseSet.h"
#include "empdatabaseDoc.h"
#include "empdatabaseView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEmpdatabaseView
IMPLEMENT_DYNCREATE(CEmpdatabaseView, CRecordView)
BEGIN_MESSAGE_MAP(CEmpdatabaseView, CRecordView)
//{{AFX_MSG_MAP(CEmpdatabaseView)
ON_COMMAND(ID_RECORD_UPDATE, OnRecordUpdate)
ON_UPDATE_COMMAND_UI(ID_RECORD_UPDATE,
OnUpdateRecordUpdate)
ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
ON_COMMAND(ID_RECORD_CLEAR, OnRecordClear)
ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,
CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEmpdatabaseView construction/destruction
CEmpdatabaseView::CEmpdatabaseView()
: CRecordView(CEmpdatabaseView::IDD)
{
//{{AFX_DATA_INIT(CEmpdatabaseView)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here

77
}
CEmpdatabaseView::~CEmpdatabaseView()
{
}
void CEmpdatabaseView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEmpdatabaseView)
DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_Empid, m_pSet);
DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_Empname, m_pSet);
DDX_FieldText(pDX, IDC_EDIT3, m_pSet->m_exp, m_pSet);
DDX_FieldText(pDX, IDC_EDIT4, m_pSet->m_gsal, m_pSet);
//}}AFX_DATA_MAP
}
BOOL CEmpdatabaseView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CEmpdatabaseView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_empdatabaseSet;
CRecordView::OnInitialUpdate();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CEmpdatabaseView printing
BOOL CEmpdatabaseView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEmpdatabaseView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEmpdatabaseView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEmpdatabaseView diagnostics

78
#ifdef _DEBUG
void CEmpdatabaseView::AssertValid() const
{
CRecordView::AssertValid();
}
void CEmpdatabaseView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CEmpdatabaseDoc* CEmpdatabaseView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEmpdatabaseDoc)));
return (CEmpdatabaseDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEmpdatabaseView database support
CRecordset* CEmpdatabaseView::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// CEmpdatabaseView message handlers
void CEmpdatabaseView::OnRecordUpdate()
{
// TODO: Add your command handler code here
m_pSet->Edit();
UpdateData(TRUE);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void CEmpdatabaseView::OnUpdateRecordUpdate(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(!m_pSet->IsEOF());
}
void CEmpdatabaseView::OnRecordAdd()
{
// TODO: Add your command handler code here
m_pSet->AddNew();
UpdateData(TRUE);
if(m_pSet->CanUpdate())
{

79
m_pSet->Update();
}
if(!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
m_pSet->Requery();
UpdateData(FALSE);
}
void CEmpdatabaseView::OnRecordClear()
{
// TODO: Add your command handler code here
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
}
void CEmpdatabaseView::OnRecordDelete()
{
// TODO: Add your command handler code here
CRecordsetStatus stat;
try
{
m_pSet->Delete();
}
catch(CDBException *e)
{
AfxMessageBox(e->m_strError);
e->Delete();
m_pSet->MoveFirst();
UpdateData(FALSE);
return;
}
m_pSet->GetStatus(stat);
if(stat.m_lCurrentRecord==0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(FALSE);
}

80

OUTPUT:

RESULT:
Thus the employee database was created and it was accessed through ODBC.

81

TO CREATE A CALENDAR CONTROL USING


ACTIVEX CONTROL

EX: NO: 18

AIM:
To write a VC++ program to create a calendar control using active x control.
ALGORITHM:
1. Choose File->New->Select Single Document and deselect printing and print
preview-> choose ActiveXControls->Click Ok
2. To install the calendar control by choose Project->Add to project->Components
and controls->Registered ActiveXControls->CalendarControl 8.0->Calendar and
Insert the control.then click close.
3. Edit the calendar control class to handle help messages. Add the coding in
calendar.cpp and calendar.h
4. Use the dialog editor to create a new dialog resource.
(i) Choose Insert->Resource->Dialog->New. Then right click the dialog .
Choose the property option then change Id,Caption as IDD_ACTIVEXDIALOG,
ActiveXDialog respectively and set the dialogs content help property on the
MoreStyle pages.
(ii) Choose a selectDate, NextWeek button,edit button and insert a calendar control
from the insert activeXcontrol by right clicking the dialog box.
(iii) Assign control Id as like Calendar control IDC_CALENDAR1, SelectDate button
IDC_SELECTDATE,
EditControl

EditControl

IDC_MONTH,

IDC_DAY,

EditControl

EditControl

IDC_YEAR,

IDC_MONTH,

NextWeak

button

IDC_NEXTWEEK.
5. Use classwizard to create the CActiveXDialog Class
(i) Accept the default options and name the class CActiveXDialog
(ii) Click on the Classwizard messagemap and add the message handler function
Object ID
CActiveXDialog

Message
M_INITDIALOG

Member function
OnInitDialog

82
IDC_CALENDAR1

NewMonth(Event)

OnNewMonthCalendar1

IDC_SELECTDATE

BN_CLICKED

OnSelectDate

IDC_NEXTWEEK

BN_CLICKED

OnNextWeek

IDOK

BN_CLICKED

OnOk

6. Use Classwizard to add the data members to the CActiveXDialog class


IDC_CALENDAR1

CCalendar

m_calendar

IDC_DAY

short

m_sDay

IDC_MONTH

short

IDC_YEAR

m_sMonth

short

m_sYear

7. Edit the CActiveXDialog class


(i) Add the m_varValue and m_Backcolor data members
(ii)

Edit

the

code

for

file

handler

functions

OnInitDialog,OnNewMonth,Calendar1,OnSelectDate,OnOk
8. Connect the dialog to the view.use classwizard to map the WM_LBUTTONDOWN
msg,then edit the handler function.
9. Edit the virtual OnDraw function in the fileview.cpp
10. Build and run the program.

83
PROGRAM:
ActivX111View.cpp :
// ActivX111View.cpp : implementation of the CActivX111View class
//
#include "stdafx.h"
#include "ActivX111.h"
#include "ActivX111Doc.h"
#include "ActivX111View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CActivX111View
IMPLEMENT_DYNCREATE(CActivX111View, CView)
BEGIN_MESSAGE_MAP(CActivX111View, CView)
//{{AFX_MSG_MAP(CActivX111View)
// 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
// 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()
/////////////////////////////////////////////////////////////////////////////
// CActivX111View construction/destruction
CActivX111View::CActivX111View()
{
// TODO: add construction code here
}
CActivX111View::~CActivX111View()
{
}
BOOL CActivX111View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CActivX111View drawing
void CActivX111View::OnDraw(CDC* pDC)
{

84
CActivX111Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CActivX111View printing
BOOL CActivX111View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CActivX111View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CActivX111View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CActivX111View diagnostics
ifdef _DEBUG
void CActivX111View::AssertValid() const
{
CView::AssertValid();
}
void CActivX111View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CActivX111Doc* CActivX111View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CActivX111Doc)));
return (CActivX111Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CActivX111View message handlers

85

MainFrm.cpp :
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "ActivX111.h"
#include "MainFrm.h"
#include "calendar.h"
#include "new.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_HELP_SHOW, OnHelpShow)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR,
// status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY |
CBRS_SIZE_DYNAMIC) ||

86
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnHelpShow()
{
CNew dlg;
/*dlg.m_BackColor=RGB(255,251,240);
COleDateTime today=COleDateTime::GetCurrentTime();

87

dlg.m_varValue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0,
0);
COleDateTime date(dlg.m_varValue);*/
dlg.DoModal();
}
New.cpp :
// New.cpp : implementation file
//
#include "stdafx.h"
#include "ActivX111.h"
#include "New.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNew dialog
CNew::CNew(CWnd* pParent /*=NULL*/)
: CDialog(CNew::IDD, pParent)
{
//{{AFX_DATA_INIT(CNew)
m_day = 0;
m_month = 0;
m_year = 0;
//}}AFX_DATA_INIT
}
void CNew::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNew)
DDX_Control(pDX, IDC_CALENDAR1, m_calendar);
DDX_Text(pDX, IDC_EDIT1, m_day);
DDX_Text(pDX, IDC_EDIT2, m_month);
DDX_Text(pDX, IDC_EDIT3, m_year);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNew, CDialog)
//{{AFX_MSG_MAP(CNew)
ON_BN_CLICKED(IDC_BUTTON2, Onnextweek)
ON_BN_CLICKED(IDC_BUTTON1, Onselectday)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////

88
// CNew message handlers
BEGIN_EVENTSINK_MAP(CNew, CDialog)
//{{AFX_EVENTSINK_MAP(CNew)
ON_EVENT(CNew, IDC_CALENDAR1, 3 /* NewMonth */, OnNewMonthCalendar1,
VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
BOOL CNew::OnInitDialog()
{
CDialog::OnInitDialog();
COleDateTime today=COleDateTime::GetCurrentTime();
m_varValue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0
,0);
m_calendar.SetValue(m_varValue);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CNew::OnNewMonthCalendar1()
{
// TODO: Add your control notification handler code here
AfxMessageBox("EVENT: CActivexDialog::OnNewMonthCalendar1");
}
void CNew::Onnextweek()
{
// TODO: Add your control notification handler code here
m_calendar.NextWeek();
}
void CNew::Onselectday()
{
// TODO: Add your control notification handler code here
CDataExchange dx(this,TRUE);
DDX_Text(&dx,IDC_EDIT1,m_day);
DDX_Text(&dx,IDC_EDIT2,m_month);
DDX_Text(&dx,IDC_EDIT3,m_year);
//
m_calendar.(m_day);
m_calendar.SetMonth(m_month);
m_calendar.SetYear(m_year);
m_calendar.GetDay();
}
void CNew::OnOK()
{
// TODO: Add extra validation here

89
CDialog::OnOK();
}

New.h:
//{{AFX_INCLUDES()
#include "calendar.h"
//}}AFX_INCLUDES
#if !
defined(AFX_NEW_H__8C17E991_073F_46A5_87C8_447FE3DCFD0C__INCLUDE
D_)
#define
AFX_NEW_H__8C17E991_073F_46A5_87C8_447FE3DCFD0C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// New.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CNew dialog
class CNew : public CDialog
{
// Construction
public:
CNew(CWnd* pParent = NULL);
COleVariant m_varValue;
unsigned long m_BackColor;
// standard constructor
// Dialog Data
//{{AFX_DATA(CNew)
enum { IDD = IDD_DIALOG1 };
CCalendar
m_calendar;
short m_day;
short m_month;
short m_year;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CNew)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions

90
//{{AFX_MSG(CNew)
virtual BOOL OnInitDialog();
afx_msg void OnNewMonthCalendar1();
afx_msg void Onnextweek();
afx_msg void Onselectday();
virtual 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_NEW_H__8C17E991_073F_46A5_87C8_447FE3DCFD0C__INCLUDE
D_)
OUTPUT:

RESULT:

91
Thus the calendar control was created using active x control

Você também pode gostar