如何在C#中使用鼠标绘制矩形
我想用 C# 在窗体上绘制一个矩形。我阅读并找到了这篇文章。有可用的示例或教程吗?这篇文章没有多大帮助。
I want to draw a rectangle on a form in C#. I read and found this article. Are there any samples or tutorials available ? The article was not very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您链接的文章似乎是 C++,这可能解释了为什么它对您没有多大帮助。
如果为 MouseDown 和 MouseUp 创建事件,则应该具有矩形所需的两个角点。从那里开始,就是在表格上绘图的问题。 System.Drawing.* 可能应该是您的第一站。下面链接了一些教程:
使用 C# 在 WinForms 中绘制图形
使用 Winforms (StackOverflow) 绘制矩形
使用 C# 进行图形编程
The article you linked appears to be C++, which may explain why it didn't help you much.
If you create events for MouseDown and MouseUp, you should have the two corner points you need for a rectangle. From there, it's a matter of drawing on the form. System.Drawing.* should probably be your first stop. There are a couple of tutorials linked below:
Drawing with Graphics in WinForms using C#
Draw a rectangle using Winforms (StackOverflow)
Graphics Programming using C#
您需要这 3 个函数和变量:
其次,您需要创建一个鼠标按下事件:
第三,您需要实现鼠标向上事件:
并确保当您调整大小或移动表单时,矩形将在那里:
You need this 3 functions and variables:
Second you need to make a mouse down event:
Third, you need to implemente the mouse up event:
And to ensure that when you resize or move the form the rectangle will be there: