Mouse Interaction using C/C++

Hi everyone…

Following is the headerfile contains functions to interact with mouse. I took the hint from “Let Us C”.

#include <dos.h>

union REGS i,o;

InitMouse()
{
i.x.ax=0;
int86 (0x33,&i,&o);
return(o.x.ax);
}

void ShowMousePointer()
{
i.x.ax=1;
int86 (0x33,&i,&o);
}

void GetPositionAndButton(int *button, int *x, int *y)
{
i.x.ax=3;
int86 (0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}

void HideMousePointer()
{
i.x.ax=2;
int86 (0x33,&i,&o);
}

Usage:
1. call the InitMouse() function to initialize. (Once required)
2. Call the function ShowMousePointer() to make the pointer appear in your application.
3. The function GetPositionAndButton will return the current mouse pointer location in terms of x & y coordinate and the button status, i.e., left or right etc..
4. You can hide your mouse pointer by calling HideMousePointer function.

Now you can capture the mouse activities using following loop.

while (!kbhit())
{
getmousepos (&button,&x,&y);
gotoxy (5,3);
(button &1)== 1 ? printf (“Left Down”): printf (“Left UP”);
gotoxy (20,3);
(button &2 )==2 ? printf (“Rigt Down”): printf (“Right UP”);
gotoxy (1,1);
printf (“x = %3d, y = %3d”,x,y);
}


5. Thats it.


Keep Smiling 🙂

Advertisements