Sunday, September 8, 2013

Windows program-Mouse Event-1(win32)

MOUSE EVENT I

Program:

MouseEvents.C


#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 ("HELLO");
HWND hwnd;
MSG msg;
// DECLARING AND INTIAZING THE WINDOW CLASS
WNDCLASS wc;                                                       // Window class declaration
wc.style = CS_HREDRAW| CS_VREDRAW;        // Window style
wc. lpfnWndProc = WndProc;                                   // Window Procedure
wc.cbClsExtra = 0;                                                     // Extra Parameters
wc.cbWndExtra = 0;                                                   // Extra Parameters
wc.hInstance = hInstance;                                          // Instance handle
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); // Icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);            // Window Cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
                                                                                    // Set Background Color
wc.lpszMenuName = NULL;                                     // Menu Handle
wc.lpszClassName = szAppName;                             // Class Name
// REGISTERING THE WINDOW CLASS
if (!RegisterClass (&wc))
{
            MessageBox(NULL, TEXT("This program requires WindowsNT!"), szAppName, MB_ICONERROR);
            return 0;
}
// CREATING THE WINDOW
hwnd = CreateWindow ( szAppName,                                  // window class name
TEXT("The Hello Program"), // window caption 
WS_OVERLAPPEDWINDOW,      // window style
CW_USEDEFAULT,                                    // initial x position
CW_USEDEFAULT,                                    // initial x position
CW_USEDEFAULT,                                    // initial x position
CW_USEDEFAULT,                                    // initial x position
NULL,                                                            // parent window handle
NULL,                                                            // window menu handle
hInstance,                                          //program instance handle
NULL);                                               // creation parameters
// DISPLAYING THE WINDOW
            ShowWindow (hwnd, iCmdShow);              
            UpdateWindow (hwnd);
// PROCESSING THE MESSAGE LOOP
                                    while (GetMessage(&msg,NULL,0,0))          // extracts message from Message Queue
{
                        TranslateMessage (&msg);
                        DispatchMessage (&msg);                  // dispatches the message received
            }
            return msg.wParam;
}

// WINDOW PROCEDURE FUNCTION
LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
            char left[]="How are u?";
            char right[]="Hello";
            switch (message)
            {
            case WM_LBUTTONDOWN:           // Left mouse button press message
                        MessageBox(GetFocus(),left,"Left Mouse Button Pressed",MB_OK);
                        return 0;
            case WM_RBUTTONDOWN:          // Right mouse button press message
                        MessageBox(GetFocus(),right,"Right Mouse Button Pressed",MB_OK);
                        return 0;
            case WM_DESTROY:                       // used to close the window
                        PostQuitMessage(0);               // used to terminate while loop in WinMain()
                        return 0;
            }
            return DefWindowProc(hwnd,message,wparam,lparam);

}
Sample Output:

No comments:

Post a Comment