Animated Shine in Owner-Created Progress Bar (ListView / DataGridView)

I noticed that the basic standard of ProgressBar in .NET 2.0 (Winforms) appears as an attractive animated luminous bar in Vista; however, using the ProgressBarRenderer (as is usually the case when trying to draw a progress bar in the owner’s list, grid view, or other such control) simply gives a visual style without nice animation.

I think it would be foolish to expect this to work just magically - I believe that Vista should have some kind of built-in timer or stream that should not exist when drawing a static image. I saw that if you redraw the ProgressBar control several times in a row (using DrawToBitmap), you can see the individual stages of the animated glow, so I experimented with a timer to automatically redraw, but something doesn't look right and it also consumes much more processor time than a real ProgressBar.

This seems to leave me with two non-standard options: a) Use the ProgressBarRenderer and end with Vista a “look”, but no animation; or b) Use a timer to constantly redraw multiple ProgressBars for bitmaps and to discard processor cycles to make them look better, but still not perfect.

I was wondering if anyone had experience building embedded progress indicators inside owner-controlled controls and might know a better way than the two options above - that can accurately reproduce the glow / luster without relying on timers and / or clogging the CPU.

+3
source share
1 answer

, . , MSFT VisualStyleElement.ProgressBar, , Vista. . , . , -, :

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Reflection;

namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    VisualStyleElement pulseOverlay;
    VisualStyleElement moveOverlay;
    VisualStyleRenderer pulseRenderer;
    VisualStyleRenderer moveRenderer;
    Timer animator = new Timer();
    public Form1() {
      InitializeComponent();
      ConstructorInfo ci = typeof(VisualStyleElement).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
        null, new Type[] { typeof(string), typeof(int), typeof(int) }, null);
      pulseOverlay = (VisualStyleElement)ci.Invoke(new object[] { "PROGRESS", 7, 0 });
      moveOverlay = (VisualStyleElement)ci.Invoke(new object[] { "PROGRESS", 8, 0 });
      pulseRenderer = new VisualStyleRenderer(pulseOverlay);
      moveRenderer = new VisualStyleRenderer(moveOverlay);
      animator.Interval = 20;
      animator.Tick += new EventHandler(animator_Tick);
      animator.Enabled = true;
      this.DoubleBuffered = true;
    }
    void animator_Tick(object sender, EventArgs e) {
      Invalidate();
    }

    int xpos;
    protected override void OnPaint(PaintEventArgs e) {
      Rectangle rc = new Rectangle(10, 10, 100, 20);
      ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rc);
      rc = new Rectangle(10, 10, 50, 20);
      ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, rc);
      xpos += 3;
      if (xpos >= 30) xpos = -150;  // Note: intentionally too far left
      rc = new Rectangle(xpos, 10, 50, 20);
      pulseRenderer.DrawBackground(e.Graphics, rc);
      moveRenderer.DrawBackground(e.Graphics, rc);
    }
  }

}
+4

Source: https://habr.com/ru/post/1699681/


All Articles