从Panel_Paint中的另一个类调用方法不会绘制任何内容

本文关键字:方法 绘制 任何内 调用 Panel Paint 另一个 | 更新日期: 2023-09-27 18:26:27

所以这应该很简单,但我看了一些类似的问题,却找不到答案。

我有Form1类和Resistor类。在Form1类中,我有一个Panel(我将名称更改为Canvas),在Canvas_Paint方法中,我从Resistor类调用方法Draw,但没有绘制任何内容。

Form1类别:

public partial class Form1 : Form
{
    static float lineWidth = 2.0F;
    static float backgroundLineWidth = 2.0F;
    static Pen pen = new Pen(Color.Yellow, lineWidth);
    static Pen backgroundPen = new Pen(Color.LightGray, backgroundLineWidth);
    private bool drawBackground = true;
    private List<Resistor> resistors = new List<Resistor>();                
    public Form1()
    {
        InitializeComponent();
    }
    private void Canvas_Paint(object sender, PaintEventArgs e)
    {
        if (drawBackground)
        {
            Console.WriteLine("Drawing background...");
            Draw_Background(e.Graphics, backgroundPen);
        }
        if (resistors != null)
        {
            foreach (Resistor r in resistors)
            {
                //This does not work.
                r.Draw(e.Graphics);
            }
        }
        //The line below draws the line fine.
        e.Graphics.DrawLine(pen, 0, 0, 100, 100);
    }
    private void Draw_Background(Graphics g, Pen pen)
    {            
        for (int i = 0; i < Canvas.Width; i += 10)
        {
            g.DrawLine(pen, new Point(i, 0), new Point(i, Canvas.Height));          
        }
        for (int j = 0; j < Canvas.Height; j += 10)
        {
            g.DrawLine(pen, new Point(0, j), new Point(Canvas.Width, j));
        }
        drawBackground = false;
    }
    private void AddResistor_Click(object sender, EventArgs e)
    {
        resistors.Add(new Resistor());
        Console.WriteLine("Added a Resistor...");
    }        
}

电阻器类别:

public class Resistor
{
    static private Point startingPoint;
    static Pen defaultPen;
    private Point[] points;
    public Resistor()
    {
        startingPoint.X = 100;
        startingPoint.Y = 100;
        defaultPen = new Pen(Color.Yellow, 2.0F);
        points = new Point[] {
            new Point( 10,  10),
            new Point( 10, 100),
            new Point(200,  50),
            new Point(250, 300) 
        };
    }
    public void Draw(Graphics g)
    {
        //Is this drawing somewhere else?
        g.DrawLines(defaultPen, points);            
    }
}

我已经研究过这个问题,它建议在这种情况下将e.Graphics对象传递给Resistor类中的Draw方法,但不起作用。

我是C#的新手,所以我非常感谢任何帮助。

编辑:如果你想下载并尝试一下,我会把这个项目放在github上。

编辑:因此,问题是在单击按钮后,面板Paint方法没有被调用。解决方案是在AddResistor_Click方法

从Panel_Paint中的另一个类调用方法不会绘制任何内容

中添加Canvas.Invalidate

在调试器中运行代码,并在事件处理程序中设置断点,您将能够检查代码是否真的在尝试绘制内容。如果没有,那么是否调用过您的事件处理程序?你的电阻清单上有什么吗?如果它正在绘制,但您没有看到任何内容,则说明您没有使用正确的"图形"上下文,或者您没有在控件的可见部分绘制内容,或者您正在使用后续的绘制代码绘制内容。

问题是,当点击按钮时,面板的绘制方法没有被调用,因为我认为绘制方法总是被调用。解决方案是在AddResistor_Click方法中加入Canvas.Invalidate

private void AddResistor_Click(object sender, EventArgs e)
    {
        resistors.Add(new Resistor());
        Console.WriteLine("Added a Resistor...");
        Canvas.Invalidate();
    }