C#在鼠标上绘制椭圆形单击表格



我在c#中绘画时非常新我想在鼠标点击上绘制椭圆形。我写了一个代码,但它不想绘制鼠标点击。这是我的代码:

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();
    }
}
}

您不应使用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));                        
                }
            }
        }
    }
}

问题可能是绘制功能以外的其他问题。

我简化了它来测试它,它对我有用:

    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();
    }

最新更新