Академический Документы
Профессиональный Документы
Культура Документы
NO: 1
DATE: SIMPLE MESSAGE BOX
AIM:
ALGORITHM:
#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:
ALGORITHM:
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:
ALGORITHM:
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:
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:
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
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
RESULT:
Thus the MDI Application was successfully executed by using VC++.
EX.NO: 6
DATE: THREADS
AIM:
ALGORITHM:
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 4: Add three data members to the CComputeDlg class. Edit the file
ComputeDlg.h
int m_nTimer;
int m_n Count;
enum { nMaxCount = 10000 };
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:
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;
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:
ALGORITHM:
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
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
RESULT:
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
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 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;
}
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::OnDrawSquare()
{
m_bc=FALSE;
m_r+=CPoint(37,37);
InvalidateRect(m_r);
}
void CAcceleratorView::OnDrawPattern()
{
m_bp^=1;
}
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.
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 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 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 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().
CMyClass ope;
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
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:
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.
Step 9: Add and override the OnMove function in the View class
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::OnRecordUpdate()
{
m_pSet->Edit();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
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:
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.
PROGRAM:
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"
RESULT:
Thus the ActiveX Control program was successfully created by using
VC++ program.