c# - 我制作了一个矩形如何检查鼠标是否点击了它?

标签 c# windows-forms-designer hittest drawrectangle

如何检查鼠标是否点击了矩形?

Graphics gfx;
Rectangle hitbox;
hitbox = new hitbox(50,50,10,10);
//TIMER AT THE BOTTOM
gfx.Draw(System.Drawing.Pens.Black,hitbox);

最佳答案

如果您的“gfx”是来自表单的“e.Graphics...”,只是一个快速而肮脏的示例:

  public partial class Form1 : Form
  {
    private readonly Rectangle hitbox = new Rectangle(50, 50, 10, 10);
    private readonly Pen pen = new Pen(Brushes.Black);

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      e.Graphics.DrawRectangle(pen, hitbox);
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
      if ((e.X > hitbox.X) && (e.X < hitbox.X + hitbox.Width) &&
          (e.Y > hitbox.Y) && (e.Y < hitbox.Y + hitbox.Height))
      {
        Text = "HIT";
      }
      else
      {
        Text = "NO";
      }
    }
  }

关于c# - 我制作了一个矩形如何检查鼠标是否点击了它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319092/

相关文章:

c++ - MFC 的 CTabCtrl::HitTest 函数为单击的任何选项卡返回 "1"

c# - 将 JSON 对象映射到 C# 类属性

c# - 将自定义消息添加到单元测试结果

java - 使用 Java 创建按钮快捷方式,例如使用 Windows 窗体的 &(& 符号)

swift - 漏洞 : hit-testing with sibling nodes and the userInteractionEnabled property in Sprite Kit

c# - 使 VisualTreeHelper.HitTest 在单元测试中工作?

c# - 将 customDimensions 拆分为 3 个 json,然后使用 kusto 查询进行项目

c# - UWP 应用程序在 Visual Studio Debug模式下工作,但发布版本不会运行,缺少 DLL (C#)

c# - 在设计器中查看表单时出现错误 - 我该如何避免这种情况?

c# - 如何防止在设计时将控件拖出自定义控件?