Вы находитесь на странице: 1из 66

EX.

NO: 1
DATE: SIMPLE MESSAGE BOX

AIM:

Write a VC++ program by using WIN32 application for creating a Simple


Message Box.

ALGORITHM:

1. Select start Programs Visual studio 6.0visual C++ Developer Studio.


2. Select new from the file menu.
3. To begin, select New from File menu.
4. In the New dialog box, pick the projects tab. Select Win32 Application.
5. In the Location field, select subdirectory.
6. In the project Name field, type the name of the project.
7. The create New workshop button should be checked.
8. The platforms section should indicate Win32. Choose OK.
9. A dialog box labeled Win32 Application – step 1 0f 1 will appear.
10.Indicate that you want to create an empty Project, and press the Finish button.
11.Select New from the File menu again.
12.In the New dialog box, pick the Files tab.
13.Select C++ Source File.
14.The Add to project box should be checked, and name of the project should be
indicated.
15.Type the name of the file name in the File Name field and choose OK.
16.Now start to type the source code in .C extension file.
17.To compile the file, select Build .exe from the Build menu, or press F7.
18.Select Execute .exe from the Build menu or press ctrl+F5 or click Execute
program from the Build tool bar to execute the program.
PROGRAM:

#include<windows.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpszCmdLine,int nShowCmd)
{
MessageBox(NULL,TEXT("Hello"),TEXT("Hai"),0);
return 0;
}

RESULT:
Thus the creating a simple message box program is executed successfully in
Visual C++ and output is verified.
EX.No:2
DATE: SIMPLE WINDOW CREATION

AIM:

Write a VC++ program by using WIN32 application for creating a Simple a


window

ALGORITHM:

1. Select start Programs Visual studio 6.0visual C++ Developer Studio.


2. Select new from the file menu.
3. To begin, select New from File menu.
4. In the New dialog box, pick the projects tab. Select Win32 Application.
5. In the Location field, select subdirectory.
6. In the project Name field, type the name of the project.
7. The create New workshop button should be checked.
8. The platforms section should indicate Win32. Choose OK.
9. A dialog box labeled Win32 Application – step 1 0f 1 will appear.
10.Indicate that you want to create an empty Project, and press the Finish button.
11.Select New from the File menu again.
12.In the New dialog box, pick the Files tab.
13.Select C++ Source File.
14.The Add to project box should be checked, and name of the project should be
indicated.
15.Type the name of the file name in the File Name field and choose OK.
16.Now start to type the source code in .C extension file.
17.To compile the file, select Build .exe from the Build menu, or press F7.
18.Select Execute .exe from the Build menu or press ctrl+F5 or click Execute
program from the Build tool bar to execute the program.

PROGRAM:

#include<windows.h>
LRESULT CALLBACK wndproc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE
hPrevInstance,LPSTR lpszCmdLIne,int iCmdShow)
{
WNDCLASS wndclass;
MSG msg;
HWND hwnd;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=wndproc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hbrBackground=(HBRUSH)
GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName="Sample";
wndclass.lpszMenuName=NULL;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("WINNT"),TEXT("NT"),0);
return 0;
}
hwnd=CreateWindow("Sample","ASampleSDKWindow",
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 message,WPARAM
wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("Hello"),-1,&rect, DT_SINGLELINE |DT_CENTER |
DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd,message,wParam,lParam);
}

RESULT:

Thus the creating a simple message box and simple window program is
executed successfully in Visual C++ and output is verified
EX.NO: 3
DATE: KEYBOARD AND MOUSE EVENTS

AIM:

Write a SDI program for creating a window and work on keyboard and mouse
events.

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

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

RESULT:

Thus the keyboard and mouse events program is executed successfully in Visual
C++ and output is verified.
EX.NO: 4
DATE: DIALOG BASED APPLICATIONS

AIM:

Write a VC++ program to perform Dialog Based Application.

ALGORITHM:

1. Select Start-> Programs -> Visual studio 6.0->visual C++ Developer.


2. Select New from the File menu.
3. To begin, select New from File menu.
4. In the New dialog box, pick the projects tab. Select Dialog based Application.
5. Design the new Dialog Box.
6. Place the controls
7. Set the properties for the controls using View=> properties
8. Set the member variables using Class Wizard
9. Write code for compute button.
10.Build and test the application.

