yoh*_*hna 4 .net c# graphics windows-forms-designer
我想画一个矩形.我想要的是向用户显示鼠标事件上的矩形.
就像在图像中.这适用于C#.net Forms应用程序.
帮助我实现这一目标.任何帮助表示赞赏.
谢谢Yohan
您可以分三步完成:
你可以做这样的事情(在你的Form):
public class Form1
{
Rectangle mRect;
public Form1()
{
InitializeComponents();
//Improves prformance and reduces flickering
this.DoubleBuffered = true;
}
//Initiate rectangle with mouse down event
protected override void OnMouseDown(MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
//check if mouse is down and being draged, then draw rectangle
protected override void OnMouseMove(MouseEventArgs e)
{
if( e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
this.Invalidate();
}
}
//draw the rectangle on paint event
protected override void OnPaint(PaintEventArgs e)
{
//Draw a rectangle with 2pixel wide line
using(Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, mRect);
}
}
}
Run Code Online (Sandbox Code Playgroud)
稍后如果要检查按钮(如图所示)是否为矩形,可以通过检查按钮的区域并检查它们是否位于绘制的矩形中来实现.
| 归档时间: |
|
| 查看次数: |
15949 次 |
| 最近记录: |