在创建可点击的WinForms C#影像图 [英] Creating clickable C# image map in winforms
问题描述
我试图开发出具有可点击影像地图为重点基本的C#WinForms应用程序。也就是说,图像当某一部分是可以点击的,将打开一个新形式。例如,图像可能是一个棋盘,并点击特定方将执行打开一个新形式的作用。
I'm trying to develop a basic C# winforms app with a clickable image map as its focus. That is, an image where a certain section is clickable and will open a new form. For example the image could be a chessboard, and clicking on a certain square will perform the action of opening a new form.
我希望这个概念是清楚的。我看了看S.O.类似的问题并且找不到一个可行的解决方案。
I hope this concept is clear. I've looked at similar questions on S.O. and could not find a workable solution.
干杯
推荐答案
我的解决办法是利用在任何控制保持图像的的MouseUp
事件。当你得到的点击,解决 e.Location
的位置下降到使用控制(的MouseUp给你 MouseEventArgs
) PointToClient()
。
My solution would be to leverage the MouseUp
event on whatever control holds the image. When you get that click, resolve the position of e.Location
(MouseUp gives you MouseEventArgs
) down to the control using PointToClient()
.
一旦你的点
,你只需要确定它是否是一个有效的长方形
,如果是,启动相应的表格。
Once you have that Point
, you need only determine if it's in a valid Rectangle
, and if it is, launch the appropriate form.
修改
您可以使用窗体设计器将控件添加到窗体,如面板
,然后一个事件处理程序添加到控制,如 OnMouseUp
。它看起来是这样的:
You can use the forms designer to add a control to the form, such as a Panel
and then add an event handler to that control, such as OnMouseUp
. It would look something like this:
private void panel1_MouseUp(Object sender, MouseEventArgs e)
{
//your code goes here
}
这是事件处理程序将被称为每当用户点击。您的面板,释放鼠标按钮
That event handler will be called whenever the user clicks on your panel and releases the mouse button.
在该事件处理程序,你可以有这样的事情:
In that event handler, you could have something like this:
if (e.Button.Equals(MouseButtons.Left))
{
Rectangle rect = new Rectangle(10, 10, 100, 100); //this is the zone you wish to react the user clicking on
if (rect.Contains(e.Location))
{
//show your form
}
}
e.Location
是怎么回事是鼠标的位置的时刻的按钮被释放时,在对应于所讨论的控制坐标,在此情况下 PANEL1
e.Location
is going to be the location of the mouse at the moment the button was released, in coordinates corresponding to the control in question, in this case panel1
.
您可能会想,如果有很多地区来考虑,如保持长方形
的列表或数组和遍历他们一个更好的解决方案。
You will probably want a more elegant solution if there are many regions to consider, such as keeping a list or array of Rectangle
and looping over them.
这篇关于在创建可点击的WinForms C#影像图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!