Picturebox:更新时内容消失 [英] Picturebox: Content disapear when update

查看:91
本文介绍了Picturebox:更新时内容消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我编写了一个简单的程序,以通过条形图显示街道外观的数量.但是当我画新的时,旧的就消失了.

Hi there,

i make a simple program to display the number of street''s appearances through bar chart. but when i draw new one, the old one disappear.

void PicStreetPaint(object sender, PaintEventArgs e)
{
    if(bClear){
        e.Graphics.Clear(Color.White);
    }
    else{
        // s = street-th, r = height of bar
        Brush bColor = new SolidBrush(Color.FromName(aColor[s]));
        grStreet.FillRectangle(bColor, s * 30, 0, 30, r * 20);
    }
}



这是旧代码,当我移动表格时,内容消失了,因为它没有重绘.



this is old code, when it i move form around, contents disappear because it not redraw.

public void UpdateStreet(int number)
{
    if(number == 0){
        return;
    }
    int[] Street = new int[] {1,1,1,2,2,2,3,3,3,4,4,4,
                            5,5,5,6,6,6,7,7,7,8,8,8,
                            9,9,9,10,10,10,11,11,11,12,12,12};
    string[] aColor = new string[]{"Silver", "Tomato", "Red", "Yellow", "GreenYellow", "Green", "Teal", "DodgerBlue", "SlateBlue", "Indigo", "Crimson", "Plum"};
    int r = 0, s = 0;
    Graphics grStreet = picStreet.CreateGraphics();
    Brush bColor;
    number -= 1; // adjust to array
    s = Street[number] - 1; // get street-th
    bColor = new SolidBrush(Color.FromName(aColor[s]));
    street[s] += 1; // update the number of street's appearance
    r = street[s]; // height of bar = number of street's appearance
    grStreet.FillRectangle(bColor, s * 30, 0, 30, r * 20);
    picStreet.Update();
}



非常感谢.



thank you very much.

推荐答案

渲染系统不包含任何内容.您需要在发送给应用程序的每个Windows消息WM_PAINT上提供正确的呈现过程.发送此消息后,CLI会调用控件的虚拟方法OnPaint(可以重写以提供呈现),并触发事件Paint,您可以为处理两者的相同目的进行处理(这几乎是不实际的;如果您将某个控件类子类化以实现自定义控件,则我建议重写方法方法OnPaint作为提供呈现的首选方法.

现在,当您控制场景或部分场景无效时,将发送此消息.当以前隐藏在其他窗口下的全部或部分控件公开或您显式调用System.Windows.Forms.Control.Invalidate方法之一时,就会发生这种情况.请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ].
我觉得您了解它的基础知识,但是对事件的顺序和预期的渲染技术有一些误解,所以我认为您很难将其正确设置.您可以在我过去的答案中找到更多详细信息:
在mdi子表单之间画线 [在面板上捕获图形 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [ http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ]).
请同时参阅我过去的答案:
如何从旧图纸中清除面板 [ ^ ],
在C#中绘制矩形 [在图片框内添加图片 [
The rendering system does not hold any content. You need to provide proper rendering procedure on each Windows message WM_PAINT sent to your application. When this message is sent, CLI calls the virtual method OnPaint of your control (which you can override to provide rendering) and fires the event Paint which you can handle for the same purpose of do both (which is hardly practical; if you subclass some control class to implement a custom control, I would recommend overriding of the method method OnPaint as a preferred way to provide rendering).

Now, this message is sent when you control or part of the scene is invalidated. I happens when all or a part of control previously hidden below some other window is exposed, or when you explicitly call one of the System.Windows.Forms.Control.Invalidate method. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].
I feel that you understand the basics of it but just have some misunderstanding on the order of events and intended technique of rendering, so I don''t thing it will be difficult for you to put it right. You can find some more detail in my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

Now, I don''t see where you use PictureBox but chances are, you don''t need it at all. This control was designed to provide a simple way of presenting an image, mostly the pre-generated one, such as loaded from a file. When you try to implement anything dynamic, anything at all, such as any interactive and/or animated behavior, it becomes a pure hassle, just eating up extra resources and your development time. There is no value in this control compared to the way of implementing a purely custom control, presumably derived from System.Windows.Forms.Control. Unfortunately, PictureBox became an easy lure for many who heavily abused this control, thus creating an accidental complexity (http://en.wikipedia.org/wiki/Accidental_complexity[^]) out of nothing.

Please see also my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

—SA


这篇关于Picturebox:更新时内容消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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