我想在的WinForms绘制椭圆用鼠标事件,
当鼠标向下,移动和向上 我的代码是这样的:形式闪烁的WinForms
void panel1_MouseDown(object sender, MouseEventArgs e)
{
startpoint.X = e.X;
startpoint.Y = e.Y;
if (m_drawrectiangle == true)
{
m_shape = new Rectiangle(0, 0, this);
m_shape.Xpos = e.X;
m_shape.Ypos = e.Y;
draw = true;
}
if (m_draweliipse == true)
{
m_shape = new Circle(0, 0, this);
m_shape.Xpos = e.X;
m_shape.Ypos = e.Y;
draw = true;
}
}
void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (m_shape != null)
{
if(m_shape.Area()==0)
{
this.Cursor = Cursors.Default;
draw = false;
}
if (draw == true)
{
shapes.Add(m_shape);
this.Cursor = Cursors.Default;
draw = false;
Invalidate();
}
}
}
void panel1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = "(" + e.X + ", " + e.Y + ")";
if (draw==true && m_drawrectiangle==true)
{
int x = (e.X < 0) ? 0 : e.X;
int y = (e.Y < 0) ? 0 : e.Y;
//switch places
m_shape.Xpos = (x < startpoint.X) ? x : startpoint.X;
m_shape.Ypos = (y < startpoint.Y) ? y : startpoint.Y;
((Rectiangle)m_shape).Width = Math.Abs(x - startpoint.X);
((Rectiangle) m_shape).Height = Math.Abs(y - startpoint.Y);
Invalidate(); //re-paint
}
if (draw==true && m_draweliipse==true)
{
int x = (e.X < 0) ? 0 : e.X;
int y = (e.Y < 0) ? 0 : e.Y;
//switch places
m_shape.Xpos = (x < startpoint.X) ? x : startpoint.X;
m_shape.Ypos = (y < startpoint.Y) ? y : startpoint.Y;
double deltax=Math.Pow(x-startpoint.X,2);
double deltay=Math.Pow(y-startpoint.Y,2);
((Circle)m_shape).Diam =(decimal)Math.Sqrt(deltax +deltay);//typecast safe
Invalidate(); //re-paint
}
}
我主要的问题是,当我保存在列表中的圆形或rectinagle,每次我画一个新的圈子/ rectiangle,在列表中闪烁的形状...... 这里我OnPaint事件:
protected override void OnPaint(PaintEventArgs e)
{
if(draw==true)
{
m_shape.draw();
}
if (shapes.Count > 0)
{
foreach (shape a in shapes)
{
a.draw();
if (m_drawarea == true)
{
string text1 = a.Area().ToString("0. 00");
Font font1 = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Point);
using (Graphics rectangledraw = CreateGraphics())
{
rectangledraw.DrawString(text1, font1, Brushes.Blue, (float)a.Xpos, (float)a.Ypos);
}
}
}
}
}
有谁能够告诉我什么,我做错了什么?因为我不知道该怎么办?发生
面板应该双缓冲以避免看到背景被绘制。你会发现一个在[这个答案](http://stackoverflow.com/questions/3113190/double-buffering-when-not-drawing-in-onpaint-why-doesnt-it-work/3113515#3113515),BufferedPanel类。或者使用PictureBox,默认情况下是双缓冲。 –