Home | API | MFC | C++ | C | Up

Programming Windows API

Working with the Mouse Example

#include <windows.h>
#include <tchar.h>
TCHAR mousestr[255]=TEXT("\0");
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Connect") ;
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 = szAppName ;
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, TEXT ("Mouse Demo"),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 ;
hdc=GetDC(hwnd);
switch (message)
{
case WM_LBUTTONDOWN://deals with left mouse button down
if (wParam & MK_SHIFT)//deals with shift key press followed by left button down
{
wsprintf(mousestr,TEXT("shift & left button down %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
}
else if (wParam & MK_CONTROL)//deals with control key press followed by left button down
{
wsprintf(mousestr,TEXT("control & left button down %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
}
else if (wParam & MK_RBUTTON)//deals with and right button down followed by left button down
{
wsprintf(mousestr,TEXT("left and right button %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
}
else //default left mouse button only
{
wsprintf(mousestr,TEXT("left button down at %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
}
ReleaseDC(hwnd,hdc);
break;
case WM_LBUTTONUP://deals with left mouse button up
wsprintf(mousestr,TEXT("left button up at %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
ReleaseDC(hwnd,hdc);
break;
case WM_RBUTTONDOWN://deals with right mouse button down
wsprintf(mousestr,TEXT("right button down at %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
ReleaseDC(hwnd,hdc);
break;
case WM_RBUTTONUP://deals with right mouse button up
wsprintf(mousestr,TEXT("right button up at %d,%d"),LOWORD(lParam),HIWORD(lParam));
TextOut(hdc,LOWORD(lParam),HIWORD(lParam),mousestr,_tcslen(mousestr));
ReleaseDC(hwnd,hdc);
break;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Home | API | MFC | C++ | C | Up
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions

Last Updated:26 October 2022