- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Restrict mouse pointer in Circle.

Posted by Tayyab

This program restricts mouse pointer in a circle i.e you can't move mouse out of a circle. When you try to bring mouse pointer outside the circle, mouse pointer is moved to it's previous location which is inside the circle. Code to restrict mouse in circle is given below :-
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<math.h>
 
union REGS i, o;
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33, &i, &o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33, &i, &o);
}
 
void hidemopuseptr()
{
   i.x.ax = 2;
   int86(0X33,&i,&o);
}
 
void getmousepos(int *x, int *y)
{
   i.x.ax = 3;
   int86(0X33, &i, &o);
   *x = o.x.cx;
   *y = o.x.dx;
 
}
 
void movemouseptr(int x, int y)
{
   i.x.ax = 4;
   i.x.cx = x;
   i.x.dx = y;
   int86(0X33, &i, &o);
}
 
main()
{
   int gd = DETECT, gm, midx, midy, radius, x, y, tempx, tempy;
 
   radius = 100;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   if(!initmouse())
   {
      closegraph();
      exit(1);
   }
 
   midx = getmaxx()/2;
   midy = getmaxy()/2;
 
   showmouseptr();
   movemouseptr(midx, midy);
   circle(midx, midy, radius);
 
   x = tempx = midx;
   y = tempy = midy;
 
   while(!kbhit())
   {
      getmousepos(&x, &y);
 
      if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0)
      {
         movemouseptr(tempx, tempy);
         x = tempx;
         y = tempy;
      }
 
      tempx = x;
      tempy = y;
   }
 
   closegraph();
   return 0;
}


read more

Restrict mouse pointer in rectangle.

Posted by Tayyab

This program restricts mouse pointer in a rectangle. When this program is executed the mouse pointer is restricted in a rectangle i.e. you can't move mouse pointer out of the rectangle.
/* Program to restrict mouse-pointer */
#include<dos.h>
#include<graphics.h>
#include<conio.h>
 
int initmouse();
void showmouseptr();
void hidemouseptr();
void restrictmouseptr(int, int, int, int);
 
union REGS i, o;
 
main()
{
   int status, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   settextstyle(DEFAULT_FONT,0,2);
 
   status = initmouse();
 
   if ( status == 0 )
      outtext("Mouse support not available.\n");
   else
   {
      showmouseptr();
      rectangle(120,70,520,410);
      restrictmouseptr(120,70,520,410);
   }
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
 
void restrictmouseptr(int x1, int y1, int x2, int y2)
{
   i.x.ax = 7;
   i.x.cx = x1;
   i.x.dx = x2;
   int86(0X33,&i,&o);
 
   i.x.ax = 8;
   i.x.cx = y1;
   i.x.dx = y2;
   int86(0X33,&i,&o);
}


read more

Determine which mouse button is clicked.

Posted by Tayyab

This program print which mouse button is clicked whether left or right, coordinates of the point where button is clicked are also printed on the screen.
/* Program to determine which mouse button is clicked */
#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 showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
 
void getmousepos(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
   {
      showmouseptr();
 
      getmousepos(&button,&x,&y);
 
      while(!kbhit())
      {
         getmousepos(&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;
}


read more

Current position of mouse pointer.

Posted by Tayyab

This program prints the x and y coordinates of current position of mouse pointer i.e. wherever you move the mouse coordinates of that point will be printed on the screen.
/* Program to get mouse-pointer coordinates - where is the mouse */
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
 
int initmouse();
void showmouseptr();
void hidemouseptr();
void getmousepos(int*,int*,int*);
 
union REGS i, o;
 
main()
{
   int gd = DETECT, gm, status, button, x, y, tempx, tempy;
   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
   {
      showmouseptr();
 
      getmousepos(&button,&x,&y);
 
      tempx = x;
      tempy = y;
 
      while(!kbhit())
      {
         getmousepos(&button,&x,&y);
 
         if( x == tempx && y == tempy )
         {}
         else
         {
            cleardevice();
            sprintf(array,"X = %d, Y = %d",x,y);
            outtext(array);
            tempx = x;
            tempy = y;
         }
      }
   }
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
 
void getmousepos(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;
}


read more

Hide mouse pointer.

Posted by Tayyab

This program hides mouse pointer. We require to hide mouse pointer when we want to draw something on screen as it may interfere with drawing, after that we again make it visible.
/* Program to show and hide mouse-pointer alternatively */
#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
int initmouse();
void showmouseptr();
void hidemouseptr();
 
union REGS i, o;
 
main()
{
   int status, count = 1, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
   {
      showmouseptr();
 
      while(count<=10)
      {
         getch();
         count++;
         if(count%2==0)
            hidemouseptr();
         else
            showmouseptr();
      }
   }
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
 
void hidemouseptr()
{
   i.x.ax = 2;             // to hide mouse
   int86(0X33,&i,&o);
}


read more

Display mouse pointer in graphics mode.

Posted by Tayyab

This program displays mouse pointer in graphics mode. First graphics mode is initialized and then mouse using initmouse.


#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
int initmouse();
void showmouseptr();
 
union REGS i, o;
 
main()
{
   int status, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      showmouseptr();
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}


read more

Display mouse pointer in textmode.

Posted by Tayyab

* Program to display mouse-pointer in text-mode */
#include<dos.h>
#include<conio.h>
 
int initmouse();
void showmouseptr();
 
union REGS i, o;
 
main()
{
   int status;
 
   status = initmouse();
 
   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      showmouseptr();
 
   getch();
   return 0;
}
 
int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
 
void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}


read more