Home   Index   About
Ultimate Pack


Custom Search
Using Cursor Functions to Create a Mousetrap

The following example uses the SetCursorPos, GetCursorPos, CreateCursor, LoadCursor, and SetCursor functions to create a simple mousetrap. It also uses cursor and timer functions to monitor the cursor's position every 10 seconds. If the cursor position has not changed in the last 10 seconds and the application's main window is minimized, the application changes the cursor and moves it to the mousetrap icon.

An example for a similar mousetrap is included in Icons. It uses the LoadCursor and LoadIcon functions instead of the more device-dependent CreateCursor and CreateIcon functions.

HICON hIcon1; // icon handles

POINT ptOld; // previous cursor location

HCURSOR hCurs1; // cursor handle

// The following cursor bitmasks are defined in a code

// example that appears earlier in this topic.

// Yin cursor AND and XOR bitmasks

BYTE ANDmaskCursor[] = ...

BYTE XORmaskCursor[] = ...

// Yang icon AND bitmask

BYTE ANDmaskIcon[] = {0xFF, 0xFF, 0xFF, 0xFF, // line 1

0xFF, 0xFF, 0xC3, 0xFF, // line 2

0xFF, 0xFF, 0x00, 0xFF, // line 3

0xFF, 0xFE, 0x00, 0x7F, // line 4

0xFF, 0xFC, 0x00, 0x1F, // line 5

0xFF, 0xF8, 0x00, 0x0F, // line 6

0xFF, 0xF8, 0x00, 0x0F, // line 7

0xFF, 0xF0, 0x00, 0x07, // line 8

0xFF, 0xF0, 0x00, 0x03, // line 9

0xFF, 0xE0, 0x00, 0x03, // line 10

0xFF, 0xE0, 0x00, 0x01, // line 11

0xFF, 0xE0, 0x00, 0x01, // line 12

0xFF, 0xF0, 0x00, 0x01, // line 13

0xFF, 0xF0, 0x00, 0x00, // line 14

0xFF, 0xF8, 0x00, 0x00, // line 15

0xFF, 0xFC, 0x00, 0x00, // line 16

0xFF, 0xFF, 0x00, 0x00, // line 17

0xFF, 0xFF, 0x80, 0x00, // line 18

0xFF, 0xFF, 0xE0, 0x00, // line 19

0xFF, 0xFF, 0xE0, 0x01, // line 20

0xFF, 0xFF, 0xF0, 0x01, // line 21

0xFF, 0xFF, 0xF0, 0x01, // line 22

0xFF, 0xFF, 0xF0, 0x03, // line 23

0xFF, 0xFF, 0xE0, 0x03, // line 24

0xFF, 0xFF, 0xE0, 0x07, // line 25

0xFF, 0xFF, 0xC0, 0x0F, // line 26

0xFF, 0xFF, 0xC0, 0x0F, // line 27

0xFF, 0xFF, 0x80, 0x1F, // line 28

0xFF, 0xFF, 0x00, 0x7F, // line 29

0xFF, 0xFC, 0x00, 0xFF, // line 30

0xFF, 0xF8, 0x03, 0xFF, // line 31

0xFF, 0xFC, 0x3F, 0xFF}; // line 32

// Yang icon XOR bitmask

BYTE XORmaskIcon[] = {0x00, 0x00, 0x00, 0x00, // line 1

0x00, 0x00, 0x00, 0x00, // line 2

0x00, 0x00, 0x00, 0x00, // line 3

0x00, 0x00, 0x00, 0x00, // line 4

0x00, 0x00, 0x00, 0x00, // line 5

0x00, 0x00, 0x00, 0x00, // line 6

0x00, 0x00, 0x00, 0x00, // line 7

0x00, 0x00, 0x38, 0x00, // line 8

0x00, 0x00, 0x7C, 0x00, // line 9

0x00, 0x00, 0x7C, 0x00, // line 10

0x00, 0x00, 0x7C, 0x00, // line 11

0x00, 0x00, 0x38, 0x00, // line 12

0x00, 0x00, 0x00, 0x00, // line 13

0x00, 0x00, 0x00, 0x00, // line 14

0x00, 0x00, 0x00, 0x00, // line 15

0x00, 0x00, 0x00, 0x00, // line 16

0x00, 0x00, 0x00, 0x00, // line 17

0x00, 0x00, 0x00, 0x00, // line 18

0x00, 0x00, 0x00, 0x00, // line 19

0x00, 0x00, 0x00, 0x00, // line 20

0x00, 0x00, 0x00, 0x00, // line 21

0x00, 0x00, 0x00, 0x00, // line 22

0x00, 0x00, 0x00, 0x00, // line 23

0x00, 0x00, 0x00, 0x00, // line 24

0x00, 0x00, 0x00, 0x00, // line 25

0x00, 0x00, 0x00, 0x00, // line 26

0x00, 0x00, 0x00, 0x00, // line 27

0x00, 0x00, 0x00, 0x00, // line 28

0x00, 0x00, 0x00, 0x00, // line 29

0x00, 0x00, 0x00, 0x00, // line 30

0x00, 0x00, 0x00, 0x00, // line 31

0x00, 0x00, 0x00, 0x00}; // line 32

hIcon1 = CreateIcon(hinst, // handle of app instance

32, // icon width

32, // icon height

1, // number of XOR planes

1, // number of bits per pixel

ANDmaskIcon, // AND bitmask

XORmaskIcon); // XOR bitmask

hCurs1 = CreateCursor(hinst, // handle of app instance

19, // horizontal position of hot spot

2, // vertical position of hot spot

32, // cursor width

32, // cursor height

ANDmaskCursor, // AND bitmask

XORmaskCursor); // XOR bitmask

// Fill in the window class structure.

WNDCLASS wc;

wc.hIcon = hIcon1; // class icon

wc.hCursor = LoadCursor(NULL, IDC_ARROW); // class cursor

//

// Register the window class and perform

// other application initialization.

//

// Set a timer for the mousetrap.

GetCursorPos(&ptOld);

SetTimer(hwnd, IDT_CURSOR, 10000, (TIMERPROC) NULL);

LONG APIENTRY MainWndProc(

HWND hwnd, // window handle

UINT message, // type of message

UINT wParam, // additional information

LONG lParam) // additional information

{

HDC hdc; // handle of device context

POINT pt; // current cursor location

RECT rc; // iconized window location

switch (message)

{

//

// Process other messages.

//

case WM_TIMER:

// If the window is minimized, compare the

// current cursor position with the one 10

// seconds before. If the cursor position has

// not changed, move the cursor to the icon.

if (IsIconic(hwnd))

{

GetCursorPos(&pt);

if ((pt.x == ptOld.x) && (pt.y == ptOld.y))

{

GetWindowRect(hwnd, &rc);

SetCursorPos(rc.left + 20, rc.top + 4);

// Note that the additional constants

// (20 and 4) are application-specific

// values to align the yin-shaped cursor

// and the yang-shaped icon.

}

else

{

ptOld.x = pt.x;

ptOld.y = pt.y;

}

}

return 0;

case WM_SETCURSOR:

// If the window is minimized, draw hCurs1.

// If the window is not minimized, draw the

// default cursor (class cursor).

if (IsIconic(hwnd))

{

SetCursor(hCurs1);

break;

}

case WM_DESTROY:

// Destroy timer.

KillTimer(hwnd, IDT_CURSOR);

PostQuitMessage(0);

break;

}

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun