/*
 *  Make sure project type is windows application
 */

#define _WIN32_WINNT 0x0500
#include<windows.h>
#include<cmath>

float sensitivity = 0.1f;


LRESULT CALLBACK mouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {

   // Let windows handle mouse clicks
   if (wParam != WM_MOUSEMOVE) {
      return CallNextHookEx(NULL, nCode, wParam, lParam);
   }

   // Get event information
   PMSLLHOOKSTRUCT p = reinterpret_cast<PMSLLHOOKSTRUCT>( lParam );

   // Old mouse location
   static float ox = static_cast<float>( p->pt.x );
   static float oy = static_cast<float>( p->pt.y );

   // Mouse location without sensitivity changed
   float x = static_cast<float>( p->pt.x );
   float y = static_cast<float>( p->pt.y );
   
   // New mouse location
   float nx = ox + (x - ox) * sensitivity;
   float ny = oy + (y - oy) * sensitivity;

   // Set cursor position
   SetCursorPos(
      static_cast<int>( nx ),
      static_cast<int>( ny )
   );

   // Set old location ready for next time
   ox = nx;
   oy = ny;

   return 1;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd) {

   // Set mouse hook
   HHOOK mouseHook = SetWindowsHookEx(
                  WH_MOUSE_LL,      /* Type of hook */
                  mouseHookProc,    /* Hook process */
                  hInstance,        /* Instance */
                  NULL);

   // Wait for user to exit
   MessageBox(NULL, "Press OK to close.", "", MB_OK);
   return 0;
}

Small bug when using a higher sensitivity, to fix it, add this function at the beginning. (line 10)

int round(float a) {
   return int(a + 0.5f);
}

and on line 36 & 37, change them to this.

round( nx ),
round( ny )

Now the mouse doesn't jump about unexpectedly.

This doesn't even compile in Visual C++ Express.

1>c:\path\visual studio 2008\projects\two\two\two.cpp(63) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [19]' to 'LPCWSTR'

That's because you've not set up your compiler properly, don't blame the code. Turn off UNICODE then try compiling again.

Thanks. I needed such a program!
but it has an error in my Borland compiler. to fix the error instead of:

HHOOK mouseHook = SetWindowsHookEx(
                  WH_MOUSE_LL,      /* Type of hook */
                 mouseHookProc,    /* Hook process */
                  hInstance,        /* Instance */
				  NULL);

write:

HHOOK mouseHook = SetWindowsHookEx(
                  WH_MOUSE_LL,      /* Type of hook */
                  (HOOKPROC)mouseHookProc,    /* Hook process */
                  hInstance,        /* Instance */
				  NULL);

And do you have a program for hooking keyboard? or the shell?

And do you have a program for hooking keyboard? or the shell?

Look at my other snippets, indeed I do ;]

I dont mind any changes people make to the code if it makes it compatiable with a wider range of compilers.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.