CREATING A DIALOG BASED APPLICATION


Step1: Open the Visual C++ IDE
Step2: Select the standard Menu and choose File=> New .The new dialog box appears

Step3: The New Dialog Box appears


Step4: Then the First Appwizard window appears

Step5: The second Appwizard window appears


Step 6: Then the Third Appwizard window appears

Step7: Then the Fourth Appwizard window appears


Step 8: Then the Fifth Appwizard window appears

Step 9: Now create a sample Dialog Based Application

Step 10: Design the new Dialog Box


 Place the controls
 Set the properties foe the controls using View=>properties
 Set the member variables using class wizard
 Write code for compute button.

Step 11: Use the dialog editor properties to assign ID to the controls
Control ID
First edit control IDC_FIRST
Second Edit Control IDC_SECOND
Third Edit Control IDC_RESULT
First radio button(set group property) IDC_OPERATION
Compute Button IDC_COMPUTE
Step 12: Use ClassWizard to add member variables and a command handler.
Control ID Member Variable Long
IDC_FIRST m_dFirst double
IDC_SECOND m_dSecond double
IDC_RESULT m_dResult double
IDC_OPERATION m_nOperation int

Step 13: Add the message handler OnCompute for the IDC_COMPUTE button: Use
the Classwizard to add the message handler for the IDC_COMPUTE and accept the
default name OnCompute().
PROGRAM:

Code the OnCompute member function in the DialogBasedDlg.cpp

void CDialogbasedDlg::OnCompute()
{
UpdateData(TRUE);
switch(m_nOperation)
{
case 0:
m_dResult=m_dFirst+m_dSecond;
break;
case 1:
m_dResult=m_dFirst-m_dSecond;
break;
case 2:
m_dResult=m_dFirst*m_dSecond;
break;
case 3:
if(m_dSecond!=0.0)
{
m_dResult=m_dFirst/m_dSecond;
}
else
{
AfxMessageBox("Not Divide by zero");
m_dResult=0.0;
}
}
UpdateData(FALSE);
}

RESULT:
Thus the Dialog Based Application was successfully executed by using Visual
C++.
EX.NO: 5
DATE: MDI APPLICATIONS

AIM:

Write a VC++ program to create a MDI application.

ALGORITHM:
1. Select Start-> Programs-> Visual studio 6.0 -> Visual C++
Developer Studio
2. Select New from the File menu
3. To begin, select New from the File menu
4. In the New dialog box, pick the Projects tab. Select MFC App
wizard application
5. Now the App Wizard will create a new MDI project and then click
OK
6. Now the MDI project will be created with its specific classes
7. Write the code CChildView::OnPaint function in the
ChildView.cpp source code file
8. Build and test the application

CREATING MDI APPLICATION:


Step 1: Using the AppWizard to create MDI application, select MFC AppWizard (exe)

Step 2: Select Multiple Documents and click next


Step 3: Select the database support to be done in the Step 2 of AppWizard and click
the next

Step 4: The step 3 of AppWizard appears and clicks next


Step 5: The step 4 of AppWizard will appear and select the options and click next

Step 6: The step 5 of AppWizard appears and selects the following options
Step 7: The step 6 of AppWizard appear that specifies the various classes and click
the finish button

Step 8: Now the AppWizard will create a new MDI project and click OK
Step 9: Now the MDI project will be created with its specific classes
PROGRAM:

View.cpp

void CSimplemdiView::OnDraw(CDC* pDC)


{
pDC->Rectangle(10,10,100,100);
pDC->Ellipse(10,10,100,100);
pDC->TextOut(100,100,"RVSCET",6);
}

RESULT:
Thus the MDI Application was successfully executed by using VC++.
EX.NO: 6
DATE: THREADS

AIM:

To write a VC++ program to implement thread operations.

ALGORITHM:

1. Select Start ProgramsVisual studio 6.0 Visual C++


Developer Studio
2. Select New from the File menu
3. To begin, select New from the File menu
4. Run the application wizard and accept all the default settings of the
Dialog box appears
5. Select the single document and deselect the print preview and accept
All other options and finishes the application
6. Use the dialog editor to create the dialog resource IDD_COMPUTE
7. Use ClassWizard to create the CComputeDlg class
8. Add three data members to the CComputeDlg class
9. Edit the file ComputeDlg.h
10. Add initialization code to the CComputeDlg constructor in the
ComputeDlg.cpp file
11. Code the OnStart function in ComputeDlg.cpp
12. Code the OnTimer function in ComputeDlg.cpp
13. Code the OnCancel function in ComputeDlg.cpp
14. Edit the ViewClass in View.cpp
15. Build and test the application
CREATING A THREAD APPLICATION USING VC++:

Step 1: Run VC++ AppWizard to create an SDI application and deselect printing
And print preview by accepting all the default setting and click finish to design
the project.

Step 2: Use the dialog editor to create the dialog resource


Step 3: Use ClassWizard to create the CComputeDlg class

Step 4: Add three data members to the CComputeDlg class. Edit the file
ComputeDlg.h
int m_nTimer;
int m_n Count;
enum { nMaxCount = 10000 };

Step 5: Add initialization code to the CComputeDlg constructor in the


ComputeDlg.cpp file
m_nCount = 0;
Step 6: Code the OnStart function in ComputeDlg.cpp
void CComputeDlg: :OnStart( )
{
MSG message;
m_nTimer = SetTimer(1, 100, NULL); // 1/10 second
ASSERT(m_nTimer != 0);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
volatile int nTemp;
for (m_nCount = 0; m_nCount < nMaxCount; m_nCount++)
{
for (nTemp = 0; nTemp < 10000; nTemp++)
{ // uses up CPU cycles
}
if (: :PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
: :TranslateMessage(&message);
: :DispatchMessage(&message);
}
}
CDialog: :OnOK( );
}
Step 7: Code the OnTimer function in ComputeDlg.cpp
void CComputeDlg: :OnTimer(UINT nIDEvent)
{
CProgressCtrl* pBar = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS1);
pBar->SetPos(m_nCount * 100 / nMaxCount);
}
Step 8: Code the OnCancel function in ComputeDlg.cpp
void CComputeDlg: :OnCancel()
{
TRACE("entering CComputeDlg::OnCancel\n");
if (m_nCount == 0) { // prior to Start button
CDialog::OnCancel();
}
else { // computation in progress
m_nCount = nMaxCount; // Force exit from OnStart
}
}
Step 9: Edit the ViewClass in View.cpp
void CThreadView: :OnDraw(CDC* pDC)
{
pDC->TextOut(0, 0, "Press the left mouse button here.");
}
Then use ClassWizard to add the OnLButtonDown function to handle
WM_LBUTTONDOWN messages, and add the following code:
void CThreadView: :OnLButtonDown(UINT nFlags, CPoint point)
{
CComputeDlg dlg;
dlg.DoModal( );
}
Add the following #include statement:
#include "ComputeDlg.h"
Step10: Build and test the application

RESULT:
Thus the implementation of thread operations was successfully created by using
VC++ program
EX.NO: 7
DATE: DOCUMENT VIEW ARCHITECTURE,
SERIALIZATION
AIM:

To write VC++ program to store and retrieve the file from the Document by using
the serialization.

ALGORITHM:

1. Select StartProgramsVisual Studio 6.0Visual C++ Developer Studio.


2. Select New from the File Menu.
3. To begin, select New from the file menu.
4. In the New dialog box, pick the projects tab. Select MFC App wizard
Application.
5. In the location field, select a subdirectory.
6. In the project name field, type the name of the projects.
7. Create New Workspace button should be checked.
8. Click finish button then perform the serialization application.
STEPS FOR CREATING:

Step1: Run AppWizard Exe to produce a SDI application and select Default option
then click finish button.
.
Step2: Select the Document Class and choose the SerialDoc.h then declare the
string data member.

public:
CString strdata;

Step3: Goto the SerialDoc.cpp then initialize the data member.


CSerialDoc::CSerialDoc()
{
strdata="";
}

Step4: Edit the Serialize() function in SerialDoc.cpp.


void CSerialDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar<<strdata;
}
else
{
ar>>strdata;
}
}

Step5: Use classwizard to connect the WM _CHAR message to SerialView.cpp class


Step 6: Edit the OnDraw() and OnChar() message handler in SerialView.cpp file.

void CSerialView::OnDraw(CDC* pDC)


{
CSerialDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(0,0,pDoc->strdata);
}

void CSerialView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)


{
CSerialDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->strdata+=nChar;
Invalidate();
pDoc->SetModifiedFlag();
CView::OnChar(nChar, nRepCnt, nFlags);
}
Step 7: Build and Run the application.

RESULT:

Thus the Serialization application was successfully executed for saving and
Retrieve the file from the Document by using VC++ program.
EX.NO: 8
DATE: DYNAMIC CONTROLS (COM)

AIM:

To write a VC++ program to generate dynamic controls

ALGORITHM:

1. Select StartProgramsVisual Studio 6.0Visual C++ Developer Studio


2. In the new dialog box, pick the project tab. Select MFC AppWizard application
3. Select the ATL COM AppWizard and accept all the default settings
4. Click the Class file and right click and select New ATL object
5. Click Simple object in the ATL object wizard and click next.
6. Then fill the ATL object wizard names and attributes and click OK
7. Right click the project class workspace to add a method with name and attribute
8. Then right click the method and write the code for the function
9. Save the project and compile the code without executing and COM is created
successfully
10.Test COM using VB and open the Visual basic 6.0 and create a new standard
exe project
11.Then create a new form and design a button and click Project menu and select
References
12.Now click, browse and select a COM project and click open
13.Open the COM project and select library file and click open
14.Select and activate the COM type library and click OK
15.Write the code for the command button
16.Run the project and click the Add button for the result
Step 1: Choose the file menu and click the new

Step 2: Select the ATL COM App Wizard and accept all the default settings and
Click DLL and click finish button.
Step 3: Then click the class file and right click and select NEW ATL object.

Step 4: Then click simple object in the ATL Object Wizard and click next
Step 5: Then fill the ATL object Wizard names and click ok

Step 6: Then Right click the project class workspace to add a method.
Step 7: Then fill the method name and attributes and click OK

Step 8: Then right click the method and write the code for the function
Step 9: Save the project and compile the code without executing and COM is created
successfully

Step 10: Then create a new form and design a button using VB. Click the Project menu
and select References

Step11: Then Create a New form and design buttons.


Step12: Then Click project Menu and Select References

Step 13: Now click, browse and select the COM project and Click open
Step 14: Now select and activate the COM type library and Click OK

Step 15: Write the code for the command buttons


Step 16: Run the project and enter the values in text box and click the Buttons for the
result

RESULT:

Thus the Dynamic Control created successfully by using VC++ program.


EX.NO: 9
DATE: MENU, ACCELERATOR, TOOL TIP, TOOLBAR

AIM:

To write a VC++ program to develop menu, accelerator, tool tip and tool bar

ALGORITHM:

1. Select Start Programs Visual Studio 6.0 Visual C++ Developer Studio
2. In the new dialog box, pick the project tab. Select MFC AppWizard application
3. Create an SDI application and deselect printing and print preview by accepting
all the default settings and click finish to design the project.
4. Use the resource editor to edit the application’s main menu
5. Click on the ResourceView tab in the workspace window and edit the
IDR_MAINFRAME menu resource to add a separator
6. Use the resource editor to update the application’s tool bar and edit the
IDR_MAINFRAME toolbar resource to create a bitmap
7. Use class wizard to add a View class command and Update Command UI
message handlers
8. Add data member to the view class and edit the file View.h
9. Edit the View.cpp file
10.Build and test the application

STEPS FOR CREATING TOOL TIP AND TOOL BAR

Step 1: Run VC++ AppWizard to create an SDI application and deselect printing and
print preview by accepting all the default settings and click finish to design the
project.
Step 2: Use the resource editor to edit the application’s main menu
Step 3: Click on the ResourceView tab in the workspace window and edit the
IDR_MAINFRAME menu resource to add a separator
Use the following command IDs for your new menu items
Menu Caption Command ID
Draw Circle\nF2 ID_DRAW_CIRCLE
Draw Square\nF3 ID_DRAW_SQUARE
Draw Pattern ID_DRAW_PATTERN

Step 4: Use the resource editor to add Keyboard Accelerator


Use the following insert keys to add the items
Accelerator ID Key
ID_DRAW_CIRCLE VK_F2
ID_DRAW_SQUARE VK_F3

Step 5: Use the resource editor to update the application’s tool bar tool tip and edit the
IDR_MAINFRAME toolbar resource to create a bitmap

Step 6: Use class wizard to add a View class command and Update Command UI
message handlers, and accept the default function names shown in the following table:
Object ID Message Member Function
ID_DRAW_CIRCLE COMMAND OnDrawCircle
ID_DRAW_CIRCLE UPDATE_COMMAND _UI OnUpdateDrawCircle
ID_DRAW_PATTERN COMMAND OnDrawPattern
ID_DRAW_PATTERN UPDATE_COMMAND _UI OnUpdateDrawPattern
ID_DRAW_SQUARE COMMAND OnDrawSquare
ID_DRAW_SQUARE UPDATE_COMMAND _UI OnUpdateDrawSquare
Step 7: Add data member to the view class and edit the file acceleratorView.h
public:
CRect m_r;
BOOL m_bc;
BOOL m_bp;

Step 8: Edit the acceleratorView.cpp file then write the corresponding coding
CAcceleratorView::CAcceleratorView():m_r(0,0,100,100)
{
m_bc=TRUE;
m_bp=FALSE;
}

void CAcceleratorView::OnDraw(CDC* pDC)


{
CBrush brush(HS_BDIAGONAL,0L);
if(m_bp)
{
pDC->SelectObject(&brush);
}
else
{
pDC->SelectStockObject(WHITE_BRUSH);
}

if(m_bc)
{
pDC->Ellipse(m_r);
}
else
{
pDC->Rectangle(m_r);
}
pDC->SelectStockObject(WHITE_BRUSH);

}
void CAcceleratorView::OnDrawCircle()
{
m_bc=TRUE;
m_r+=CPoint(37,37);
InvalidateRect(m_r);
}

void CAcceleratorView::OnUpdateDrawCircle(CCmdUI* pCmdUI)


{
pCmdUI->Enable(!m_bc);
}

void CAcceleratorView::OnDrawSquare()
{
m_bc=FALSE;
m_r+=CPoint(37,37);
InvalidateRect(m_r);
}

void CAcceleratorView::OnUpdateDrawSquare(CCmdUI* pCmdUI)


{
pCmdUI->Enable(m_bc);
}

void CAcceleratorView::OnDrawPattern()
{
m_bp^=1;
}

void CAcceleratorView::OnUpdateDrawPattern(CCmdUI* pCmdUI)


{
pCmdUI->SetCheck(m_bp);
}
Step 9: Build and test the application

RESULT:
Thus the Menu, Keyboard, Tool tips and Tool Bar was successfully created
by using VC++ program.
EX.NO: 10
DATE: CREATE AN MFC EXTENSION DLL

AIM:
To write a VC++ program to build an MFC extension DLL for performing
addition, subtraction, multiplication and division operations.

STEPS FOR CREATING SERVER DLL:

Step 1: Run VC++ AppWizard and choose new from Visual C++ file menu, and then
click on the projects tab as usual. Instead of selecting MFC AppWizard (exe),
choose MFC AppWizard (dll), as shown below,

Step 2: Then Choose regular DLL using Shared MFC DLL in the AppWizard screen
Step 3: Goto Class View and right click the DLL class and Choose new class

Step 4: Now choose the class type to the generic class and type the class name as
CMyClass
Step 5: Then add member function to the CMyClass by right clicking the CMyClass
and type the function type and function name.

Step 6: Add the function definition in MyClass.cpp


long CMyClass::add(long a, long b)
{
return a+b;
}

long CMyClass::sub(long a, long b)


{
return a - b;
}

long CMyClass::mul(long a, long b)


{
return a * b;
}

long CMyClass::div(long a, long b)


{
return a / b;
}

Step 7: To call the DLL function from an external application we have to prefix the
function with the signature in MyClass.h “declspec(dllexport)

Public:
_declspec(dllexport)long div(long a,long b);
_declspec(dllexport)long mul(long a,long b);
_declspec(dllexport)long sub(long a,long b);
_declspec(dllexport)long add(long a,long b);
_declspec(dllexport)CMyClass();
_declspec(dllexport)virtual ~CMyClass();

Step 8: Build the code without executing

STEPS FOR CREATING CLIENT DLL:

Step 1: Run the VC++ AppWizard (exe) to develop a dialog based application and
click finish to develop the project.

