简体   繁体   English

如何使矩形可点击,C#

[英]How can I make the rectangles clickable, C#

The code can generate rectangles (Rectangle rectangle) at runtime. 该代码可以在运行时生成矩形(Rectangle矩形)。 The position of rectangles may change according to users' choices. 矩形的位置可能会根据用户的选择而变化。

I want to add code in the method where it creates rectangles to make the rectangles clickable. 我想在其中创建矩形以使矩形可单击的方法中添加代码。 And after user clicking the rectangle, there will be a new window to show content just like text. 用户单击矩形后,将出现一个新窗口,以显示与文本一样的内容。

You can use Contains method of the Rectangle object. 您可以使用Rectangle对象的Contains方法。

private Rectangle _myRectangle;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (this._myRectangle.Contains(e.Location))
    {

    }
}

Create a label control with the border property and transaparent background(so that it will look as rectangle) and add click event handler for each label you add. 创建一个带有border属性和透明背景的标签控件(这样它将看起来像矩形),并为您添加的每个标签添加click事件处理程序。 It will be good if you create your own Rectangle control by deriving from Label class or you can create your own control(Many other solutions). 如果您通过从Label类派生来创建自己的Rectangle控件,或者可以创建自己的控件(很多其他解决方案),那将是很好的。

我将考虑处理窗口本身(或任何“背景”控件是)上的click事件,获取其坐标,并将其与矩形的已知位置/大小进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM