0

我正在开发用于视频使用的时间线用户控件。这是我的工作照片: 在此处输入图像描述

在这里我有问题。当 currentTime 改变时,我必须更新 UI。但是当我使用 this.Invalidate(); 它刷新了整个控件。但我只想更新指针(灰色背景中的白线)。因为它会导致控件在非常小的时间变化上一遍又一遍地闪烁。如何只更新指针?这是我的 onPaint 方法

 private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        int width = this.Width;
        int height = this.Height;
        int Floor = height - FloorDistance;
        Pen SecPen = new Pen(SecColor);
        Pen MinPen = new Pen(MinColor);
        SolidBrush TimeLineBrush = new SolidBrush(Color.Gray);
        StringFormat stringFormat = new StringFormat();
        SolidBrush TimesolidBrush = new SolidBrush(TimeColor);
        SolidBrush BackBrush = new SolidBrush(TimlineBackColor);
        SolidBrush PointerBrush = new SolidBrush(PointerColor);
        stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
        switch (Zoom)
        {
            case ZoomLEvel.Quarter:
                width = (int)Math.Ceiling((TotalDuration / 900)) * ThickDistance;
                break;
            case ZoomLEvel.Minute:
                width = (int)Math.Ceiling((TotalDuration / 60)) * ThickDistance;
                break;
            case ZoomLEvel.Secound:
                width = (int)Math.Ceiling(TotalDuration) * ThickDistance;
                break;
            case ZoomLEvel.MiliSecound:
                width = (int)Math.Ceiling(TotalDuration * 10) * ThickDistance;
                break;
        }
        width += 11;
        this.Width = width;

        e.Graphics.FillRectangle(TimeLineBrush, 0, Floor, width, 3);
        e.Graphics.FillRectangle(BackBrush, 0, 0, width, height - FloorDistance);

        int x = ThickDistance;
        int step = 0;
        while (x <= width - ThickDistance)
        {
            if (step % 5 == 0)
            {
                e.Graphics.DrawLine(MinPen, x, Floor, x, Floor - _MinHeight);
                // print time
                string time = "";
                double totalSecounds = 0;
                PointF pointF = new PointF(x - 8, Floor + 5);


                switch (Zoom)
                {
                    case ZoomLEvel.Quarter:
                        totalSecounds = step * 900;
                        break;
                    case ZoomLEvel.Minute:
                        totalSecounds = step * 60;
                        break;
                    case ZoomLEvel.Secound:
                        totalSecounds = step;
                        break;
                    case ZoomLEvel.MiliSecound:
                        totalSecounds = step / 10d;
                        break;
                }

                time = (new TimeSpan(0, 0, 0, (int)totalSecounds, (int)(step % 10) * 100)).ToString(@"hh\:mm\:ss\:fff");
                e.Graphics.DrawString(time, this.Font, TimesolidBrush, pointF, stringFormat);

            }
            else
                e.Graphics.DrawLine(SecPen, x, Floor, x, Floor - _SecHeight);
            x += ThickDistance;
            step++;
        }
        int PointerTime = 0;//(int)Math.Floor(CurrentTime);
        int pointerX = 0;
        switch (Zoom)
        {
            case ZoomLEvel.Quarter:

                PointerTime = (int)Math.Floor(CurrentTime / 900);
                pointerX = PointerTime * ThickDistance;
                break;
            case ZoomLEvel.Minute:
                PointerTime = (int)Math.Floor(CurrentTime / 60);
                pointerX = PointerTime * ThickDistance;
                break;
            case ZoomLEvel.Secound:
                PointerTime = (int)Math.Floor(CurrentTime);
                pointerX = PointerTime * ThickDistance;
                break;
            case ZoomLEvel.MiliSecound:
                PointerTime = (int)Math.Floor(CurrentTime * 10);
                pointerX = PointerTime * ThickDistance;
                break;
        }
        pointerX += 5;
        e.Graphics.FillRectangle(PointerBrush, pointerX, 0, 2, height - FloorDistance);
    }
4

1 回答 1

2

Invalidate()该方法的重载采用Rectangle定义要刷新的区域的类型参数,称为剪切矩形。您应该传递指针的边界矩形(白线,四边可能有 10 个像素的填充)。在您的OnPaint()方法中,您应该检查e.ClipRectangle需要重绘区域的属性。现在,对于所有绘图逻辑(例如,Graphics.DrawX()调用),您应该首先确认该元素是否与剪切区域相交(可以使用 轻松完成Rectangle.IntersectsWith())。如果是,则应调用DrawX方法,否则不应调用。

为避免闪烁,您应该将DoubleBuffered控件的属性打开为True. 这对实现真正流畅的渲染大有帮助。

此外,我看到您在您的OnPaint方法中声明了很多刷子和钢笔,并且在使用后没有丢弃它们。我假设其中许多将一次又一次地被需要,因此您可能希望在类级别声明它们,然后在Dispose()您的控件的方法中处理它们。考虑到这是一个与视频相关的应用程序,这将为您节省一些处理时间。

希望有帮助。

于 2013-04-28T09:05:56.967 回答