我目前正在智能设备项目中的弯曲进度栏上工作。我重写了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中再次重绘它,但是它不起作用。加载表单时,我看不到任何东西。有任何想法吗?

感谢您的任何帮助。

问候

最佳答案

这是对实际屏幕的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 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到图形,先绘制黄色,然后将其闪烁显示在屏幕上。

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会更好,并且不会闪烁。

关于c# - Winform中的OnPaint-.NET Compact Framework 3.5,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9763682/

10-10 22:56