Ex No: / KEY BOARD EVENT
Date:

AIM:

To create a program for display the character and key presses on the keyboard on the client area.

ALGORITHM:

Step 1: To begin, select New from the File menu.

Step 2: In the New dialog box, pick the Projects tab.

Step 3: Select Win32 Application. In the Location field, select a subdirectory.

Step 4: In the Project Name field, type the name of the project as kb

Step 5: This will be a subdirectory of the directory indicated in the Location field.

Step 6: The Create New Workspace button should be checked.

Step 7: The Platform section should indicate Win32. Choose OK.

Step 8: A dialog box labeled Win32 Application- Step 1 of 1 will appear.

Step 9: Indicate that we want to create an Empty Project, and press the Finish button.

Step 10: Select New from the File menu again. In the New dialog box, pick the Files tab.

Step 11:Select c++ Source File. The Add To Project box should be checked, and kb should be indicated.

Step 12: Type kb.c in the File Name field. Choose OK.

Step 13: Now we can type in the kb.c file as shown below.

CODING:

#include<windows.h>

#define BUFFER(x,y)*(pBuffer+y*cxBuffer+x)

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

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int hShowCmd)

{

HWND hwnd;

MSG msg;

WNDCLASS s;

s.style=CS_HREDRAW/CS_VREDRAW;

s.lpfnWndProc=WndProc;

s.cbClsExtra=0;

s.cbWndExtra=0;

s.hInstance=hInstance;

s.hIcon=NULL;

s.hCursor=NULL;

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

s.lpszMenuName=NULL;

s.lpszClassName="mps";

RegisterClass(&s);

hwnd=CreateWindow("mps","this is the simple window",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,NULL,

hInstance,

NULL);

ShowWindow(hwnd,hShowCmd);

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)

{

static DWORD dwCharSet=DEFAULT_CHARSET;

static int cxChar,cyChar,cxClient,cyClient,cxBuffer,cyBuffer,xCaret,yCaret;

static char *pBuffer=NULL;

int i,x,y;

HDC hdc;

PAINTSTRUCT PS;

TEXTMETRIC tm;

switch(msg)

{

case WM_INPUTLANGCHANGE:

dwCharSet=wParam;

case WM_CREATE:

hdc=GetDC(hWnd);

SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,dwCharSet,0,0,0,FIXED_PITCH,NULL));

GetTextMetrics(hdc,&tm);

cxChar=tm.tmAveCharWidth;

cyChar=tm.tmHeight;

DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT)));

ReleaseDC(hWnd,hdc);

case WM_SIZE:

if(msg==WM_SIZE)

{

cxClient=LOWORD(lParam);

cyClient=HIWORD(lParam);

}

cxBuffer=max(1,cxClient/cxChar);

cyBuffer=max(1,cyClient/cyChar);

if(pBuffer!=NULL)

free(pBuffer);

pBuffer=(TCHAR*)malloc(cxBuffer*cyBuffer*sizeof(TCHAR));

for(y=0;y<cyBuffer;y++)

for(x=0;x<cxBuffer;x++)

BUFFER(x,y)=' ';

xCaret=0;

yCaret=0;

if(hWnd=GetFocus())

SetCaretPos(xCaret*cxChar,yCaret*cyChar);

InvalidateRect(hWnd,NULL,TRUE);

return 0;

case WM_SETFOCUS:

CreateCaret(hWnd,NULL,cxChar,cyChar);

SetCaretPos(xCaret*cxChar,yCaret*cyChar);

ShowCaret(hWnd);

return 0;

case WM_KILLFOCUS:

HideCaret(hWnd);

DestroyCaret();

return 0;

case WM_KEYDOWN:

switch(wParam)

{

case VK_HOME:

xCaret=0;

break;

case VK_END:

xCaret=cxBuffer-1;

break;

case VK_PRIOR:

xCaret=cxBuffer-1;

break;

case VK_NEXT:

yCaret=0;

break;

case VK_LEFT:

xCaret=max(xCaret-1,0);

break;

case VK_RIGHT:

xCaret=min(xCaret+1,cxBuffer-1);

break;

case VK_DOWN:

yCaret=min(yCaret+1,cyBuffer-1);

break;

case VK_DELETE:

for(x=xCaret;x<cxBuffer-1;x++)

BUFFER(cxBuffer-1,yCaret)=' ';

HideCaret(hWnd);

hdc=GetDC(hWnd);

SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,dwCharSet,0,0,0,FIXED_PITCH,NULL));

