Click here to Skip to main content
11,480,082 members (65,528 online)
Click here to Skip to main content

Mouse Programming in C/C++

, 13 Aug 2005 155.9K 24
Rate this:
Please Sign up or sign in to vote.
This is an article which describes interfacing of the mouse using C/C++ programming. It contains information about: 1]calling the mouse 2]hiding the mouse 3]setting the position of the mouse 4]restricting the mouse position.

Introduction

       
              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 bellow

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.


       #include<graphics.h>
       #include<stdio.h>
       #include<conio.h>
       #include<dos.h>
       union REGS in,out;

       int callmouse()
       {
              in.x.ax=1;
              int86(51,&in,&out);
              return 1;
       }
       void mouseposi(int &xpos,int &ypos,int &click)
       {
              in.x.ax=3;
              int86(51,&in,&out);
              click=out.x.bx;
              xpos=out.x.cx;
              ypos=out.x.dx;
        }
       int mousehide()
       {
              in.x.ax=2;
              int86(51,&in,&out);
              return 1;
       }
      void setposi(int &xpos,int &ypos)
      {
             in.x.ax=4;
             in.x.cx=xpos;
             in.x.dx=ypos;
             int86(51,&in,&out);
      }
     int main()
      {
             int x,y,cl,a,b;
             clrscr();
             int g=DETECT,m;
             initgraph(&g,&m,"c:\tc\bgi");
             a=100;
             b=400;
             setposi(a,b);
             callmouse();
             do
             {
                    mouseposi(x,y,cl);
                    gotoxy(10,9);
                    printf("\n\tMouse Position is: %d,%d",x,y);
                    printf("\n\tClick: %d",cl);
                    printf("\n\tPress any key to hide the mouse");
             }while(!kbhit());
             getch();
             mousehide();
             printf("\n\n\tPress any key to Exit");
             getch();
     }

 

 The above program makes use of the following functions:

  • callmouse( )

  • mouseposi( )

  • mousehide( )

  • setposi( )

     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.

     mousehide() :-  In this function AX is set to "2".When this function is called in main() it hides the mouse pointer. This function is useful while drawing  figures, first the mouse pointer is kept hidden, then the figure is been drawn and again the mouse pointer is been called.

     mouseposi() :-  In this function AX is set to "3". This function returns the position of the mouse pointer. It contains three parameters,they are xpos,ypos,click. xpos and ypos returns the position of x co-ordinate and y co-ordinate respectively. Click is the integer variable which returns the values 1,2,3 corresponding to the button pressed on the mouse and 0 for buttons being not pressed.
 

note: while loop

kbhit: If any key is pressed kbhit returns nonzero integer; if not it returns zero

      setposi() :-      In this function AX is set to "4". This function sets the mouse pointer to specific position . CX is been loaded by x co-ordinate of the mouse pointer and DX is been loaded with the y co-ordinate of the mouse pointer.

    Let us consider another program


         #include<graphics.h>
         #include<stdio.h>
         #include<conio.h>
         #include<dos.h>
         union REGS in,out;

         int callmouse()
          {
                 in.x.ax=1;
                 int86(51,&in,&out);
                 return 1;
          }
         void restrictmouseptr(int x1,int y1,int x2,int y2)
          {
                 in.x.ax=7;
                 in.x.cx=x1;
                 in.x.dx=x2;
                 int86(51,&in,&out);
                 in.x.ax=8;
                 in.x.cx=y1;
                 in.x.dx=y2;
                 int86(51,&in,&out);
          }
         int main()
          {
                 int x,y,cl,a,b;
                 clrscr();
                 int g=DETECT,m;
                 initgraph(&g,&m,"c:\tc\bgi");
                 rectangle(100,100,550,400);
                 callmouse();
                 restrictmouseptr(100,100,550,400);
                 getch();
          }

            The above program makes use of the following functions:

  • Horizontal ( )

  • Vertical( )

      Horizontal() :- In this function AX is set to "7". Its sets the horizontal barrier for the pointer which restricts the mouse pointer to pass that limit. CX is been loaded with the minimum x co-ordinate and Dx is been loaded with the maximum x co-ordinate.

     Vertical() :-     In this function AX is set to "8".Its sets the vertical barrier for the pointer which restricts the mouse pointer to pass that limit. CX is been loaded with the minimum y co-ordinate and Dx is been loaded with the maximum y co-ordinate.

               For more programs visit my site http://electroguys.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ranjith KH
Web Developer
India India
Ranjith K H
http://electroguys.com

Comments and Discussions

 
SuggestionMessage Automatically Removed Pin
31-Aug-14 2:07
memberMember 1104824331-Aug-14 2:07 
GeneralMy vote of 3 Pin
Member 1100457317-Aug-14 21:42
memberMember 1100457317-Aug-14 21:42 
Generalutility to record mouse movement and keyboard Pin
Member 1096512422-Jul-14 22:20
memberMember 1096512422-Jul-14 22:20 
GeneralNice Pin
Member 983311931-May-13 7:34
memberMember 983311931-May-13 7:34 
Questiongetting linker error .....please help Pin
pavangopal14-Mar-13 9:07
memberpavangopal14-Mar-13 9:07 
AnswerRe: getting linker error .....please help Pin
Member 983311931-May-13 7:36
memberMember 983311931-May-13 7:36 
QuestionCopyright Law Pin
Youssef_D25-May-12 22:59
memberYoussef_D25-May-12 22:59 
GeneralMy vote of 1 Pin
Cyphoid30-Mar-10 16:03
memberCyphoid30-Mar-10 16:03 
Generalabout error in program Pin
aman garg.9122-Nov-09 20:46
memberaman garg.9122-Nov-09 20:46 
GeneralRe: about error in program Pin
Ankur, Owner of Tech Growth11-Aug-12 10:49
memberAnkur, Owner of Tech Growth11-Aug-12 10:49 
Generalc# Pin
watta4831-Dec-07 17:52
memberwatta4831-Dec-07 17:52 
QuestionNeed some help Pin
coffeewithsuds26-Mar-07 14:33
membercoffeewithsuds26-Mar-07 14:33 
AnswerRe: Need some help Pin
Member 983311931-May-13 7:38
memberMember 983311931-May-13 7:38 
GeneralGreat Article! Pin
assafp36-Sep-06 6:47
memberassafp36-Sep-06 6:47 
GeneralThanks Pin
Drexlfoley19-Mar-06 5:59
memberDrexlfoley19-Mar-06 5:59 
GeneralThanks for this .... Pin
Anonymous8-Oct-05 10:39
sussAnonymous8-Oct-05 10:39 
QuestionAre you kidding me? Pin
Ralph Henderson16-Aug-05 11:13
memberRalph Henderson16-Aug-05 11:13 
GeneralUseful information back in 1985... Pin
anonymous14-Aug-05 7:22
sussanonymous14-Aug-05 7:22 
GeneralRe: Useful information back in 1985... Pin
Tarek Ahmed Abdel Rahmane14-Aug-05 7:25
memberTarek Ahmed Abdel Rahmane14-Aug-05 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 14 Aug 2005
Article Copyright 2005 by Ranjith KH
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid