Friday, October 25, 2019

Text Over Progressbar

Transparent labels don't work in winforms so the text has to be manually drawn. From this article, you can add percents. Based on that code, here is a version with bolded custom text:

        private void UpdateProgressText(string ProgressText)
        {
            using (Graphics gr = prgStatus.CreateGraphics())
            {
                gr.DrawString(ProgressText,
                    new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold),
                    Brushes.Black,
                    new PointF(prgStatus.Width / 2 - (gr.MeasureString(ProgressText,
                        SystemFonts.DefaultFont).Width / 2.0F),
                    prgStatus.Height / 2 - (gr.MeasureString(ProgressText,
                        SystemFonts.DefaultFont).Height / 2.0F)));
            }
        }

Make sure to call UpdateProgressText() in the ProgressChanged() handler after doing a prgBar.Update();

1 comment: