0% found this document useful (0 votes)
32 views

Mouse-Programming in CPP

Uploaded by

jawadalijamshaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Mouse-Programming in CPP

Uploaded by

jawadalijamshaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Mouse Programming

With “C” or “C++”


Fundamental knowledge:-
 Mouse programming is developed by
Dr.Peter
Grogono in the early 1980`s.
 Each device has a unique port which
has a hexadecimal value and value is
designed to be machine independent .
 Mouse has port attached to it 0X33 .
 We also make use of address registers.
These are basically Union of type REGS
defined in dos.h .
 Just remember we use two register to
communicate to a device driver using
AX Register:-
•We can access various mouse
functions using different values
of AX input Register and passing
those values to mouse port using
a interrupt .

•The Functions are listed below


- Here AX, BX, CX and DX are
members of Union REGS .
Function Returns
Input Performed

AX=0 Get Mouse Output AX Value = FFFFh if mouse support


is available.
Status Output AX Value = 0 if mouse support is not
available.
AX=1 Show Nothing
Mouse
Pointer

AX=2 Hide Mouse Nothing


Pointer

AX=3 Mouse CX = Mouse X Coordinate


Position DX = Mouse Y Coordinate

AX=3 Mouse BX = 0 No Key Is Pressed ,BX = 1 Left Button is


Button Pressed, BX = 2 Right Button is Pressed ,BX = 3
Press Centre Button is Pressed
Ax =7 Set Nothing
Horizontal
Limit
Ax =8 Set Vertical Nothing
Limit
DETECT MOUSE
• Before starting mouse programming we
should always check whether the mouse
programming is supported or not.

• If some how mouse fails to initialize we


should always make sure that our
program terminates.

• To do mouse programming we should


include file ie <dos.h>.
DETECT MOUSE :-
#include <dos.h> //Header file for mouse
detecting
union REGS in, out; //REGS is union
void detectmouse ( ) //function for mouse
detection
{
in.x.ax = 0;
int86 (0X33,&in,&out); //invoke interrupt

if (out.x.ax == 0)
cout<<"\nMouse Fail To Initialize";
else
cout<<"\nMouse Succesfully Initialize";
}
void main ( )
{
detectmouse (); //function calling
getch ();
}
Showing Mouse:-
Now first we show mouse on screen .

Mouse works both in text mode and graphic


mode.

In text mode it looks like a square while in


graphics mode it looks like a pointer.

Here is a screen shot for Text Mode &


Graphics Mode.
Showing Mouse Pointer text
mode :-
#include <dos.h>
union REGS in, out;
void detectmouse ()
{
in.x.ax = 0;
int86 (0X33,&in,&out);
if (out.x.ax == 0)
cout<<"\nMouse Fail To Initialize";
else
cout<<"\nMouse Succesfully Initialize";
}
void showmousetext ()
{
in.x.ax = 1; //Input
int86 (0X33,&in,&out); //Invoke intrrupt
}
void main ()
{
detectmouse ();
showmouse ();
getch ();
}
Showing Mouse in Graphics mode:-
#include <dos.h>
union REGS in, out;
void detectmouse ()
{
in.x.ax = 0;
int86 (0X33,&in,&out);
if (out.x.ax == 0)
cout<<"\nMouse Fail To Initialize";
else
cout<<"\nMouse Succesfully Initialize";
}
void showmousegraphic ()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
in.x.ax = 1; //Input
int86 (0X33,&in,&out); //Invoke intrrupt
}
void main ()
{
detectmouse ();
showmousegraphic ();
getch ();
}
Mouse Hiding :-

void hidemouse ( )
{
in.x.ax = 2;
int86 (0X33,&in,&out);
}
Detect Mouse Key:-
void detect ()
{
while ( !kbhit () )
{
in.x.ax = 3;
int86 (0X33,&in,&out);
if (out.x.bx == 1)
cout<<"Left";
if (out.x.bx == 2)
cout<<"Right";
delay (200); /* Otherwise due to quick computer response
100s of words will get print */

}
}
Detecting coordinate :-
void detect ()
{
while (!kbhit () )
{
int x, y;
in.x.ax = 3;
int86 (0X33,&in,&out);
x = out.x.cx; y = out.x.dx;
if (out.x.bx == 1)
{
cout<<"\n Left \n";
}
if (out.x.bx == 2)
{
cout<<"\n Right \n”;
}
cout<<“X= "<<x<<"\n Y= "<<y;
delay (200);
}
}
MOUSE RESTRICT
• We can restrict the mouse in
particular rectangle.We create a
function called restrict which takes
four parameters,and two cartisian
points each containing X coordinate
and Y coordinate.

• First point mention the top of the


rectangle and the second point
mention the bottom of the rectangle.
Mouse Restrict :-
void restrict (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);
}
Tower of Hanoi
• There are three towers

• 3 disks, with decreasing sizes, placed


on the first tower

• You need to move all of the disks


from the first tower to the second
tower

• Larger disks can not be placed on top


of smaller disks
Recursive Algorithm
void Hanoi(int n, string a, string
b, string c)
{
if (n == 1) /* base case */
Move(a,b);
else { /* recursion */
Hanoi(n-1,a,c,b);
Move(a,b);
Hanoi(n-1,c,b,a);
}
Recursive Solution
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi

You might also like