Step 2: The Resource editor will be enabled once finishing the project and add now
design the dialog box
Step 3: Use the dialog editor properties to assign ID to the controls
Control ID
First edit control IDC_FIRST
Second Edit Control IDC_SECOND
Third Edit Control IDC_RESULT
First radio button(set group property) IDC_OPERATION
Compute Button IDC_COMPUTE

Step 4: Use ClassWizard to add member variables and a command handler.


Control ID Member Variable Type
IDC_FIRST m_First long
IDC_SECOND m_Second long
IDC_RESULT m_Result long
IDC_OPERATION m_Operation int

Step 5: Add the message handler OnCompute for the IDC_COMPUTE button: Use the
Classwizard to add the message handler for the IDC_COMPUTE and accept the
default name OnCompute().

Step 6: Create an object for the CMyClass of the MyDll in TESTDlg.cpp

CMyClass ope;

Step 7: Now edit the code on TESTDlg.cpp


void CTESTDlg::OnCompute()
{
UpdateData(true);
switch(m_Operation)
{
case 0:
m_Result=ope.add(m_First,m_Second);
break;
case 1:
m_Result=ope.sub(m_First,m_Second);
break;
case 2:
m_Result=ope.mul(m_First,m_Second);
break;
case 3:
if(m_Second!=0.0)
{
m_Result=ope.div(m_First,m_Second);
}
else
{
AfxMessageBox("Not Divide by zero");
m_Result=0;
}
break;
default:
TRACE("default;m_Operation=%d\n",m_Operation);
}
UpdateData(FALSE);
}

Step 8: Now add the entire path of the MyDll server header file in the TESTDlg.h
#include "E:\MyDll\Class.h"

Step 9: Click the project menu and select the settings and click on the link tab and type
the entire path of the MyDll lin in the object/Library modules text box.
Step 10: Now copy the MyDll.dll in the MyDll debug folder into the TEST folder

Step 11: Build and test the application

RESULT:
Thus the MFC extension DLL for performing addition, subtraction and
multiplication and division operation was created successfully by using VC++
program.
EX.NO: 11
DATE: DATA ACCESS THROUGH ODBC

AIM:
To write a VC ++ program to perform data base access through ODBC

ALGORITHM:

1. Select StartProgramsVisual Studio 6.0Visual C++ Developer Studio.


2. Select New from the File Menu.
3. To begin, select New from the file menu.
4. In the New dialog box, pick the projects tab. Select MFC App wizard
Application.
5. In the location field, select a subdirectory.
6. In the project name field, type the name of the projects.
7. Create New Workspace button should be checked.
8. Copy the student Registration database to your hard disk.
9. Run ODBC Data Source Administrator to install the student Registration data
source
10.Run AppWizard to produce a SDI application and select database with file
support.
11. Select the data source and set the ODBC connection
12. Select the table and click ok. And press finish button. One dialog
Box fill appear.
13. Place the controls and write the program for corresponding buttons.
14. Set the properties using class wizard
15. Build and test the project

STEPS FOR CREATING A DATABASE:

Step 1: Create the database table namely students with fields Roll No, Name,
Department and year using MS Access
Step 2: Run AppWizard to produce a SDI application and select database with file
support

Step 3: Select the data source and set the ODBC connection

Step 4: Select the table and click ok. And press finish button. One dialog box fill
appear.
Step 5: place the controls and write the program for corresponding buttons.

Step 6: Use Class Wizard to link the edit controls to the record set data
Step 7: Add menu commands

Step 8: Class Wizard to map the commands to the specified View class member.

Menu Command Update


Command Command ID Handler Command UI
Handler
Add Record ID_RECORD_ADD OnRecordAdd
Clear Fields ID_RECORD_CLEAR OnRecordClear
OnUpdateRecord
Delete Record ID_RECORD_DELETE OnRecordDelete
Delete
OnUpdateRecord
Update Record ID_RECORD_UPDATE OnRecordUpdate
Update

Step 9: Add and override the OnMove function in the View class

BOOL CODBCView::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:
ASSERT(FALSE);
}
UpdateData(FALSE);
return TRUE;
}

Step 10: Edit the menu command handler and add the following code

