Mouse Programming
Mouse Programming
21
155
156
Let Us C
157
Let Us C
158
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 ) ;
}
159
160
Let Us C
Interrupt
33h
Service
0
33h
33h
33h
33h
33h
33h
Purpose
Reset mouse and get status
Call with: AX = 0
Returns:
AX = FFFFh If mouse support is available
AX = 0 If mouse support is not available
Show mouse pointer
Call with: AX = 1
Returns: Nothing
Hide mouse pointer
Call with: AX = 2
Returns: Nothing
Get mouse position and button status
Call with: AX = 3
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
Set mouse pointer position
Call with: AX = 4
CX = x coordinate
DX = y coordinate
Returns: Nothing
Set horizontal limits for mouse pointer
Call with: AX = 7
CX = minimum x coordinate
DX = maximum x coordinate
Returns: Nothing
Set vertical limits for mouse pointer
Call with: AX = 8
CX = minimum y coordinate
161
Let Us C
162
DX = maximum y coordinate
Figure 21.1
Then the control enters 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 on the following page.
163
Let Us C
164
x2 = sx ;
}
else
{
x1 = sx ;
x2 = x ;
}
if ( y < sy )
{
y1 = y ;
y2 = sy ;
}
else
{
y1 = sy ;
y2 = y ;
}
setcolor ( WHITE ) ;
save ( x1, y1, x2, y2 ) ;
rectangle ( x1, y1, x2, y2 ) ;
}
getmousepos ( &button, &tx, &ty ) ;
}
restore ( x1, y1 ) ;
showmouseptr( ) ;
}
} while ( ( button & 2 ) != 2 ) ;
gotoxy ( 1, 1 ) ;
printf ( "Press any key to exit" ) ;
gotoxy ( 60, 1 ) ;
printf ( " " ) ;
getch( ) ;
165
166
putimage ( midx + 1, midy + 1, p4, OR_PUT ) ;
farfree ( p1 ) ;
farfree ( p2 ) ;
farfree ( p3 ) ;
farfree ( p4 ) ;
}
initmouse( )
{
i.x.ax = 0 ;
int86 ( 0x33, &i, &o ) ;
return ( o.x.ax ) ;
}
showmouseptr( )
{
i.x.ax = 1 ;
int86 ( 0x33, &i, &o ) ;
}
hidemouseptr( )
{
i.x.ax = 2 ;
int86 ( 0x33, &i, &o ) ;
}
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 ) ;
Let Us C
167
}
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 ;
}
Let Us C
168
1111111111111111
0000000000000000
1000000000000001
0000000000000000
1111111111111111
0000000000000000
1000000000000001
0000000000000000
0100000000000010
1000000000000001
0010000000000100
1100000000000011
0000100000010000
1111000000001111
0000001001000000
1111110000111111
0000001001000000
1111110000111111
0000100000010000
1111000000001111
0010000000000100
1100000000000011
0100000000000010
1000000000000001
1000000000000001
0000000000000000
1111111111111111
0000000000000000
1000000000000001
0000000000000000
1111111111111111
0000000000000000
Screen Mask
Figure 21.2
#include "graphics.h"
169
170
Let Us C
}
initmouse( )
{
i.x.ax = 0 ;
int86 ( 0x33, &i, &o ) ;
return ( o.x.ax ) ;
}
showmouseptr( )
{
i.x.ax = 1 ;
int86 ( 0x33, &i, &o ) ;
}
changecursor ( int *shape )
{
i.x.ax = 9 ; /* service number */
i.x.bx = 0 ; /* actual cursor position from left */
i.x.cx = 0 ; /* actual cursor position from top */
i.x.dx = ( unsigned ) shape ; /* offset address of pointer image */
segread ( &s ) ;
s.es = s.ds ; /* segment address of pointer */
int86x ( 0x33, &i, &i, &s ) ;
}
171
Let Us C
172
173
174
Let Us C
Let us now try to gather all that we know about the mouse and its
functions to develop a utility for drawing freehand... the way it is
done in softwares like Paintbrush, Corel Draw etc. Here is the
program...
#include "dos.h"
#include "graphics.h"
union REGS i, o ;
main( )
{
int gd = DETECT, gm, maxx, maxy, x, y, button, prevx, prevy ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;
maxx = getmaxx( ) ;
maxy = getmaxy( ) ;
rectangle ( 0, 0, maxx, maxy ) ;
setviewport ( 1, 1, maxx - 1, maxy - 1, 1 ) ;
if ( initmouse( ) == 0 )
{
closegraph( ) ;
restorecrtmode( ) ;
printf ( "Mouse driver not loaded" ) ;
exit ( 1 ) ;
}
restrictmouseptr ( 1, 1, maxx - 1, maxy - 1 ) ;
showmouseptr( ) ;
while ( !kbhit( ) )
{
getmousepos ( &button, &x, &y ) ;
if ( ( button & 1 ) == 1 )
{
175
176
Let Us C
{
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 ) ;
}
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 ;
}
177
work for all memory models and for different types of adapters
like CGA, EGA, VGA etc.
#include "dos.h"
#include "graphics.h"
#include "alloc.h"
char *menu[ ] = { "Samosa", "Sambarwada", "Dahiwada", "Exit" } ;
union REGS i, o ;
main( )
{
int gd = DETECT, gm, choice = 1, bill = 0, width = 0, i, count ;
char **buffer ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;
if ( initmouse( ) == 0 )
{
printf ( "\nUnable to initialise Mouse... " ) ;
exit( ) ;
}
count = sizeof ( menu ) / sizeof ( char * ) ;
settextstyle ( TRIPLEX_FONT, 0, 3 ) ;
displaymenu ( menu, count, 100, 100 ) ;
for ( i = 0 ; i < count ; i++ )
{
if ( textwidth ( menu[i] ) > width )
width = textwidth ( menu[i] ) ;
}
buffer = malloc ( sizeof ( menu ) ) ;
savemenu ( menu, buffer, width, count, 100, 100 ) ;
while ( choice != 4 )
Let Us C
178
{
179
Let Us C
180
if ( ( button & 1 ) == 1 )
{
while ( ( button & 1 ) == 1 )
getmousepos ( &button, &x, &y ) ;
181
182
Let Us C
Exercise
[A] State True or False:
(a) All mouse services have been gather under interrupt number
51.
(b) It is possible to build mouse pointer in graphics mode of size
bigger then 16x16 pixel.
(c) It is mandatory that the mouse driver should stand loaded
before we can use the mouse.
(d) We can build the arrow shape mouse pointer in text mode.
(e) We can build a color mouse pointer through interrupt number
33h.
(f) To find out the status of the different mouse buttons, we have
to use different services under interrupt number 33h.
(g) The service to initialize the mouse also manages to display the
mouse pointer.
183
(a) Write a function that would not allow the mouse pointer to
enter a particular rectangular area on the screen. Use the
following details:
Interrupt number 33h
AX 16, service to deactivate mouse when it enters rectangle defined
by CX, DX, SI, DI
CX upper X screen coordinate
DX upper Y screen coordinate
SI lower X screen coordinate
DI lower Y screen coordinate
(b) Write a function that allows a user to set the sensitivity of the
mouse. The sensitivity is measured in a unit called mickeys
where 1 mickey represents approximately 1 / 200 of an inch
of mouse travel on the table. Effectively it means how much
movement of the mouse on the table would cause the mouse
cursor to move by one unit on the screen, where one unit
corresponds to 8 pixels. Use the following details:
Interrupt number 33h
AX 1Ah
BX = horizontal mickeys (1 to 32767, default = 8)
CX = vertical mickeys (1 to 32767, default = 16)
184
Let Us C
185