在.Update()调用后绘制到面板

时间:2015-04-23 09:36:22

标签: c# panel paint

我在调用panel.Update()后尝试立即绘制到面板时遇到问题。 这是代码:

    public void AddAndDraw(double X, double Y){
        AddPoint (X, Y);
        bool invalidated = false;

        if (X > _master.xMaxRange) {
            _master.Update ();
            invalidated = true;
        }
        if (Y > _master.yMaxRange) {
            _master.Update ();
            invalidated = true;
        }

        if (!invalidated) {
            _master.UpdateGraph (this);
        } else {
            _master.PaintContent ();
        }
    }

运行此问题时,我只看到已清除的面板,而不是他满足于我试图在.PaintContent() - 方法中绘制。我已经尝试在面板上使用.Invalidate()和.Refresh()而不是.Update()

有关如何解决此问题的任何建议吗?

1 个答案:

答案 0 :(得分:0)

您的情况似乎需要PictureBox

PBs非常有趣,因为它们可以显示三个图层:

  • 他们有BackgroundImage属性
  • 他们有Image属性
  • 并且,作为大多数控件,他们都有一个表面,您可以在Paint事件
  • 中进行绘制

由于您需要一个固定的轴,并且图形不会一直变化,而且您想要经常更新的点PB似乎是为您制作的!

根据需要调用这些功能,当点已更改Invalidate()时调用PictureBox

Bitmap GraphBackground = null;
Bitmap GraphImage = null;
Point aPoint = Point.Empty;

private void Form1_Load(object sender, EventArgs e)
{
    PictureBox Graph = pictureBox1;  // short reference, optional

    GraphBackground = new Bitmap(Graph.ClientSize.Width, Graph.ClientSize.Height);
    GraphImage = new Bitmap(Graph.ClientSize.Width, Graph.ClientSize.Height);

    // intial set up, repeat as needed!
    Graph.BackgroundImage = DrawBackground(GraphBackground);
    Graph.Image = DrawGraph(GraphImage);
}

Bitmap DrawBackground(Bitmap bmp)
{
    using (Graphics G = Graphics.FromImage(bmp) )
    {
        // my little axis code..
        Point c = new Point(bmp.Width / 2, bmp.Height / 2);
        G.DrawLine(Pens.DarkSlateGray, 0, c.Y, bmp.Width, c.Y);
        G.DrawLine(Pens.DarkSlateGray, c.X, 0, c.X, bmp.Height);
        G.DrawString("0", Font, Brushes.Black, c);
    }
    return bmp;
}

Bitmap DrawGraph(Bitmap bmp)
{
    using (Graphics G = Graphics.FromImage(bmp))
    {
        // do your drawing here

    }

    return bmp;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    // make it as fat as you like ;-)
    e.Graphics.FillEllipse(Brushes.Red, aPoint.X - 3, aPoint.Y - 3, 7, 7);
}