Mouse Programming C and C++

Mouse can be used in text mode as well as in graphics mode. Usually it is used in graphics mode. Hence we must first change over to graphics mode. In our program the function initgraph() is responsible for switching the mode from text to graphics .DETECT is a macro defined in ‘graphics.h’. It requests initgraph() to automatically determine which graphics driver to load in order to switch to the highest resolution graphics mode. The initgraph() function takes three parameters, the graphics driver, the graphics mode and the path to the driver file.

Once the driver has been loaded, initgraph() sets up the numeric values of the graphics mode chosen in the variables gd and gm respectively. Here we are assuming that the driver files are in the directory ‘c:\tc\bgi’. Hence the path passed to initgraph() is ‘c:\tc\bgi’.
The various mouse functions can be accessed by setting up the AX register with different values (service number) and issuing interrupt number 51. The functions are listed below.

Interrupt Service Purpose
51 0
Reset mouse and get status
Call with AX = 0
Returns:  AX = FFFFh If mouse support is available
Ax = 0 If mouse support is not available
51 1
Show mouse pointer
Call with AX = 1
Returns: Nothing
51 2
Hide mouse pointer
Call with AX = 2
Returns: Nothing
51 3
Get mouse position and button status
Call with AX = 3
Returns: BX = mouse button status
Bit   Significance
0     button not pressed
1     left button is pressed
2     right button is pressed
3     center button is pressed
CX = x coordinate
DX = y coordinate
51 4
Set mouse pointer position
Call with AX = 4
CX = x coordinate
DX = y coordinate
Returns: Nothing
51 7
Set horizontal limits for pointer
Call with AX = 7
CX = minimum x coordinate
DX = maximum x coordinate
Returns: Nothing
51 8
Set vertical limits for pointer
Call with AX = 8
CX = minimum y coordinate
DX = maximum y coordinate
Returns: Nothing

Let us consider a program which makes use of the above functions.
 

A SIMPLE PROGRAM TO CALL MOUSE POINTER

         #include < graphics.h >
         #include < stdio.h >
         #include < conio.h >
         #include < dos.h >


         union REGS in,out;

       void callmouse()
      {
         in.x.ax=1;                                      
         int86(51,&in,&out);

      }
            

 //MAIN FUNCTION
 void main()
      {
         int x,y,cl,a,b;
         clrscr();
         int g=DETECT,m;
         initgraph(&g,&m,”");


         callmouse();     // CALLING MOUSE FUNCTION
          getch();
      }

callmouse() :-  In this function AX is set to “1″. When this function is called in main() it displays the mouse pointer. The position of the pointer can be changed by using the mouse.

BY NISHANT SRIVASTAVA 

www.geekstrack.blogspot.com

Leave an Awesome Comment

comments

About Admin

Nishant Srivastava is a blogger & Software Developer from New Delhi, India. He writes
about Programming Technologies,Gadget Reviews,Website Reviews,Tips and Tricks about Computers,SEO, Technology and Make money online on this Blog.

GeeksTrack.com Runs on Hostgator Hosting!

Geeks Track Strongly Recommends to use Hostgator if you are Planning to start Online Business,The Hostgator Hosting Helps you to build incredible websites with WordPress. Hostgator provides 24x7 Support and Number one hosting provider for hosting Blogs and Websites.

Speak Your Mind

*