How can I make the rectangles clickable, C#

2019-07-17 18:56发布

The code can generate rectangles (Rectangle rectangle) at runtime. 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.

3条回答
Viruses.
2楼-- · 2019-07-17 19:14

I would consider handling the click event on the window itself (or whatever your "background" control is), getting its coordinates, and comparing those to the known locations/sizes of your rectangles.

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-17 19:22

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. 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).

查看更多
太酷不给撩
4楼-- · 2019-07-17 19:31

You can use Contains method of the Rectangle object.

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

    }
}
查看更多
登录 后发表回答