C#在鼠标单击Form上绘制椭圆

时间:2017-04-28 16:15:32

标签: c# drawing ellipse

我在绘制C#时很新 我想在鼠标点击时在Form上绘制省略号。我写了一段代码,但它并不想用鼠标点击。这是我的代码:

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
{   public Form1()
    {
        InitializeComponent();
    }
    Pen p = new Pen(Color.Red, 3);
    SolidBrush b = new SolidBrush(Color.Red);
    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        Graphics g = CreateGraphics();

        if (radioButton1.Checked)
        {
            p.Color = Color.Red;
            b.Color = Color.Red;

            }
        if (radioButton2.Checked)
        {
            p.Color = Color.Yellow;
            b.Color = Color.Yellow;

            }
        if (radioButton3.Checked)
        {
            p.Color = Color.Blue;
            b.Color = Color.Blue;

            }
        if (checkBox1.Checked)
            g.FillEllipse(b, e.X, e.Y, 50, 50);

        else
            g.DrawEllipse(p, e.X, e.Y, 50, 50);
            g.Dispose();
    }
}
}

2 个答案:

答案 0 :(得分:1)

您不应使用CreateGraphics来劫持控件的绘图表面。当表格重新绘制时,您以这种方式绘制的任何内容都将消失。此外,您不会丢弃PenSolidBrush,因此您会泄漏GDI资源。

相反,创建一个要绘制的省略号列表,然后在表格OnPaint覆盖中绘制它们。

这样的事情:

首先创建一个表示你的形状的类:

public class Ellipse
{
    public Color Color { get; set; }
    public Point Location { get; set; }
    public Size Size { get; set; }
    public bool Filled { get; set; }
}

然后,在表单上单击鼠标时,创建形状的新实例并将其添加到列表中。

public partial class Form1 : Form
{
    //A list to hold all the ellipses to be drawn
    private List<Ellipse> ellipses = new List<Ellipse>();

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnMouseClick(MouseEventArgs e)
    {
        //When the form is clicked, create a new ellipse and add it to the list.
        ellipses.Add(new Ellipse
        {
            Color = radioButton1.Checked ? Color.Red : (radioButton2.Checked ? Color.Yellow : Color.Blue),
            Location = e.Location,
            Size = new Size(50, 50),
            Filled = checkBox1.Checked
        });

        //Tell the form to redraw itself
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //Redraw each ellipse in the list.
        foreach (var ellipse in ellipses)
        {
            if (ellipse.Filled)
            {
                //Use a using block to make sure the resource is disposed
                using (var b = new SolidBrush(ellipse.Color))
                {
                    e.Graphics.FillEllipse(b, new Rectangle(ellipse.Location, ellipse.Size));
                }
            }
            else
            {
                //Use a using block to make sure the resource is disposed
                using (var p = new Pen(ellipse.Color))
                {
                    e.Graphics.DrawEllipse(p, new Rectangle(ellipse.Location, ellipse.Size));                        
                }
            }
        }
    }
}

答案 1 :(得分:-1)

问题可能不是绘制功能。

我将其简化为测试它,它对我有用:

    Pen p = new Pen(Color.Red, 3);
    SolidBrush b = new SolidBrush(Color.Red);

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        Graphics g = this.CreateGraphics();

        p.Color = Color.Blue;
        b.Color = Color.Blue;
        g.FillEllipse(b, e.X, e.Y, 50, 50);
        g.DrawEllipse(p, e.X, e.Y, 50, 50);
        g.Dispose();
    }