TextOut(hdc,xCaret*cxChar,yCaret*cyChar,&BUFFER(xCaret,yCaret),cxBuffer-xCaret);

DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT)));

ReleaseDC(hWnd,hdc);

ShowCaret(hWnd);

break;

}

SetCaretPos(xCaret*cxChar,yCaret*cyChar);

return 0;

case WM_CHAR:

for(i=0;i<(int)LOWORD(lParam);i++)

{

switch(wParam)

{

case '\b':

if(xCaret>0)

{

xCaret--;

SendMessage(hWnd,WM_KEYDOWN, VK_DELETE,1);

}

break;

case '\t':

do

{

SendMessage(hWnd,WM_CHAR,' ',1);

}

while(xCaret%8!=0);

break;

case '\n':

if(++yCaret==cyBuffer)

yCaret=0;

break;

case '\v':

xCaret=0;

if(++yCaret==cyBuffer)

yCaret=0;

break;

case '\x1B':

for(y=0;y<cyBuffer;y++)

for(x=0;x<cxBuffer;x++)

BUFFER(x,y)=' ';

xCaret=0; yCaret=0;

InvalidateRect(hWnd,NULL,FALSE);

break;

default:

BUFFER (xCaret,yCaret)= (TCHAR)wParam;

HideCaret(hWnd);

hdc=GetDC(hWnd);

SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,dwCharSet,0,0,0,FIXED_PITCH,NULL));

TextOut(hdc,xCaret*cxChar,yCaret*cyChar,&BUFFER(xCaret,yCaret),1);

DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT))); ReleaseDC(hWnd,hdc);

ShowCaret(hWnd);

if(++xCaret==cxBuffer)

{

xCaret=0;

if(++yCaret==cyBuffer)

yCaret=0;

}

break;

}

}

SetCaretPos(xCaret*cxChar,yCaret*cyChar);

return 0;

case WM_PAINT:

hdc=BeginPaint(hWnd,&PS);

SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,dwCharSet,0,0,0,FIXED_PITCH,NULL));

for(y=0;y<cyBuffer;y++)

TextOut(hdc,0,y*cyChar,&BUFFER(0,y),cxBuffer);

DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT)));

EndPaint(hWnd,&PS);

return 0;

case WM_DESTROY:

PostQuitMessage(0);

return 0;

}

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

}

OUTPUT:

Result:

Thus the above program has been executed successfully.

Ex No: / MOUSE EVENT
Date:

AIM:

To create a program for draw a line wherever the user drags the mouse, and display the text and display the message box in the client area.

ALGORITHM:

Step 1: To begin, select New from the File menu.

Step 2: In the New dialog box, pick the Projects tab.

Step 3: Select Win32 Application. In the Location field, select a subdirectory.

Step 4: In the Project Name field, type the name of the project as Mouse.

Step 5: This will be a subdirectory of the directory indicated in the Location field.

Step 6: The Create New Workspace button should be checked.

Step 7: The platform section should indicate Win32. Choose OK.

Step 8: A dialog box labeled Win32 Application – Step 1 of 1 will appear.

Step 9: Indicate that we want to create an Empty Project, and press the finish button.

Step 10: Select New from the File menu again. In the New dialog box, pick the Files tab.

Step 11: Select C++ source File. The Add To Project box should be checked, and Mouse

should be indicated.

Step 12: Type Mouse.c in the File Name field. Choose OK.

Step 13: Now we can type in the Mouse.c file as shown below.

CODING:

#include<windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdline,int nShowCmd)

{

HWND hwnd;

MSG msg;

WNDCLASS wc;

wc.style=CS_HREDRAW|CS_VREDRAW;

wc.lpfnWndProc=WndProc;

wc.cbClsExtra=0;

wc.cbWndExtra=0;

wc.hInstance=hInstance;

wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);

wc.hCursor=LoadCursor(NULL,IDC_ARROW);

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

wc.lpszMenuName=NULL;

wc.lpszClassName="Window Class";

if(!RegisterClass(&wc))

{

MessageBox(NULL,"can't Registered","window",MB_ICONERROR);

return 0;

}

hwnd=CreateWindow("Window Class","mouse message",WS_OVERLAPPEDWINDOW,10,10,800,600,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,nShowCmd);

UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return 0;

}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)

