我知道这听起来很简单,但我从未使用过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中传递的内容...
答案 0 :(得分:2)
存储有关您要在表单/班级中绘制的内容的信息,并使用Paint() Event通过Graphics
提供自己的e.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);
}
}
}
}
}