PRODUCT : Borland C++ NUMBER : 724 VERSION : 2.0 OS : DOS DATE : October 19, 1993 PAGE : 1/4 TITLE : Example Mouse Event Handler /************************************************************** An example of using a mouse event handler. **************************************************************/ #include #include #include #define MOUSEINT geninterrupt( 0x33 ) /*-- mouse event masks (use with M_event) --*/ #define MOUSEMOVE 0x01 /* Use, for example: */ #define L_PRESS 0x02 /* L_PRESS | MOUSEMOVE */ #define L_RELEASE 0x04 #define R_PRESS 0x08 #define R_RELEASE 0x10 /*-- mouse button status macros --*/ #define L_DOWN ( M_buttonstatus & 0x01 ) #define R_DOWN ( M_buttonstatus & 0x02 ) #define HANDLER_EXIT_PROCESSING() \ __emit__( (unsigned char) 0x5D, \ (unsigned char) 0x5F, \ (unsigned char) 0x5E, \ (unsigned char) 0x1F, \ (unsigned char) 0x07, \ (unsigned char) 0x5A, \ (unsigned char) 0x59, \ (unsigned char) 0x5B, \ (unsigned char) 0x58, \ (unsigned char) 0xCB ) ; #if 0 ------------------------------------------- The above emitted code is equivalent to the following assembler code (but does not require the -B compiling option): asm pop bp ; asm pop di ; asm pop si ; asm pop ds ; asm pop es ; /* interrupt exit processing */ asm pop dx ; asm pop cx ; PRODUCT : Borland C++ NUMBER : 724 VERSION : 2.0 OS : DOS DATE : October 19, 1993 PAGE : 2/4 TITLE : Example Mouse Event Handler asm pop bx ; asm pop ax ; asm retf ; /* far return */ -------------------------------------------- #endif /*--Global mouse status variables --*/ int M_xpos, M_ypos, /* cursor location */ M_buttonstatus, /* bits 0-2 ON if button is down */ M_eventcount, /* # of times event has occurred */ M_event ; /* flags a mouse event */ /*-- Reset mouse -- returns # of buttons or 0 if problems --*/ int Mreset( void ) { _AX = 0 ; MOUSEINT ; return( _AX ? _BX : _AX ) ; } /*-- Show mouse cursor --*/ void Mshow( void ) { _AX = 1 ; MOUSEINT ; } /*-- Hide mouse cursor --*/ void Mhide( void ) { _AX = 2 ; MOUSEINT ; } /*-- Get mouse position and button status --*/ void Mpos( void ) { _AX = 3 ; MOUSEINT ; M_xpos = _CX ; M_ypos = _DX ; M_buttonstatus = _BX ; } PRODUCT : Borland C++ NUMBER : 724 VERSION : 2.0 OS : DOS DATE : October 19, 1993 PAGE : 3/4 TITLE : Example Mouse Event Handler /*-- Installs mouse handler --*/ void Minsthandler( unsigned mask, void interrupt (*fn)(void) ) { union REGS reg ; struct SREGS seg ; reg.x.ax = 20 ; /* mouse fn. 20 -- replaces fn.12 */ reg.x.cx = mask ; reg.x.dx = FP_OFF( fn ) ; seg.es = FP_SEG( fn ) ; int86x( 0x33, ®, ®, &seg ) ; } /*--------------------------------------------------------- This mouse event handler simply updates mouse information. ----------------------------------------------------------*/ void interrupt mousehandler( void ) { M_event = _AX ; M_buttonstatus = _BX ; M_xpos = _CX ; M_ypos = _DX ; HANDLER_EXIT_PROCESSING() ; } /*---------------------------------------------------- Demonstrate mouse tracking via mouse event handler. ----------------------------------------------------*/ main() { clrscr() ; if( !Mreset() ) { fputs("Cannot initialize mouse.", stderr) ; exit(1) ; } Minsthandler( MOUSEMOVE, mousehandler ) ; M_event = 0 ; Mshow() ; gotoxy(20,24) ; cprintf("Text mode mouse tracking with event handler") ; gotoxy(20,25) ; cprintf(" Press any KEY to quit.") ; PRODUCT : Borland C++ NUMBER : 724 VERSION : 2.0 OS : DOS DATE : October 19, 1993 PAGE : 4/4 TITLE : Example Mouse Event Handler gotoxy( 1, 1) ; cprintf("Position and button status is updated only when the mouse moves.") ; gotoxy( 1, 4); cprintf("Button Status at time of handler call:") ; while( !kbhit() ) { if( M_event ) { gotoxy(1,2) ; cprintf("Cursor position: %3d, %3d", M_xpos/8+1, M_ypos/8+1 ) ; gotoxy(1,5) ; L_DOWN ? cprintf("Left DOWN") : cprintf("Left UP ") ; gotoxy(20,5) ; R_DOWN ? cprintf("Right DOWN") : cprintf("Right UP ") ; M_event = 0 ; /* reset the event */ } } Mhide() ; Mreset() ; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.