{

HDC hdc;

int x2,y2,x1,y1;

switch(msg)

{

case WM_LBUTTONDOWN:

x1=LOWORD(lp);

y1=HIWORD(lp);

hdc=GetDC(hwnd);

TextOut(hdc,x1,y1,"HELLO",8);

ReleaseDC (hwnd,hdc);

break;

case WM_RBUTTONDOWN:

x2=LOWORD(lp);

y2=HIWORD(lp);

hdc=GetDC(hwnd);

MoveToEx(hdc,x2,y2,0);

LineTo(hdc,20,30);

ReleaseDC (hwnd,hdc);

break;

case WM_KEYUP:

MessageBox(hwnd,"UPARROW","KEYPRESS",MB_OK);

break;

case WM_MOUSEMOVE:

x2=LOWORD(lp);

y2=HIWORD(lp);

hdc=GetDC(hwnd);

MoveToEx(hdc,x2,y2,0);

LineTo(hdc,20,30);

ReleaseDC (hwnd,hdc);

}

return DefWindowProc(hwnd,msg,wp,lp);

}

OUTPUT:

Result:

Thus the above program has been executed successfully.

Ex No: / DIALOG BASED APPLICATION
Date:

AIM:

To create a program for calculator using Dialog Based Application.

ALGORITHM:

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 to assign IDs to the controls shown in the table below.

Control / ID
Left operand edit control / IDC_FIRST
Right operand edit control / IDC_SECOND
Edit control / IDC_RESULT
Radio button / IDC_OPERATION
Compute pushbutton / IDC_COMPUTE

Step 4: Open the Properties diaog box and click on the Styles tab. Select the System Menu and Minimize Box options.

Step 5: Use ClassWizard to add member variables and a command handler: AppWizard has already generated a class CDialoBasedDlg. Add the data members as shown in the following table,

Control ID / Member variable / Type
IDC_LEFT / m_dFirst / Double
IDC_RIGHT / m_dSecond / Double
IDC_RESULT / m_dResult / Double
IDC_OPERATION / m_nOperation / Int

Step 6:Add the message handler OnCompute for the IDC_COMPUTE button: use class wizard to add the message handler for the IDC_COMPUTE and accept the default function name OnCompute().

Step 7: Code the OnCompute member function in the DialogBasedDlg.cpp file.

Step 8: Build and test the DialogBased.DSW application.

CODING:

DialogBasedDlg.cpp file

void CDialogBasedDlg::OnCompute()

{

UpdateData(TRUE);

switch(m_nOperation)

{

case 0:

// add

m_dResult=m_nFirst + m_dSecond;

break;

case 1:

//subtract

m_dResult=m_nFirst + m_dSecond;

break;

case 2:

//multiply

m_dResult=m_nFirst * m_dSecond;

break;

case 3:

//divide

if(m_dSecond!=0)

{

m_dResult=m_nFirst / m_dSecond;

}

else

{

//For Error Message Box

AfxMessageBox(“Divide by zero”);

m_dResult=0;

}

break;

default:

TRACE(“default;m_nOperation=%d\n”,m_nOperation);

}

UpdateData(FALSE);

}

OUTPUT:

Result:

Thus the above program has been executed successfully.

Ex No: / MULTIPLE DOCUMENT INTERFACE
Date :

AIM:

To develop a MDI document by using MFC AppWizard and to draw text and to draw text and a rectangle in the output view window.

ALGORITHM:

Step 1: Use the AppWizard to create MDI Application Select MFC

AppWizard(exe)

Step 2: Select Multiple Document and deselect the document view

architecture and click next

Step 3: Select the database support to be none in step 2 of AppWozard and click the next

Step 4: The step3 of Appwizard appears and click next

Step 5: The step4 of Appwizard will appear and select the options and click next

Step 6: The step5 of AppWizard appears and select the following options

Step 7: The step6 of AppWizard will appears 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.

Step 10: Add the private variables in CMdiEx View.h.

Private:

CRect m_rectEllipse;

Step 11: Add the initialization code in the CMdiExview.cpp file constructors.

CExView::CExView():m_rectEllipse(0,0,200,200)

{

}

Step 12: Write the code to the CChildView::OnPaint function in the ChildView.cpp source code file.

CODING:

ChildView.cpp source code file.

void CChildView::OnPaint()

{

CPaintDC dc(this);

dc.Rectangle(100,120,200,200);

dc.TextOut(90,90,"Sample MDI Application");

dc.Ellipse(300,300,200,200);

}

OUTPUT:

Result:

Thus the above program has been executed successfully.

Ex No: / DOCUMENT VIEW ARCHITECTURE WITH SERIALIZATION
Date :

AIM:

To Write a VC++ Program to create the Document View Architecture with Serialization.

