C program to determine which mouse button is clicked

Below is the C program display mouse pointer in graphics mode. Here we have used 2 functions to achieve it.

int initmouse() : function is used to intialize the mouse pointer. Which returns int value. Value 0 indicates Mouse support is not available.

void showmousepointer() : function is used to show mouse pointer. Which does not return any value.

void getmouseposition(int *button, int *x, int *y) : function to get the position of check the position of the mouse pointer. Also it sets the 'button' variable value to 1 or 2, which identifies the which mouse button is clicked.

void outtext(char *string) : Display the text at current position.

void cleardevice() : Function is used to clear the screen in graphics mode.

int kbhit() : Function is used to identify that is key is pressed or not.



Code:
#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
union REGS i, o;
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmousepointer()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
 
void getmouseposition(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;
}
 
main()
{
   int gd = DETECT, gm, status, button, x, y;
   char array[50];
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   settextstyle(DEFAULT_FONT,0,2);
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
   {
      showmousepointer();
 
      getmouseposition(&button,&x,&y);
 
      while(!kbhit())
      {
         getmouseposition(&button,&x,&y);
 
         if( button == 1 )
         {
            button = -1;
            cleardevice();
            sprintf(array,"Left Button clicked x = %d y = %d",x,y);
            outtext(array);
         }
         else if( button == 2 )
         {
            button = -1;
            cleardevice();
            sprintf(array,"Right Button clicked x = %d y = %d",x,y);
            outtext(array);
         }
 
      }
   }
 
   getch();
   return 0;
}

Post a Comment

0 Comments