Industrial Training




Building Mouse Cursors page-2



Building Mouse Cursors

main( )

{

int gd = DETECT, gm, button, x, y, area, i, choice ;

char *p ;

initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;

if ( initmouse( ) == 0 )

{

closegraph( ) ;

puts ( "Mouse not installed!" ) ;

exit ( 1 ) ;

}

for ( i = 0 ; i <4 ; i++ )

{

changecursor ( c[i] ) ;

showmouseptr( ) ;

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

area = imagesize ( x - 15, y - 7, x + 32, y + 24 ) ;

p = malloc ( area ) ;

if ( p == NULL )

exit( ) ;

getimage ( x - 15, y - 7, x + 32, y + 24, p ) ;

putimage ( i * 48 + 1, 1, p, COPY_PUT ) ;

rectangle ( i * 48, 0, ( i + 1 ) * 48, 33 ) ;

hidemouseptr ( ) ;

}

gotoxy ( 10, 25 ) ;

printf ( "Press any key to exit..." ) ;

choice = 1 ;

disp ( choice, p ) ;

changecursor ( c[choice - 1] ) ;

showmouseptr( ) ;

while ( !kbhit( ) )

{

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

if ( ( button & 1 ) == 1 )

{

for ( i = 0 ; i <4 ; i++ )

{

if ( choice - 1 == i )

continue ;

if ( y > 0 && y <33 )

{

if ( x > i * 48 && x <( i + 1 ) * 48 )

{

hidemouseptr( ) ;

disp ( choice, p ) ;

choice = i + 1 ;

disp ( choice, p ) ;

changecursor ( c[choice - 1] ) ;

showmouseptr( ) ;

}

}

}

}

}

getch( ) ;

}

disp ( int choice, char *p )

{

getimage ( ( choice - 1 ) * 48 + 1, 1, choice * 48, 32, p ) ;

putimage ( ( choice - 1 ) * 48 + 1, 1, p, NOT_PUT ) ;

}

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 ) ;

}

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 ;

}


Freehand Drawing Using Mouse

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, CorelDraw 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 )

{

hidemouseptr( ) ;

prevx = x ;

prevy = y ;

while ( ( button & 1 ) == 1 )

{

line ( prevx, prevy, x, y ) ;

prevx = x ;

prevy = y ;

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

}

showmouseptr( ) ;

}

}

}

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 ) ;

}

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 ;

}

In main( ) the loop while ( !kbhit( ) ) allows the drawing of as many freehand drawings as the user desires, the process stopping when the user hits a key from the keyboard. The freehand drawing begins on hitting the left mouse button, and grows as the mouse is dragged with the left button depressed. On releasing the left button that freehand drawing is terminated. When the actual drawing is in progress the mouse pointer is hidden, and it reappears when the mouse button is released.



Hi I am Pluto.