ALGORITHM:

Step 1: Run VC++ AppWizard(EXE) to create an SDI application. Accept all the default

settings and select the document view architecture and deselect the printing and

print preview by accepting all the default settings and click finish to design the

project.

Step 2: Declare the string data member in a DOCWRITERDoc.h file

CString StrData

Step 3: Intialize the data member in DOCWRITERDoc.cpp file as,

CDOCWRITERDoc::CDOCWRITERDoc()

{

StrData=“ “;

}

Step 4: Edit the Serialize() function in DOCWRITERDoc.cpp

Step 5: Use classwizard to connect the WM_CHAR message to CDOCWRITERView

class

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

Step 7: Build the program and execute it. Now you can type the text and you can save it

in the system and again you open any file and we can view the content which is

serialized inside the document and the output is shown.

CODING:

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

(

CDOCWRITERDoc* pDoc= GetDocument();

ASSERT_VALID(pDoc);

pDoc->StrData+=nChar;

Invalidate();

pDoc->SetModifiedFlag();

CView::OnChar(nChar, nRepCnt, nFlags);

}

void CDOCWRITERView::OnDraw(CDC* pDC)

{

CDOCWRITERDoc* pDoc=GetDocument();

ASSERT_VALID(pDoc);

pDc->TextOut(0,0,pDoc->StrData);

}

OUTPUT:

Write your Text in the Workspace.

Save your Workspace as mca1.txt

Open your Existing mca1.txt

Output Workspace:

Result:

Thus the above program has been executed successfully.

Ex No: / EMPLOYEE PAYROLL USING STATIC ANDDYNAMIC CONTROLS
Date :

AIM:

.To prepare a Payroll using static and dynamic controls

ALGORITHM:

Step 1: Select the project Tab and Click the MFC AppWizard(exe).

Step 2:Enter the Project Name.

Step 3:Click Ok. Select theDialogbasedpplication from Step-1 dialog box.

Step 4:Click Finish. Click Ok on the New Project Information dialog Box.

Step 5:Design your dialog with the controls.

Step 6:Press Ctrl + W to start Class Wizard or from the menu View->Class Wizard to

start Class Wizard.

Step 7:Select the Member Variable tab and add the following member variables.

Step 8: Double click on Calculate Command button and accept the default member

function name(OnCalculate).

Step 9: Append the following coding in “DynaDig.cpp”.

Void CDynamicDlg::OnCalculate()

{

UpdateData(TRUE);

//TODO : Add your control notification handler code here

UpdateData(FALSE);

}

Step 10:Build and Execute the application.

CODING:

void CDynamicDlg::Oncalculate()

{

UpdateData(TRUE);

int detuct;

detuct=m_pf+m_lic+m_loan;

m_da=m_bp/2;

m_ta=(m_bp*5)/100;

m_hra=(m_bp*10)/100;

m_ml=(m_bp*3)/100;

m_gpay=m_bp+m_da+m_ta+m_hra;

m_npay=m_gpay-detuct;

UpdateData(FALSE);

}

OUTPUT:

Result:

Thus the above program has been executed successfully.

Ex No: / CREATION OF MENU, TOOLBAR BUTTONS,ACCELERATORS & TOOLTIP
Date :

AIM:

To write a VC++ program to illustrate the creation and working of menu, tools, accelerator and tool tip.

ALGORITHM:

Step1: Run the MFCAppWizard(exe) and select SDI application.

Step2: Add the menu items as per shown in below using the menu resource

editor.

Main Menu: Graphics

 Line(Popup Menu)

  • Horizontal Line
  • Vertical Line

Ellipse

Circle

RoundRect

Rectangle

Main Menu: Color

Brush Color

Pen Color

Step3:Using Properties Dialog assign the ID for all the sub menu items

as

Menu Caption / Menu ID / Prompt
Horizontal Line / ID_GRAPH_HORZLINE / Horizontal Line\n HorzLine
Vertical Line / ID_GRAPH_VERLINE /

Draws Vertical line

Ellipse / ID_GRAPH_ELLIPSE / Draws Ellipse\n Ellipse Tool
Circle / ID_GRAPH_CIRCLE / This is circle menu
RoundRect / ID_GRAPH_ROUNDRECT / Displays Round Rectangle
Rectangle / ID_GRAPH_RECT / This is rectangle
Brush Color / ID_COLOR_BRUSH / Selects Brush Color
Pen Color / ID_COLOR_PEN / Selects Pen Color

NOTE: The Prompt strings are displayed at status bar whenever user points the menu

