Another example of a control hosted in a ToolStrip: ToolStripCustomProgressBar

First off, while taking screenshots to use in this post, I found an issue in my ffmpeg wrapper, which specified “libvo_aacenc” for the aac audio encoder. It turns out that has since changed and because I recently updated the ffmpeg referenced, I had to change it to use “aac” for the built-in encoder. The source code archive has been updated. By the way, I use the Windows 64-bit and 32-bit static builds of ffmpeg, from this site.

I originally shared the code for my custom ProgressBar in September 2013.

That’s a progress bar that does some custom drawing to show a disabled state, and in the dialogs shown below, my code to suspend a process is used when the user clicks the Pause/Resume button to suspend or resume the underlying ffmpeg process that’s being controlled. (Actually I wrote the pause/resume process code and this control entirely because of this foolish feature that allows you to do as many simultaneous video conversions as you like. I also allow you to set the video conversion process priority higher than the application process priority. Good luck clicking those buttons then. If I were smarter, I’d probably have limited the number of allowed simultaneous conversions and had one batch dialog to show them all. But I’m not that smart. Sorry.)

So… this is what the custom progress bar normally looks like:

SNAGHTML4eb7f9d2

And here it is disabled when the video conversion is paused:

SNAGHTML4eb8c10a

(Once again, these dialogs are using my stupid code that draws shadows for common controls. I can’t remember if I shared that but can’t seem to find it on this blog.)

You can refer to the old post or the full source code archive (see the first paragraph of this post) for the progress bar itself. I just wanted to show (again) how easy it is to host a control in a ToolStrip or StatusStrip.

Here’s the hosted control, showing normal and disabled state in a StatusStrip… To force a long-running process, I used a feature of my application that allows the user to precache all thumbnails in a hierarchy of nested directories. One of my less intuitive/intelligent pieces of code that supports being paused by clicking on the label next to the progress bar.

SNAGHTML4ebfb2ef

SNAGHTML4ec0234c

And here’s the Toolstrip-hosted code for that control… I’ve shared this simply to show again how easy it is to host such a control… the actual code required to get all this working isn’t complete in this post, because I didn’t include the interface where the disabled progress bar colours are defined.

The point here is to emphasize how easy it is to host such a control. In this case, the control hosted derives from ProgressBar, so all the relevant properties and methods of the hosted (wrapped) control are exposed such that using it programmatically is then exactly the same as using a ProgressBar directly.

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Romy.Controls
{
    /// <summary>A ProgressBar that uses custom drawing to show if it is disabled, hosted in a ToolStrip.</summary>
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
    public class ToolStripCustomProgressBar : ToolStripControlHost, IDisabledProgressBarColors
    {
        public ToolStripCustomProgressBar()
            : base(CreateControlInstance())
        { }

        [Description("The color used for the bottom of the ProgressBar background when it is disabled."), Category("Disabled Appearance")]
        public Color BackGradientBottomColor
        {
            get => ProgressBar.BackGradientBottomColor;
            set => ProgressBar.BackGradientBottomColor = value;
        }

        [Description("The color used for the middle of the ProgressBar background when it is disabled."), Category("Disabled Appearance")]
        public Color BackGradientMiddleColor
        {
            get => ProgressBar.BackGradientMiddleColor;
            set => ProgressBar.BackGradientMiddleColor = value;
        }

        [Description("The color used for the top of the ProgressBar background when it is disabled."), Category("Disabled Appearance")]
        public Color BackGradientTopColor
        {
            get => ProgressBar.BackGradientTopColor;
            set => ProgressBar.BackGradientTopColor = value;
        }

        [Description("The color used for the bottom border of the ProgressBar when it is disabled."), Category("Disabled Appearance")]
        public Color BorderGradientBottomColor
        {
            get => ProgressBar.BorderGradientBottomColor;
            set => ProgressBar.BorderGradientBottomColor = value;
        }

        [Description("The color used for the top border of the ProgressBar when it is disabled."), Category("Disabled Appearance")]
        public Color BorderGradientTopColor
        {
            get => ProgressBar.BorderGradientTopColor;
            set => ProgressBar.BorderGradientTopColor = value;
        }

        [Description("The color used for the bottom of the ProgressBar progress value when it is disabled."), Category("Disabled Appearance")]
        public Color ProgressGradientBottomColor
        {
            get => ProgressBar.ProgressGradientBottomColor;
            set => ProgressBar.ProgressGradientBottomColor = value;
        }

        [Description("The color used for the middle of the ProgressBar progress value when it is disabled."), Category("Disabled Appearance")]
        public Color ProgressGradientMiddleColor
        {
            get => ProgressBar.ProgressGradientMiddleColor;
            set => ProgressBar.ProgressGradientMiddleColor = value;
        }

        [Description("The color used for the top of the ProgressBar progress value when it is disabled."), Category("Disabled Appearance")]
        public Color ProgressGradientTopColor
        {
            get => ProgressBar.ProgressGradientTopColor;
            set => ProgressBar.ProgressGradientTopColor = value;
        }

        [Description("The color used to highlight the bottom of the ProgressBar progress value when it is disabled."), Category("Disabled Appearance")]
        public Color ProgressHighlightColor
        {
            get => ProgressBar.ProgressHighlightColor;
            set => ProgressBar.ProgressHighlightColor = value;
        }

        public CustomProgressBar ProgressBar => Control as CustomProgressBar;

        public int Maximum
        {
            get => ProgressBar.Maximum;
            set => ProgressBar.Maximum = value;
        }

        public int Minimum
        {
            get => ProgressBar.Minimum;
            set => ProgressBar.Minimum = value;
        }

        public int Value
        {
            get => ProgressBar.Value;
            set => ProgressBar.Value = value;
        }

        public ProgressBarStyle Style
        {
            get => ProgressBar.Style;
            set => ProgressBar.Style = value;
        }

        public void Increment(int value) => ProgressBar.Increment(value);

        public void PerformStep() => ProgressBar.PerformStep();

        private static Control CreateControlInstance() => new CustomProgressBar { Size = new Size(100, 16) };
    }
}
Advertisements

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s