Перехват мыши

Этот пример показывает как можно отслеживать - с какими окнами в системе работает   пользователь. То есть при нажатии на Титле бар , минимизации, закрытии окна.

#define STRICT
#include
#include "resource.h"

const char *pszTitle = "Descriptor Systems Mouse Capture Example";

// internal function prototypes
LRESULT CALLBACK WndProc ( HWND hwnd, UINT iMsg
, WPARAM wParam, LPARAM lParam );

int PASCAL WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance
, LPSTR lpszCmdLine, int nCmdShow )
{
HWND hwnd; // window handle
WNDCLASS wndclass; // class structure
MSG msg; // msg data structure


// register only if this is the first instance
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon ( hInstance
, MAKEINTRESOURCE ( IDI_MYICON ));
wndclass.hCursor = LoadCursor ( 0, IDC_ARROW );
wndclass.hbrBackground = (HBRUSH)GetStockObject ( WHITE_BRUSH );
wndclass.lpszMenuName = MAKEINTRESOURCE ( IDM_MENU );
wndclass.lpszClassName = "Template";

// attempt to register -- exit if we can't
if ( !RegisterClass ( &wndclass ) )
return 0;
}

// create a window based on the class we just registered
hwnd = CreateWindow (
"Template" // class name
, pszTitle // window caption (title)
, WS_OVERLAPPEDWINDOW // style
, CW_USEDEFAULT // initial x
, CW_USEDEFAULT // initial y
, CW_USEDEFAULT // initial cx
, CW_USEDEFAULT // initial cy
, 0 // no parent
, 0 // no menu
, hInstance // instance handle
, NULL ); // no create parameters

if ( hwnd == 0 )
return 0;

// show the window and force it to paint
ShowWindow ( hwnd, nCmdShow );
UpdateWindow ( hwnd );

// enter the event loop -- remain in loop until window closes
while ( GetMessage ( &msg, NULL, 0, 0 ) != FALSE )
{
DispatchMessage ( &msg );
}

return msg.wParam;
}

//**********************************************************
LRESULT CALLBACK WndProc ( HWND hwnd, UINT iMsg
, WPARAM wParam, LPARAM lParam )
{

switch ( iMsg )
{
case WM_DESTROY:
PostQuitMessage (0);
return 0;

case WM_COMMAND:
{
switch ( LOWORD ( wParam ) )
{
case IDM_CHOOSE:
{
MessageBox ( hwnd, "Click on any window"
, "Choose a window"
, MB_OK | MB_ICONINFORMATION );

SetCapture ( hwnd );
}
break;
}
}
return 0;

case WM_LBUTTONUP:
{
if ( GetCapture () == hwnd )
{
POINT pt;

// retrieve the mouse coordinates
pt.x = (short)LOWORD ( lParam );
pt.y = (short)HIWORD ( lParam );

// convert to screen units
ClientToScreen ( hwnd, &pt );

// determine handle of window under the click
HWND hwndClick = WindowFromPoint ( pt );

// display the window's text
if ( hwndClick != 0 )
{
char szText[255];

GetWindowText ( hwndClick, szText
, sizeof szText );

MessageBox ( hwnd, szText, "You clicked on"
, MB_OK | MB_ICONINFORMATION );
}

// release the mouse capture
ReleaseCapture ();
}
}

case WM_PAINT:
{
PAINTSTRUCT ps;

BeginPaint ( hwnd, &ps );

RECT rc;

GetClientRect ( hwnd, &rc );

DrawText ( ps.hdc
, "Select \"Choose Window\" from the menu"
, -1, &rc
, DT_SINGLELINE | DT_CENTER | DT_VCENTER );

EndPaint ( hwnd, &ps );
}
return 0;

default:
return DefWindowProc ( hwnd, iMsg, wParam, lParam);
}
}
 
« Предыдущая статья   Следующая статья »