C#createGraphics,仅在面板绘制事件

时间:2015-09-30 20:05:55

标签: c#

我需要绘制随机颜色和随机位置的随机形状。 现在我想出了如何使用paint事件,但它似乎只有在我在paint事件中初始化然后使用Pen时

    private void ShapesPanel_Paint(object sender, PaintEventArgs e)
    {
        _graphics = ShapesPanel.CreateGraphics();
        _pen = new Pen(Color.Black, 1);
    }

这样可行,但我想要随机颜色,每个形状都有自己生成的随机颜色。

我有这个有效的foreach:

        foreach (var shape in _shapes)
        {
            shape.DrawAble(_graphics);
        }

现在我想让绘图变形:

        foreach (var shape in _shapes)
        {
            _pen = new Pen(shape.Color, 3);
            shape.DrawAble(_graphics);
        }

这根本不会给出任何图纸。 有人熟悉吗? 感谢

表单类

    public partial class ShapesForm : Form
{
    private Shape _shape;

    private Graphics _graphics;
    private Pen _pen;

    private Random _random;

    private int _red, _blue, _green;
    private Color _color;

    private int _x, _y;
    private Point _point;

    private int _randomShape;

    private double _size;
    private double _radius;


    private List<Shape> _shapes;

    public ShapesForm()
    {
        InitializeComponent();
        _shapes = new List<Shape>();
        _random = new Random();
    }

    private void ShapesPanel_Paint(object sender, PaintEventArgs e)
    {
        _graphics = ShapesPanel.CreateGraphics();
        _pen = new Pen(Color.Black, 1);
    }

    private void AddShapeButton_Click(object sender, EventArgs e)
    {
        _red = _random.Next(0, 255);
        _green = _random.Next(0, 255);
        _blue = _random.Next(0, 255);
        _color = Color.FromArgb(1, _red, _green, _blue);

        _x = _random.Next(0, 100);
        _y = _random.Next(0, 100);
        _point = new Point(_x, _y);

        _radius = _random.Next(0, 20);
        _size = _random.Next(0, 20);

        _randomShape = _random.Next(0, 2);

        switch(_randomShape)
        {
            case 0:
                _shape = new Circle(_point, _color, _radius);
                _shapes.Add(_shape);
                UpdateShapeListBox();
                DrawShapes();
                break;
            case 1:
                _shape = new Square(_point, _color, _size);
                _shapes.Add(_shape);
                UpdateShapeListBox();
                DrawShapes();
                break;
        }
    }

    public void UpdateShapeListBox()
    {
        ShapesListBox.Items.Clear();

        foreach (var shape in _shapes)
        {
            ShapesListBox.Items.Add(shape.ToString());
        }
    }

    public void DrawShapes()
    {
        ShapesPanel.Refresh();
        foreach (var shape in _shapes)
        {
            _pen = new Pen(shape.Color, 3);
            shape.DrawAble(_graphics);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要使用Painte.Graphics处理程序中完成所有绘图。

如果您想绘制某些内容,请致电Invalidate()以举起Paint个事件,然后确保您的Paint处理程序将绘制您想要的所有内容。

答案 1 :(得分:1)

这是你想要实现的那种逻辑:

private void ShapesPanel_Paint(object sender, PaintEventArgs e)
{
    foreach (var shape in _shapes)
    {
        shape.DrawAble(e.Graphics);
    }    
}

// in the Shape class
public void DrawAble(Graphics g)
{
    var pen = new Pen(this.Color, 3);
    g.DrawRect( ... ); // or whatever
}

您应该使用paint处理程序中的e.Graphics,并且仅在绘制处理程序运行时使用。{/ p>

通常会在必要时调用paint处理程序。如果您想要重新绘制,因为形状已更改,请致电ShapesPanel.Invalidate()