Thursday, 21 March 2013

Mouse Programming in C

This is simple tutorial to show you How you can  use Mouse  in your C program.
I have Attached a source file. This program is not developed by me. I found it on internet somewhere, But as this program is easy to understand I am using this program.

See Source code here.


To Enable Mouse in C program you need to generate Interrupt.
The interrupt number for Mouse input related service is 33h.



Files to include -> dos.h
Function from dos.h to be use ->int int86(int intno,union REGS *input , union Regs *output)
This Function Generates Software interrupts

union - REGS is defined in dos.h file
it is defined as

union REGS{
struct WORDREGS   x;
struct BYTEREGS     y;
};

WORDREGS  & BYTEREGS are defined as

struct WORDREGS{
unsigned int ax,bx,cx,dx;
unsigned int si,di,cflag,flags;
};

struct BYTEREGS{
unsigned int al,ah,bl,bh;
unsigned int cl,ch,dl,dh;
};


You don't need to remember all this stuff. This is just for understanding.
If you have studied any  Micro Controller subject then you can easily identify that these ax,bx... indicates Registers.

So, let me first tell  what we exactly gonna do.

For every type of event there are Software interrupt number are defined.
for example using keyboard, sound, mouse.
Here for mouse interrupt number is 33h.
and for all types of operation like show mouse pointer, initialize mouse, restrict mouse pointer on screen etc.. we have different Service numbers.

Generally service number will be provided in
ax register,
so here we will write something like  i.x.ax

now all the input information will be provided in   variable of type REGS, and we will get out put in similar union varible

In our program we have declared
union i,o;
we will use
 i as input 
o as output, 

so this are some of service numbers for mouse



0~ Mouse Reset/Get Mouse Installed Flag
1~ Show Mouse Cursor
2~ Hide Mouse Cursor
3~ Get Mouse Position and Button Status
4~ Set Mouse Cursor Position
5~ Get Mouse Button Press Information
6~ Get Mouse Button Release Information
7~ Set Mouse Horizontal Min/Max Position
8~ Set Mouse Vertical Min/Max Position

This is example who you  can use this  service.

To initialize mouse

i.x.ax=0;
int86(33h,&i,&o);

done !!


to Restrict mouse pointer between square (10,10) to (20,20)

        i.x.ax=7;
 i.x.cx=10;
 i.x.dx=20;
 int86(0x33,&i,&o);

 i.x.ax=8;
 i.x.cx=10;
 i.x.dx=20;
 int86(0x33,&i,&o);

very simple right?
now Go through source code and  try your self.

1 comment: