有没有 .contains 就像 C# 上的 Java 函数一样
本文关键字:一样 函数 Java contains 就像 上的 有没有 | 更新日期: 2023-09-27 18:31:58
我的意思是graphic2D包含检查鼠标e.X和e.Y是否在该图形内被单击的功能。 由于它在 Java 上可用,我想知道 C# 上是否有类似的东西
你可以在 C# 中做这样的事情
List<ShapeObj> ShapeObj_list; //The list of objects drawn.
private void OnMouseDown(object sender, MouseEventArgs e)
{
foreach(ShapeObj obj in ShapeObj_list)
{
if(obj.InsideTheObject(e.X, e.Y))
{
//Do Something
}
}
}
在类中,ShapeObj实现了函数InsideTheObject:
public class ShapeObj
{
public Point Location { get; set; }
public Size Size {get; set; }
public bool InsideTheObject(int x, int y)
{
Rectangle rc = new Rectangle(this.Location, this.Size);
return rc.Contains(x, y);
}
}