Chris Pollett > Students >
Shivani

    ( Print View )

    [Bio]

    [Project Blog]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3]

    [CS297Report-PDF]

    [CS298Proposal]

    [CS298Presentation-PDF]

    [CS298Report-PDF]

    [Project Paper-PDF]

                          

























To write a program that records the mouse coordinates in background

Description: The idea of this task was to note the movements of the user in background. This helped to track the path of motion of the user. Wherever the mouse moved, the coordinates were noted in some file. Later coordinates were extracted from the file and a graph was plotted to show the coordinates.

A windows program was written for this that recorded the mouse coordinates on every move the user made. The code installed a windows hook. Whenever the mouse was moved a message was generated in the queue. If two consecutive points differed by more than 7 pixels, then the coordinates were written into a file otherwise they were rejected to avoid excessive plotting of points. A java program then read the file and plotted those points.

Example:This is what my code outputs on these inputs.

 Graph1

 Graph2

The above two examples show the mouse coordinates for a user. Each of these have been taken for about four hours. Each oval shows the pixel where the user's mouse moved.


//   Title:Program to calculate the velocity of mouse movements
// Author : Shivani Hashia
// Date : 1 March 2004


      //  Defines the entry point for the application.


#include "stdafx.h"
#include "windows.h"
#include "winuser.h"
#include "stdio.h"
#include <time.h>

// Global Variables:
HINSTANCE hInst;     // current instance

static CHAR szWindowClass[]="MyMouse";
FILE *fp;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HOOKPROC hkprcSysMsg;
static HHOOK hhookSysMsg;
DWORD hCurrThread;
static HINSTANCE hinstDLL;
HWND  ghWnd=NULL;
int num=0;

void __declspec(dllimport) SetMouseHook(HWND hCallingWnd);
void __declspec(dllimport) RemoveMouseHook(void);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{

   MSG msg;
   HDC   hDC;
   int  count=0;
   PAINTSTRUCT ps;
   int XCoor,YCoor,TempX,TempY;
   char tempstr[50];
   char tmpbuf[128];
   // Initialize global strings

   MyRegisterClass(hInstance);

   // Perform application initialization:
   if (!InitInstance (hInstance, nCmdShow))
   {
      return FALSE;
   }


   fp=fopen("tmp.txt","w");

   SetMouseHook(ghWnd);


   hDC=GetDC(ghWnd);

// if u want to hide the window
   ShowWindow(ghWnd,SW_HIDE);

   // Main message loop:
   while (GetMessage(&msg, NULL, 0, 0))
   {
      if (msg.message==10){

         sprintf(tempstr,"%d %d",msg.pt.x,msg.pt.y);

         XCoor=msg.pt.x; YCoor=msg.pt.y;

         if(num==0)
            {
               fprintf(fp,tempstr);
               fprintf(fp,"\n");
               TempX=XCoor;TempY=YCoor;
               num++;
            }
            else
            {
               if((((TempX-7)>XCoor) ||((TempX+7)<XCoor)) || (((TempY-7)>YCoor)
                  ||((TempY+7)<YCoor)))
               {
                  fprintf(fp,tempstr);
                  fprintf(fp,"\n");
                  TempX=XCoor;TempY=YCoor;
                  num++;
               }
            }


         BeginPaint(ghWnd, &ps);
         TextOut(hDC,10,10+count,tempstr,sizeof(tempstr));
         count+=20;
         EndPaint(ghWnd,&ps);

         if (count > 600){
            PostMessage(ghWnd,WM_PAINT,10,100);
            count=0;
         }

      }


   }

   return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
   WNDCLASSEX wcex;

   wcex.cbSize = sizeof(WNDCLASSEX);

   wcex.style  = CS_HREDRAW | CS_VREDRAW;
   wcex.lpfnWndProc  = (WNDPROC)WndProc;
   wcex.cbClsExtra= 0;
   wcex.cbWndExtra= 0;
   wcex.hInstance= hInstance;
   wcex.hIcon  = NULL;
   wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
   wcex.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
   wcex.lpszMenuName = NULL;
   wcex.lpszClassName   = szWindowClass;
   wcex.hIconSm= NULL;
   return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, "MouseCoordinates", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ghWnd=hWnd;
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT   - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   //int wmId, wmEvent;
   PAINTSTRUCT ps;
   HDC hdc;


   switch (message)
   {
      case WM_PAINT:
         hdc = BeginPaint(hWnd, &ps);
         // TODO: Add any drawing code here...
         RECT rt;
         GetClientRect(hWnd, &rt);
         EndPaint(hWnd, &ps);
         break;
      case WM_DESTROY:
         RemoveMouseHook();
         PostQuitMessage(0);
         fclose(fp);
         break;
      default:
         return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}