Graphics in C – Getting Position of Moving Mouse
In C computer language you
can get the position of mouse pointer on the screen. There are plenty of
services available under interrupt 33h. In the previous post, we have seen
mouse pointer moving within a boundary. Now, we’ll see the mouse pointer along
with its screen coordinates. Please type the program given below in your
editor.
#include<graphics.h>
#include<dos.h>
union REGS in, out;
int mousestart();
void showpointer();
void boundpointer(int x1, int y1, int
x2, int y2);
void mouseposition(int *x, int *y);
void main()
{
int gd = DETECT, gm, maxx, maxy, x, y;
char str[20];
initgraph(&gd, &gm,
"c:\\tc\\bgi");
maxx = getmaxx();
maxy = getmaxy();
if(mousestart()==0)
{
closegraph();
restorecrtmode();
printf("\nMouse
driver not loaded");
exit(1);
}
while(!kbhit())
{
cleardevice();
rectangle(0, 20, maxx,
maxy-20);
boundpointer(1, 21,
maxx-1, maxy-21);
showpointer();
mouseposition(&x,
&y);
sprintf(str, "(%d,
%d)", x, y);
outtextxy(x, y, str);
delay(10);
}
closegraph();
restorecrtmode();
}
int mousestart()
{
in.x.ax = 0;
int86(0x33, &in, &out);
return(out.x.ax);
}
void showpointer()
{
in.x.ax = 1;
int86(0x33, &in, &out);
}
void boundpointer(int x1, int y1, int
x2, int y2)
{
in.x.ax = 7;
in.x.cx = x1;
in.x.dx = x2;
int86(0x33, &in, &out);
in.x.ax = 8;
in.x.cx = y1;
in.x.dx = y2;
int86(0x33, &in, &out);
}
void mouseposition(int *x, int *y)
{
in.x.ax = 3;
int86(0x33, &in, &out);
*x = out.x.cx;
*y = out.x.dx;
}
Compile and execute this
program. In the output, you’ll see that as you move the mouse pointer, the
screen coordinates of mouse pointer show up along with the mouse pointer.
In this program, I have
declared two variables ‘in’ and ‘out’ of type union REGS. What is union
REGS, I have already discussed this in my previous post. To recall the
topic, please see here.
Then, I have declared four
functions.
int mousestart();
void showpointer();
void boundpointer(int x1, int y1, int
x2, int y2);
void mouseposition(int *x, int *y);
In the function main(), I have got maximum screen coordinates by these
statements given below-
maxx = getmaxx();
maxy = getmaxy();
Then, I checked whether
mouse driver is loaded or not.
if(mousestart()==0)
{
closegraph();
restorecrtmode();
printf("\nMouse driver not loaded");
exit(1);
}
The definition of function mousestart() is given after the function main(). For the
explanation of this function, please refer my previous post here.
Now, I have taken a ‘while’ loop, which executes repeatedly until a key is
pressed. In this loop, a rectangle has been drawn to define the boundaries of
mouse pointer. Then, a function boundpointer() is called, which
is defined after the function main(). Function boundpointer() restricts the movement of mouse pointer
within the rectangular boundaries.
rectangle(0, 20, maxx, maxy-20);
boundpointer(1, 21, maxx-1, maxy-21);
For the explanation of
function boundpointer(), please refer my
previous post here.
Now, function showpointer() has been called to show the mouse pointer on
the screen. Definition of this function is given after the function main(). For the explanation of this function, please refer
my previous post here.
To get the position of mouse
pointer on the screen, I have called the function mouseposition().
mouseposition(&x, &y);
In this function, I have
passed the addresses of variables ‘x’
and ‘y’. This is so, because, in the
definition of this function, the parameters are the pointer variables. This is
the way of a function to return more than one value to the calling function.
Now, see the function definition given below-
void mouseposition(int *x, int *y)
{
in.x.ax = 3;
int86(0x33, &in, &out);
*x = out.x.cx;
*y = out.x.dx;
}
To understand this function,
please recall the service 3 available under interrupt 33h, which is given
below-
Interrupt : 33h
Service : 3
Gets mouse position and button status
Call with AX = 3
Returns BX = Mouse button status
(1=Corresponding button pressed)
Bit 0 à left button is down
Bit 1 à right button is down
Bit 2 à center button is down (if
present)
Bits 3-15 à cleared to 0
CX = x coordinate
DX = y coordinate
Therefore, you can see, I
have given the value ‘3’ to ‘ax’ variable, called the function int86(), and passed the addresses of variables ‘in’ and ‘out’ in this function. This function returns some values in ‘out’ variable. We know that after executing
the service ‘3’ through function int86(), variable ‘cx’
has the value of mouse’s x-coordinate, and variable ‘dx’ will get the value of mouse’s y-coordinate on the screen. These
values are assigned to pointer variables ‘x’
and ‘y’. Here, I assume you understand
the basics of pointers. To understand this function better, please refer my two
recent posts.
After getting mouse
pointer’s position on screen, we have formatted these values to a string ‘str’ using function sprintf(). Next, this string is being displayed continuously
on this mouse position using function outtextxy() as you move mouse
pointer. Function delay() has been used to give a smooth
display.
mouseposition(&x, &y);
sprintf(str, "(%d, %d)", x,
y);
outtextxy(x, y, str);
delay(10);
Now, the explanation of this
simple program is complete. For explanation of the rest of the functions and terms,
please refer my previous posts. You can click on labels given in the right
side-bar for the explanation of any function.
OUTPUT:
OUTPUT:
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C