Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable Image Map c#

I'm making a C# inventory application. However, I want there to be a map where the user can click several areas and interact with the inventory on that certain area.

Currently I photoshopped the map and made it the background of the form. I'm planning on putting pictureboxes over the different areas and code manually the mouse over, click, and mouse down events to give the button appearance.

Anyway, my question is, is this a good idea? Should I just load the map into a picturebox, get rid of the buttonish visual effects and track the coordinates of the click?

like image 997
barbara Avatar asked Nov 05 '22 19:11

barbara


1 Answers

While I don't think this is a bad idea, one alternative would be to use Rectangles and have an onclick function consisting of a series of Rectangle.Contains(Point) to find out if the point clicked by the mouse is contained in any of the clickable areas.

E.G.

Rectangle R1 = new Rectangle(/*Passed Variables*/);
Rectangle R2 = new Rectangle(/*Passed Variables*/);
//...
OnClick(object sender, MouseEventArgs e)
 {
 If (R1.Contains(e.Location))
  {
   //Stuff
  }
 else
 {
  If (R2.Contains(e.Location))
   {
    //Stuff
   }
 }
}

If you have a larger list of Rectangle objects, though, you could always use an array of Rectangles and a for loop for a more efficient way of checking if the clicked location is inside any Rectangle.

like image 90
Edward Black Avatar answered Nov 09 '22 05:11

Edward Black