void CODBCView::OnRecordAdd()
{
m_pSet->AddNew();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
if (!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
m_pSet->Requery();
UpdateData(FALSE);
}

void CODBCView::OnRecordClear()
{
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
}
void CODBCView::OnRecordDelete()
{
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
e->Delete();
m_pSet->MoveFirst(); // lost our place!
UpdateData(FALSE);
return;
}

m_pSet->GetStatus(status);
if (status.m_lCurrentRecord == 0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(FALSE);
}

void CODBCView::OnUpdateRecordDelete(CCmdUI* pCmdUI)


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

void CODBCView::OnRecordUpdate()
{
m_pSet->Edit();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
}

void CODBCView::OnUpdateRecordUpdate(CCmdUI* pCmdUI)


{
pCmdUI->Enable(!m_pSet->IsEOF());
}
Step 11: Build and test the ODBC application again.

RESULT:
Thus the data base access through ODBC program created successfully by
using VC++ program.
EX.NO: 12
DATE: ACTIVEX CONTROL

AIM:
To write a VC++ program to create an ActiveX control.
ALGORITHM:

1. Select StartProgramsVisual Studio 6.0Visual C++ Developer Studio.


2. Select New from the File Menu.
3. To begin, select New from the file menu.
4. In the New dialog box, pick the projects tab. Select MFC App wizard
Application.
5. In the location field, select a subdirectory.
6. In the project name field, type the name of the projects.
7. Verify that the calendar control is registered.
8. Run Appwizard and accept all of the default settings by selecting Single
Document.
9. Deselect Printing and Print Preview in the Appwizard dialog Box.
10.In the Appwizard step3dialog box, select ActiveX controls option.
11.Install the Calendar control in the project.
12.Edit the calendar control class to handle help messages.
13.Use the dialog editor to create a new dialog resource.
14.Use ClassWizard to create the CActiveXDialog class.
15.Use Classwizard to add data members to the CActiveXDialog class.
16.Edit the CActiveXDialog class.
17.Edit the virtual OnDraw function in the file View.cpp
18.Build and test the application.
STEPS FOR CREATING AN ACTIVE-X CONTROL:

Step 1: Verify that the calendar control is registered.

Step 2: Run AppWizard and accept all of the default settings by selecting
Single Document and deselect Printing and Print Preview.

Step 3: In the AppWizard step3 dialog step3 dialog, make sure the ActiveX
Controls option is selected, as shown below.

Step 4: Install the Calendar Control in the project


Step 5: Create new dialog editor and place the control.

Step 6: Assign control IDs shown in a following table.


Control ID
Calendar Control IDC_CALENDAR1
Select Date Button IDC_SELECTDATE
Edit Control1 IDC_DAY
Edit Control 2 IDC_MONTH
Edit Control3 IDC_YEAR
Next Weak Button IDC_NEXTWEEK
Step 7: Use ClassWizard to create the CActiveXDialog.

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)

Step 8: Use ClassWizard to add data members to the CActiveXDialog.

PROGRAM:

Edit the CActiveXDialog class

ActiveXDialog.h

COleVariant m_varValue;
unsigned long m_BackColor;
ActiveXDialog.cpp
CActiveXDialog::CActiveXDialog(CWnd* pParent /*=NULL*/)
: CDialog(CActiveXDialog::IDD, pParent)
{

m_BackColor=0x8000000F;
}
BOOL CActiveXDialog::OnInitDialog()
{
CDialog::OnInitDialog();
m_calendar.SetValue(m_varValue);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

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

void CActiveXDialog::OnNewMonthCalendar1()
{
AfxMessageBox("EVENT:ActiveXDialog::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();
}

View.cpp

#include "ActiveXDialog.h"

void CActiveView::OnDraw(CDC* pDC)


{
pDC->TextOut(0,0,"Press the left mouse button here");
}
void CActiveView::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
dlg.m_BackColor=RGB(255,251,240);
COleDateTime today=COleDateTime::GetCurrentTime();
dlg.m_varValue=COleDateTime(today.GetYear(),today.GetMonth(),
today.GetDay(),0,0,0);
if(dlg.DoModal()==IDOK)
{
COleDateTime date(dlg.m_varValue);
AfxMessageBox(date.Format("%B %d ,%Y"));
}

RESULT:
Thus the ActiveX Control program was successfully created by using
VC++ program.

Вам также может понравиться