Clickable Image Map c#

 ̄綄美尐妖づ 提交于 2019-12-10 13:48:34

问题


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?


回答1:


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.




回答2:


At CodeProject, someone has created an ImageMap Control for this very purpose. I imagine others are available.



来源:https://stackoverflow.com/questions/6392450/clickable-image-map-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!