item. The string after the “\n”(new line char) is shown as tool tip

When the user points the corresponding tool.

Step 4: Declare the enumerated data type named as Graphics in the

View.cpp file.

Enum Graphics {LINE, VERLINE, HORILINE, CIRCLE,

RECTANGLE, ROUNDRECT, ELLIPSE};

Step 5: Add the member variable for enumerated data type Graphics

named as shape in View class.

enum Graphics shape;

Step 6: Add the following member variables for storing pen and brush

color type to the View class.

private:

COLORREF PenColor;

COLORREF BrushColor;

BOOL m_ncolor;

BOOL m_ncolor1;

Step 7: Using Class wizard add the COMMAND Event handlers to all the

sub menu items.

Step 8: Edit the OnDraw() function of the View Class to draw the various shapes.

Step10: Creation of toolbar buttons for the menu items

  • Select Resource View Pane from the workspace window.
  • Select Toolbar folder & Double click IDR_MAINFRAME. It Opens tool

editor.

  • Click the blank tool button and design the tool icon using the bitmap shown.
  • Double Click the tool button to open the property box. In that select the menu id to

which you want to assign the tool.

Step11: Creation of Accelerator keys for sub menu items

  • Select Resource View Pane from the workspace window.
  • Select Accelerator folder & Double click IDR_MAINFRAME. It shows accelerator

list. In which select blank entry and double click it add new one.

  • In the appeared Accel Properties do the following
  • Select the Modifiers( ie Ctrl | Alt)
  • Enter the char in the key text box.( ie if you want Ctrl+W as a Accel,

select Ctrl Modifier and type ‘W’ in the key text box.

  • Select the menu ID to which you want to assign a accelerator key from the

ID combo box.

Step12: Build and Run the application.

CODING:

COMMAND Event handlers

void CMenucolorView::OnGraphEllipse()

{

shape=ELLIPSE;

Invalidate();

}

void CMenucolorView::OnGraphHorzline()

{

shape=HORILINE;

Invalidate ();

}

void CMenucolorView::OnGraphRectangle()

{

shape=RECTANGLE;

Invalidate();

}

void CMenucolorView::OnGraphRoundRect()

{

shape=ROUNDRECT;

Invalidate();

}

void CMenucolorView::OnGraphCircle()

{

shape=CIRCLE;

Invalidate();

}

void CMenucolorView::OnGraphVerline()

{

shape=VERLINE;

Invalidate();

}

void CMenucolorView::OnColorBrush()

{

CColorDialog c;

if(c.DoModal()==1)

{

Brushcolor=c.GetColor();

Invalidate();

}

}

void CMenucolorView::OnColorPen()

{

CColorDialog c;

if(c.DoModal()==1)

m_ncolor=c.GetColor();

Invalidate();

}

OnDraw() function:

void CMenucolorView::OnDraw(CDC* pDC)

{

CPen pen;

pDC->SelectObject(new CBrush(m_ncolor1));

pen.CreatePen(PS_SOLID,2,m_ncolor);

pDC->SelectObject(&pen);

switch(shape)

{

case ELLIPSE:

pDC->Ellipse(10,10,200,150);

break;

case CIRCLE:

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

break;

case RECTANGLE:

pDC->Rectangle(10,10,200,150);

break;

case VERLINE:

pDC->MoveTo(10,10);

pDC->LineTo(10,100);

break;

case HORILINE:

pDC->MoveTo(10,10);

pDC->LineTo(100,10);

break;

case ROUNDRECT:

pDC->RoundRect(10,10,200,150,25,25);

break;

}

}

OUTPUT:

RESULT:

Thus to write a VC++ program to illustrate the creation and working of menu, tools, accelerator and tool tip is executed and verified.

Ex No: / DLL CREATION FOR ADDITION AND SUBTRACTION
Date :

AIM:

To Write a VC++ Program to create the Regular DLL for addition and subtraction of two numbers.

ALGORITHM:

DLL CREATION

Step 1:Choose File-> New->Project tab and select MFC AppWizard(Dll).

Step 2:Enter the DLL name and press the ok button.

Step 3:Select the option “Regular DLL using Shared MFCDLL” from the

MFCAppWizard(exe) dialog box appears.

Step 4:Click finish button.

Step 5:Write the export function declaration in the “dll header file”

declspec(dllexport) int WINAPI Add(int a,int b);

declspec(dllexport) int WINAPI Sub(int a,int b);

Step 6: Include the definition of export functions in the corresponding “ dll