Applications in Windows environments now often use a progress bar to indicate the running state of some complex processes. At the moment we are using a monochrome progress bar for form design. This article describes a progress bar component that uses VB2005 to make a beautiful color gradient in a Windows XP environment.
A Technical details
The display of such a progress bar is equivalent to drawing squares on a form using many color-similar "brushes". These "brushes" are equivalent to a brush array. First you create an array of colors, put some color values at the beginning, and then reset the array of colors according to the smoothness setting. In turn, remove the two adjacent color values from the color array, calculate the blend color of the two colors, and put them in the middle of the two colors. In the example, there are 8 colors in the color array. The smoothness value starts at 0 and the highest is 7. Each time you go through a loop, the color array adds some gradient colors. The greater the smoothness setting, the softer the color transitions. The result is more beautiful. After the color array is generated, the brush array is determined. It has the same size and array of colors. With a brush array, the following problem is drawing operations on the form.
Ii. Principle of realization
Before the drawing operation. So to overload the message handling event WndProc for forms in the base class, the form size Change event onresize and the form redraw event OnPaint. Add the following property settings:
Minimum. Represents the minimum value of a progress bar. The default is 0.
Maximum. Represents the maximum value of a progress bar. The default is 100.
Value. Represents the current value of a progress bar. The default is 0.
Smoothness. Represents the smoothness of the progress bar color gradient. The default is 0.
When initialized, the corresponding brush array is generated when the above four properties are set. Each change to the current value triggers the OnPaint event. After the event is triggered, the outer border of the progress bar is drawn first, followed by the total width and height of the inner border. Calculates the percent complete by the current value, the minimum and the maximum, and the width of the current value by the percent complete multiplied by the total width. The total width divided by the number of brush arrays gets the width that each brush occupies. Then, from the initial width loop to the width occupied by the current value, use the brush in turn from the brush array to fill the color with the width and height that each brush occupies. After the loop ends, if the percentage is 100%. The color is drawn using the last brush in the brush array. The progress bar is displayed.