Industrial Training




Interaction With Mouse



Graphical User Interfaces (GUIs) and mouse go hand in hand. Though some GUIs do exist which manage the show without a mouse, the mouse has more or less become a standard input device with any GUI worth its name. A mouse is used to point at the icons which form the menu in a GUI - much like the way a child points to something he wants. These point-and-shoot menus of GUI bring along ease and convenience along with all the added agility of the real life look alike of the mouse. As a result, more and more packages today are not only menu driven, but also mouse driven. The use of a mouse requires a program to sense its presence. Just attaching the mouse to the computer is not enough. What we also need to do is load a device driver program that understands the mouse. A device driver is a program which senses the signals coming from the port to which the mouse is attached. On sensing th

e signals, the driver translates these into the related action on the screen. This device driver is usually available in a program called MOUSE.COM or WITTYMS.COM, which work with different variety of mice. The mouse has a separate cursor (often called a mouse pointer ) which looks like an arrow and functions in the same way as the normal cursor. As we move the mouse, the mouse pointer moves correspondingly. It is just like using arrow keys. The only difference being the speed at which the mouse cursor moves is much faster than that of an ordinary cursor. If desired, we can even change the speed of the mouse pointer, and even its shape. Once the mouse driver is loaded, the various mouse functions can be accessed by setting up the AX register with different values (service numbers) and issuing interrupt number 0x33. Some of these functions are used in the following program.

Mouse Demonstration Program
#include "dos.h" 
#include "graphics.h"
union REGS i, o ; 
main( ) 

int gd = DETECT, gm, maxx, maxy, x, y, button ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ; 
maxx = getmaxx( ) ;  
maxy = getmaxy( ) ;
rectangle ( 0, 56, maxx, maxy ) ; 
setviewport ( 1, 57, maxx - 1, maxy - 1, 1 ) ;
gotoxy ( 26, 1 ) ;  
printf ( "Mouse Demonstration Program" ) ;
if ( initmouse( ) == 0 ) 

closegraph( ) ;   
restorecrtmode( ) ; 
printf ( "\nMouse driver not loaded" ) ; 
exit ( 1 ) ; 
}
restrictmouseptr ( 1, 57, maxx - 1, maxy - 1 ) ; 
showmouseptr( ) ; 
gotoxy ( 1, 2 )  ;   
printf ( "Left Button" ) ; 
gotoxy ( 15, 2 ) ;   
printf ( "Right Button" ) ; 
gotoxy ( 55, 3 ) ;   
printf ( "Press any key to exit..." ) ;
while ( !kbhit( ) ) 

getmousepos ( &button, &x, &y ) ;
gotoxy ( 5, 3 ) ; 
( button & 1 ) == 1 ? printf ( "DOWN" ) : printf ( "UP  " ) ;
gotoxy ( 20, 3 ) ; 
( button & 2 ) == 2 ? printf ( "DOWN" ) : printf ( "UP  " ) ;
gotoxy ( 65, 2 ) ; 
printf ( "X = %03d  y = %03d", x, y ) ; 

}

/* initialises mouse */ 
initmouse( )   

i.x.ax = 0 ; 
int86 ( 0x33, &i, &o ) ; 
return ( o.x.ax ) ; 
}

/* displays mouse pointer */ 
showmouseptr( )   

i.x.ax = 1 ; 
int86 ( 0x33, &i, &o ) ; 
}

/* restricts mouse movement */ 
restrictmouseptr ( 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 ) ; 
}

/* gets mouse coordinates and button status */ 
getmousepos ( 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 ; 


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. 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 is loaded, initgraph( ) sets up the numeric values of the graphics driver and the graphics mode chosen in the variables gd and gm respectively. Here we are assuming the driver files are in the directory c:\tc\bgi. Thus the path passed to initgraph( ) is "c:\\tc\\bgi". Once we are into the graphics mode we call the functions getmaxx( ) and getmaxy( ) to obtain the maximum x and y coordinates in the current graphics mode. Then we draw a rectangle using the function rectangle( ) and set the viewport area which restricts any drawing activity within the viewport. Next we call the function initmouse( ) to initialise the mouse. It checks if the mouse driver has been loaded or not (by issuing interrupt 33h, service number 0) and then reports the status to main( ). If mouse is not initialised successfully then the closegraph( ) function unloads the graphics driver and restorecrtmode( ) takes the screen back to the mode that existed prior to the calling of initgraph( ), which in our case is the text mode. If you have loaded the mouse driver successfully then in all probability the mouse would be successfully initialised. Most softwares today provide windowing feature. Not just windows on the screen as boxes, but windows that restrict cursor movement. Like a screen within a screen. If we define the size of the window then we can make sure that the cursor only moves within the window. This is what programs like Windows and even DBMSs like Foxpro and dBase IV give us. There is nothing too difficult about it. It's just a matter of putting the right values in the right registers. In our program this has been achieved by the function restrictmouseptr( ). Next, another function called showmouseptr( ) is called. It actually displays the mouse pointer on the screen. Both the tasks of restricting the mouse pointer movement and displaying the mouse pointer are achieved by invoking appropriate services available under interrupt number 33h. In fact once the mouse driver has been loaded, anything to be done with the mouse is always done by some service or the other available under interrupt 33h. Then we enter a while loop where we check to see which button has been pressed and accordingly display either UP or DOWN. Additionally the current coordinates of the mouse pointer are also displayed. If a key is hit from the keyboard we exit the loop. Details of some of the more commonly used mouse services are given below:



Interrupt

Service

Service

33h

0

Reset mouse and get status
Call with: AX = 0000h
Returns:
AX = FFFFh If mouse support is available
AX = 0000h If mouse support is not available

33h

1

Show mouse pointer
Call with: AX = 0001h
Returns: Nothing

33h

2

Hide mouse pointer
Call with: AX = 0002h
Returns: Nothing

33h

3

Get mouse position and button status
Call with: AX = 0003h
Returns: BX = mouse button status
Bit Significance
0 left button is down
1 right button is down
2 center button is down
CX = x coordinate
DX = y coordinate

33h

4

Set mouse pointer position
Call with: AX = 0004h
CX = x coordinate
DX = y coordinate
Returns: Nothing

33h

7

Set horizontal limits for pointer
Call with: AX = 0007h
CX = minimum x coordinate
DX = maximum x coordinate
Returns: Nothing

33h

8

Set vertical limits for pointer
Call with: AX = 0008h
CX = minimum y coordinate
DX = maximum y coordinate



Hi I am Pluto.