[英]How to draw an ellipse in a form when clicking in the form?
我知道这听起来很简单,但我从未在Visual Studio中使用过,但我无法理解。 我正在使用
private void Usecasediagram_Paint_elipse(object sender, PaintEventArgs e)
{
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Green, 5);
Rectangle myRectangle = new Rectangle(100, 100, 250, 200);
graphicsObj.DrawEllipse(myPen, myRectangle);
}
在代码运行时绘制此椭圆,但我希望仅当我单击表单中的某个位置并在鼠标位置显示此圆时才显示它。 我已经可以使用表单的click方法了,但是我不知道如何调用该函数,例如在PaintEventArgs中传递的内容...
存储有关您要在Form / Class级别上绘制的内容的信息,并使用Paint()事件通过e.Graphics
提供自己的Graphics
如果要一个椭圆,则:
public partial class Form1 : Form
{
private Point DrawEllipseAt;
private bool DrawEllipse = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint1;
this.Click += Form1_Click;
}
private void Form1_Click(object sender, EventArgs e)
{
this.DrawEllipseAt = this.PointToClient(Cursor.Position);
this.DrawEllipse = true;
this.Invalidate();
}
private void Form1_Paint1(object sender, PaintEventArgs e)
{
if (this.DrawEllipse)
{
Graphics G = e.Graphics;
Rectangle myRectangle = new Rectangle(DrawEllipseAt, new Size(0, 0));
myRectangle.Inflate(new Size(125, 100));
using (Pen myPen = new Pen(System.Drawing.Color.Green, 5))
{
G.DrawEllipse(myPen, myRectangle);
}
}
}
}
如果要多个椭圆:
public partial class Form1 : Form
{
private List<Point> DrawEllipsesAt = new List<Point>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint1;
this.Click += Form1_Click;
}
private void Form1_Click(object sender, EventArgs e)
{
this.DrawEllipsesAt.Add(this.PointToClient(Cursor.Position));
this.Invalidate();
}
private void Form1_Paint1(object sender, PaintEventArgs e)
{
Graphics G = e.Graphics;
if (this.DrawEllipsesAt.Count > 0)
{
using (Pen myPen = new Pen(System.Drawing.Color.Green, 5))
{
foreach (Point pt in this.DrawEllipsesAt)
{
Rectangle myRectangle = new Rectangle(pt, new Size(0, 0));
myRectangle.Inflate(new Size(125, 100));
G.DrawEllipse(myPen, myRectangle);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.