调用.Update()后绘画到面板 [英] Painting to panel after .Update() call

查看:117
本文介绍了调用.Update()后绘画到面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem when trying to draw to a panel immediatly after calling a panel.Update(); Here's the code:

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

When running this problem I only see the cleared panel and not he content I'm trying to paint in the .PaintContent()-method. I've already tried using .Invalidate() and .Refresh() on the panel instead of .Update()

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

Any suggestions on how to fix this?

推荐答案

您的情况似乎需要PictureBox.

PBs在这里很有趣,因为它们具有可以显示的 3 层:

PBs are interesting here because they have three layers they can display:

  • 它们具有BackgroundImage属性
  • 它们具有Image属性
  • 和大多数控件一样,您可以在Paint事件中绘制表面
  • They have a BackgroundImage property
  • They have an Image property
  • And as most controls they have a surface you can draw on in the Paint event

因为您需要一个固定轴,并且图形一直都没有变化,而且您想要经常更新的点PB似乎适合您!

As you need a fixed axis, and graph that is not changing all the time and a point you want to update often PB seems to be made for you!

根据需要调用函数,并且当点改变时,调用PictureBox上的Invalidate()

Call the functions as needed and when the point has changed call Invalidate() on your 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);
}

这篇关于调用.Update()后绘画到面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