Visual C++/Windows SDK Program for the Implementation of Keyboard and Mouse Events | CS1255 - Visual Programming Laboratory


AIM:
To write a Visual C++/Windows SDK Program for the Implementation of Keyboard and Mouse Events in CS1255 - Visual Programming Lab.

SOURCE CODE:
#include<windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
 HWND hwnd;
 MSG msg;
 WNDCLASS wndclass;
 wndclass.style=CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=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="Window Class";
 if(!RegisterClass(&wndclass))
 {
  MessageBox(NULL,"Can not Registered","Window",MB_ICONERROR);
  return 0;
 }
 hwnd=CreateWindow("Window Class","JEBASTIN - KEYBOARD AND MOUSE EVENTS",WS_OVERLAPPEDWINDOW,10,10,800,600,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 wp,LPARAM lp)
{
 switch(message)
 {
 case WM_LBUTTONDOWN:
     MessageBox(GetFocus(),"You have pressed Left Mouse Button","Mouse Events",MB_OK|MB_ICONINFORMATION);
    break;
 case WM_RBUTTONDOWN:
    MessageBox(GetFocus(),"You have pressed Right Mouse Button","Mouse Events",MB_OK|MB_ICONINFORMATION);
    break;
 case WM_KEYDOWN:
    MessageBox(GetFocus(),"You have typed a key from Keyboard","Keyboard Events",MB_OK|MB_ICONINFORMATION);
    break;
 case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
 case WM_DESTROY:
    PostQuitMessage(0);
    break;
 }
 return DefWindowProc(hwnd,message,wp,lp);
}

OUTPUT:

Previous
Next Post »

Still not found what you are looking for? Try again here.