How can I make the rectangles clickable, C#

倾然丶 夕夏残阳落幕 提交于 2019-12-10 22:47:58

问题


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.


回答1:


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

    }
}



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/11260397/how-can-i-make-the-rectangles-clickable-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!