Loading....
Coupon Accepted Successfully!

 

Mouse Programming

A mouse is an input device for the computer. It is used to move the cursor over the screen of a computer, and it points to the icon or with clicking the right button, some action can be done. When the mouse is moved, the cursor also moves over the screen. With arrow keys, the cursor also can be moved over the screen. However, moving the cursor with a mouse is much faster than using arrow keys.

A computer senses the connectivity of a mouse by loading a mouse driver program. Once the mouse driver program is loaded, various mouse functions can be accessed /provided. The AX register is to be loaded with different values as service numbers and issuing software interrupts such as 51h or 33h. In the following programs, the int86() C++ function calls the interrupts. Its prototype is in dos.h. Its arguments are interrupt number, &in, and &out, which are provided within parentheses. The in and out registers should be a type of REGS. REGS is a built-in UNION declaration in C++.

22.20 Write a program to display a rectangle which is a limited area of a mouse.

#include<conio.h>

#include<dos.h>

#include<graphics.h>

union REGS in,out;

int main()

{

int x,y,cl,a,b;

int gd=DETECT,gm;

int mouse(void);

void limitmsptr(int,int,int,int);

clrscr();

initgraph(&gd,&gm, “c:\\tc\\bgi”); rectangle(100,100,550,400);

mouse();

limitmsptr(100,100,550,400);

getch();

return 0;

}

int mouse()

{

in.x.ax=1;

int86(51,&in,&out);

return 1;

}

void limitmsptr(int a1,int b1,int a2,int b2)

{

in.x.ax=5;

in.x.cx=a1;

in.x.dx=a2;

int86(51,&in,&out);

in.x.ax=7;

in.x.cx=b1;

in.x.dx=b2;

int86(51,&in,&out);

}

 

Explanation: In the above program, the mouse is used in the graphics mode. We have to switch over the functioning of the mouse from the text to the graphics mode. The initgraph() function is responsible for this job.

The function restrictmouseptr(); defines the area of the mouse, that is, the mouse pointer area. In this function, int86() is invoked by initializing CPU registers with appropriate values.

The mouse() function checks whether the mouse driver is loaded or not. The rectangle() function is invoked for drawing the rectangle with the required coordinates. After running the program, a rectangle appears on the screen, which is the restricted area of the mouse. The mouse is activated in the rectangle area only.

In the limitmsptr() function, the ax register is initialized to 5, which limits the mouse pointer to the horizontal obstacle. The cx and dx are initialized with minimum and maximum x horizontal coordinates.

Similarly, ax is initialized to 7 for limiting the vertical obstacle. The cx and dx are further initialized to 100 and 400, respectively, for vertical y coordinates.

22.21 Write a program to display status of mouse button as to whether it is pressed or not and restrict the mouse between given co-ordinates on the screen. Use int86() function to call different ROM-BIOS services.

#include<conio.h>

#include<process.h>

#include<dos.h>

#include<graphics.h>

#include<iostream.h>

union REGS i,o;

void main()

{

int stmouse(void);

void sharow(void);

void msarea(int, int, int,int);

void pntat( int *, int *, int *);

int gd=DETECT, gm,button,x,y;

initgraph(&gd,&gm, “c:\\turboc3\\tc\\bgi”);

rectangle(0,56,100,400);

setviewport (1,57,99,399,1);

gotoxy(28,1);

cout<<“\n Mouse Button pressed”;

if (stmouse()==0)

{

closegraph();

restorecrtmode();

cout<<“\n Mouse driver not found”;

exit(1);

}

msarea(0,100,50,190);

sharow();

gotoxy(52,3);

cout<<“\n Press any key to exit”;

while (!kbhit())

{

pntat(&button,&x,&y);

gotoxy(5,3);

(button & 1)==1 ? cout<<“LEFT”:cout<<“*****”;

gotoxy(20,3);

(button & 2)==2 ? cout<<“RIGHT”:cout<<“*****”;

gotoxy(65,2);

cout<<“Cursor Position”;

gotoxy(65,4);

 

cout<<“X=”<< x<<“\t”<<“Y=”<< y;

}

}

stmouse()

{

i.x.ax=0;

int86(0x33,&i,&o);

return(o.x.ax);

}

void sharow()

{

i.x.ax=1;

int86(0x33,&i,&o);

}

void msarea(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);

}

void pntat( 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;

}

Explanation: In the above program, the function initgraph() switches the mode from text to graphics. The DETECT is a macro that requests the initgraph() to set the graphics driver. The function setviewport() defines the viewport area that bounds the drawing operation inside the area.

The stmouse() function checks whether the mouse driver is loaded or not. If the mouse in not initialized, then the closegraph() function off-loads the graphics driver. The restoremode() function restores the mode.

The function msarea() defines the area in which the mouse pointer can be a pointer. In this function, int86() is invoked by initializing the CPU register with appropriate values.

The functions msarea() and sharow() call ROM-BIOS services under 33h with appropriate values. The while() loop checks the button pressed and displays messages accordingly. The function pntat() displays the current cursor position. When the user presses a key, the program is terminated.





Test Your Skills Now!
Take a Quiz now
Reviewer Name