registermousehandler


Syntax

#include "graphics.h"

void registermousehandler(int kind, void h(int, int));

Description[WIN]
The registermousehandler function is available in the winbgim implementation of BGI graphics. In general, you write a different "handler function" to handle each different kind of mouse event, and you "register" each of your handlers by calling registermousehandler. The first argument to registermousehandler is one of these constants from the graphics.h file:
WM_MOUSEMOVE
if you want the handler called whenever the mouse moves
WM_LBUTTONDBLCLK
...called whenever the left mouse button is double clicked
WM_LBUTTONDOWN
...called whenever the left mouse button is clicked down
WM_LBUTTONUP
...called whenever the left mouse button is released up
WM_MBUTTONDBLCLK
...called whenever the middle mouse button is double clicked
WM_MBUTTONDOWN
...called whenever the middle mouse button is clicked down
WM_MBUTTONUP
...called whenever the middle mouse button is released up
WM_RBUTTONDBLCLK
...called whenever the right mouse button is double clicked
WM_RBUTTONDOWN
...called whenever the right mouse button is clicked down
WM_RBUTTONUP
...called whenever the right mouse button is released up
The second argument to registermousehandler must be the name of the handler function that you wrote. This function must be a void function with two int parameters. Whenever the specified mouse event occurs, your handler will be called and the two int parameters will be the x and y positions where the event happened.

The middle mouse button handlers aren't working on my machine. I haven't yet tracked down the reason--it could be a broken mouse or it could be a bug in my programming.

Return Value
None.

See also
mousex
mousey

Example

/* mouse example */ 

#include "graphics.h"



// The click_handler will be called whenever the left mouse button is

// clicked. It checks copies the x,y coordinates of the click to

// see if the click was on a red pixel. If so, then the boolean

// variable red_clicked is set to true. Note that in general

// all handlers should be quick. If they need to do more than a little

// work, they should set a variable that will trigger the work going,

// and then return.

bool red_clicked = false;

void click_handler(int x, int y)

{

    if (getpixel(x,y) == RED)

        red_clicked = true;

}



// Call this function to draw an isosoles triangle with the given base and

// height. The triangle will be drawn just above the botton of the screen.

void triangle(int base, int height)

{

    int maxx = getmaxx( );

    int maxy = getmaxy( );



    line(maxx/2 - base/2, maxy - 10, maxx/2 + base/2, maxy - 10);

    line(maxx/2 - base/2, maxy - 10, maxx/2, maxy - 10 - height);

    line(maxx/2 + base/2, maxy - 10, maxx/2, maxy - 10 - height); 

}



void main(void)

{

    int maxx, maxy;  // Maximum x and y pixel coordinates

    int divisor;     // Divisor for the length of a triangle side



    // Put the machine into graphics mode and get the maximum coordinates:

    initwindow(450, 300);         

    maxx = getmaxx( );

    maxy = getmaxy( );



    // Register the function that handles a left mouse click

    registermousehandler(WM_LBUTTONDOWN, click_handler);

    

    // Draw a white circle with red inside and a radius of 50 pixels:

    setfillstyle(SOLID_FILL, RED);

    setcolor(WHITE);

    fillellipse(maxx/2, maxy/2, 50, 50);

    

    // Print a message and wait for a red pixel to be double clicked:

    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);

    outtextxy(20, 20, "Left click in RED to end.");

    setcolor(BLUE);

    red_clicked = false;

    divisor = 2;

    while (!red_clicked)

    {

        triangle(maxx/divisor, maxy/divisor);

        delay(500);

        divisor++;

    }



    cout << "The mouse was clicked at: ";

    cout << "x=" << mousex( );

    cout << " y=" << mousey( ) << endl;

    

    // Switch back to text mode:

    closegraph( );

}


Back to index