KEYBOARD AND MOUSE EVENTS | Create SDI PROGRAM CREATING WINODOW AND WORK on keyboard and mouse events using VC++.

OBJECTIVE:
To write a SDI program for creating a window and work on keyboard and mouse events.
PROCEDURE:

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
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,PSTR SzCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("ex 10");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW CS_VREDRAW;
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_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
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_SINGLELINEDT_CENTERDT_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);
}
}




Related post