Question

Is it possible to make 9 buttons from this picture without separating them. I tryed inserting 9 defferent pictureboxes, but they overlap. I also tryed to set transparent background and change parents. Any suggestions? 8 buttons

Was it helpful?

Solution

As Rodrigo Silva's comment suggested, an "image map" made from a single PictureBox is probably the best way to go here. Place the entire image inside the PictureBox. Register a Click event handler. Inside the handler, you can get the mouse coordinates with MousePosition.X and MousePosition.Y.

To determine which region was clicked, the way I'd approach it is to calculate the angle of the mouse click relative to the center of the picture. Then test which of the 8 sectors the click fell in (perhaps also with a "radius" check to make sure the click wasn't in the center region or outside the outer edge). Essentially:

double cx; // set to center X coordinate of picture
double cy; // set to center Y coordinate of picture

double x = MousePosition.X;
double y = MousePosition.Y;

double angle = Math.Atan2(y - cy, x - cx);

// convert from radians to degrees
angle *= 180 / Math.PI;

double distance = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));

if (distance > min_distance && distance <= max_distance)
{
    if (angle >= 0 && angle < 45) { /* ... */ }
    else if (angle < 90) { /* ... */ }
    // etc.
}

Of course you can use any other method you wish to determine what region was clicked based on the mouse coordinates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top