winform中的OnPaint - .NET Compact Framework 3.5

时间:2012-03-19 00:42:06

标签: c# winforms compact-framework onpaint

我目前正在智能设备项目中处理弯曲的进度条。我重写了OnPaint函数。

在Override OnPaint()函数中,我以深灰色绘制弯曲的进度条,并以黄色绘制进度条的一部分以反映进度条的变化值(例如30%)。但是,当值连续变化时,我可以看到进度条颜色的一部分变为黄色。但是,弯曲的进度条本身也会重新绘制。有没有人知道如何在值变化时避免重绘原始曲线进度条的方法?因此,当值发生变化时,它只会用黄色重绘进度条的一部分,而不是带灰色的原始进度条。这是我在OnPaint函数中使用的代码。

protected override void OnPaint(PaintEventArgs e)
        {
            gx = e.Graphics;
// Draw the original curved progress bar
            int intPosition1 = m_NumberOfSpoke;

            for (int intCounter1 = 0; intCounter1 < m_NumberOfSpoke; intCounter1++)
            {
                intPosition1 = intPosition1 % m_NumberOfSpoke;
                DrawLine(e.Graphics,
                         GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition1]),
                         GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition1]),
                         Color.DarkGray, m_SpokeThickness);
                intPosition1++;
            }

     // Draw a part of the progress bar to reflect the changing current value(such as 30%)
   int intPosition = CurrentValue;

                for (int intCounter1 = 0; intCounter1 < CurrentValue; intCounter1++)
                {
                    intPosition = intPosition % CurrentValue;
                    DrawLine(gx,
                             GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]),
                             GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]),
                             Color.Yellow, m_SpokeThickness);
                    intPosition++;
                }

        base.OnPaint(e);



        }

我尝试使用Override OnBackgroundPaint绘制原始曲线进度条作为背景,以避免在OnPaint中再次重绘它,但它不起作用。加载表单时我看不到任何内容。有什么想法吗?

感谢您提前提供任何帮助。

问候

1 个答案:

答案 0 :(得分:3)

这是对实际屏幕的DrawLine调用的负载,并且很可能会导致闪烁。您应该通过创建一个后台缓冲区来双倍缓冲,完成所有绘制,然后通过一行调用DrawBitmap将这些缓冲到屏幕上:

protected override void OnPaint(PaintEventArgs e)
{
    using(var buffer = new Bitmap(this.Width, this.Height))
    using(var gx = Graphics.FromImage(buffer))
    {
        // for loops to draw to gx
        ....

        e.Graphics.DrawBitmap(buffer, ...);
    }

}

我也非常倾向于不做上面的事情,但是缓存该缓冲区以防止在每次调用时生成Bitmap的垃圾。

Bitmap m_buffer;
Gramphic m_gx;

protected override void OnPaint(PaintEventArgs e)
{
    if(m_buffer == null)
    {
        m_buffer = new Bitmap(this.Width, this.Height))
        m_gx = Graphics.FromImage(buffer))
    }

    // clear the backbuffer with a FillRect

    // for loops to draw to m_gx
    ....

    e.Graphics.DrawBitmap(m_buffer, ...);
}

我甚至可能更进一步,如果控件的灰色部分始终相同并执行“三重缓冲”,保留图像的缓存版本并绘制灰色,然后在OnPaint中,blit到图形,绘制黄色,然后blit到屏幕。

Bitmap m_buffer;
Bitmap m_backimage;
Gramphic m_gx;

protected override void OnPaint(PaintEventArgs e)
{
    if(m_buffer == null)
    {
        m_backimage = new Bitmap(this.Width, this.Height);
        var g = Graphics.FromImage(m_backImage);
        // for loop to draw the grey stuff to g
        ....

        m_buffer = new Bitmap(this.Width, this.Height))
        m_gx = Graphics.FromImage(buffer))
    }

    m_gx.DrawImage(m_backImage);

    // for loop to draw *just the yellow* to m_gx
    ....

    e.Graphics.DrawBitmap(m_buffer, ...);
}

你可能不得不重写OnResize和其他一些东西,加上Dispose来清理这些成员级的GDI对象,但是perf会更好,而且不会闪烁。