Adding text to the toolbar progress bar

I'm trying to add some text to the Toolbar toolbar , but so far I have not understood it, here is the code I found HERE :

private void pbPrecentage(ToolStripProgressBar pb) { ProgressBar p = new ProgressBar(); int percent = (int)(((double)(pb.Value - pb.Minimum) / (double)(pb.Maximum - pb.Minimum)) * 100); using (Graphics gr = pb.CreateGraphics()) { gr.DrawString(percent.ToString() + "%", SystemFonts.DefaultFont, Brushes.Black, new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%", SystemFonts.DefaultFont).Width / 2.0F), pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%", SystemFonts.DefaultFont).Height / 2.0F))); } } 

The problem is that there is no CreateGraphics method in the toolbar progress panel. So I was wondering if anyone was able to successfully add text to the progress bar of the tool.

UPDATE

Well, it looks like ToolStripProgressBar has a progress bar property, which in turn has a CreateGraphics method, but now the problem is that the text value is blinking and blinking, how can I fix it? Here is the revised code:

 private void pbPrecentage(ToolStripProgressBar pb) { int percent = (int)(((double)(pb.Value - pb.Minimum) / (double)(pb.Maximum - pb.Minimum)) * 100); using (Graphics gr = pb.ProgressBar.CreateGraphics()) { gr.DrawString(percent.ToString() + "%", SystemFonts.DefaultFont, Brushes.Black, new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%", SystemFonts.DefaultFont).Width / 2.0F), pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%", SystemFonts.DefaultFont).Height / 2.0F))); } } 
+6
source share
2 answers

You are facing a very common GDI + based Windows Forms issue. The default double buffering setting for drawing controls (and custom drawing contexts such as graphics) is disabled. So just add a few lines of code to your form:

 public Form1() { //Activate Double Buffering for all kind of drawing within your form this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); } 

If you want more nice graphics, you should also use the SmoothingMode for your drawing context:

 private void pbPrecentage(ToolStripProgressBar pb) { int percent = (int)(((double)(pb.Value - pb.Minimum) / (double)(pb.Maximum - pb.Minimum)) * 100); using (Graphics gr = pb.ProgressBar.CreateGraphics()) { //Switch to Antialiased drawing for better (smoother) graphic results gr.SmoothingMode = SmoothingMode.AntiAlias; gr.DrawString(percent.ToString() + "%", SystemFonts.DefaultFont, Brushes.Black, new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%", SystemFonts.DefaultFont).Width / 2.0F), pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%", SystemFonts.DefaultFont).Height / 2.0F))); } } 
+6
source

I need to use MethodInvoker to solve a problem while drawing an updated progress bar.

Here is the updateProgressBars () function, which is called using a timer.

 private void updateProgressBars(object sender, System.Timers.ElapsedEventArgs e) { this.Invoke(new MethodInvoker(delegate { FillDiskRatio(false); })); } 

FillDiskRatio () updates the progress bar ratio.

0
source

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


All Articles