2

im running windows 7 embedded and to save on performance i change the visual styles from windows 7 basic to windows classic.

on which im running WinForms appl with a custom progress bar. the error is on the line Rectangle.Inflate(x,y);

On windows 7 basic theme all is good but after i switch to windows classic im getting the above error. Couldn't find any info about this, is there anyway around this?

this is my custom control

public class CustomProgressBar : ProgressBar
    {
        //Property to set to decide whether to print a % or Text
        public ProgressBarDisplayText DisplayStyle { get; set; }

        //Property to hold the custom text
        public String CustomText { get; set; }

        public CustomProgressBar()
        {
            // Modify the ControlStyles flags -> OptimizedDoubleBuffer to avoid flickering text          
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rect = ClientRectangle;
            Graphics g = e.Graphics;

            ProgressBarRenderer.DrawHorizontalBar(g, rect);
            rect.Inflate(-3, -3); //here im getting the error
            if (Value > 0)
            {
                // As we doing this ourselves we need to draw the chunks on the progress bar
                Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
                ProgressBarRenderer.DrawHorizontalChunks(g, clip);
            }

            // Set the Display text (Either a % amount or our custom text
            string text = DisplayStyle == ProgressBarDisplayText.Percentage ? Value.ToString() + '%' : CustomText;


            using (Font f = new Font(FontFamily.GenericSerif, 14))
            {

                SizeF len = g.MeasureString(text, f);
                // Calculate the location of the text (the middle of progress bar)
                // Point location = new Point(Convert.ToInt32((rect.Width / 2) - (len.Width / 2)), Convert.ToInt32((rect.Height / 2) - (len.Height / 2)));
                Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2), Convert.ToInt32((Height / 2) - len.Height / 2));
                // The commented-out code will centre the text into the highlighted area only. This will centre the text regardless of the highlighted area.
                // Draw the custom text
                g.DrawString(text, f, Brushes.Red, location);
            }
        }
    }

this is my stack trace

System.InvalidOperationException: Visual Styles-related operation resulted in an error because no visual style is currently active.
   at System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsCombinationDefined(String className, Int32 part)
   at System.Windows.Forms.VisualStyles.VisualStyleRenderer..ctor(String className, Int32 part, Int32 state)
   at System.Windows.Forms.ProgressBarRenderer.InitializeRenderer(VisualStyleElement element)
   at System.Windows.Forms.ProgressBarRenderer.DrawHorizontalBar(Graphics g, Rectangle bounds)
   at YonatanDataReader2.CustomProgressBar.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Edit

for some reason on my client's machine even if the visual styles are checked and he uses Windows 7 Aero theme im still getting the above error, I also tried to set Application.SetCompatibleTextRenderingDefault(true); which did not helped as well

styx
  • 1,852
  • 1
  • 11
  • 22
  • Possible duplicate of [How make sure Aero effect is enabled?](https://stackoverflow.com/questions/5114389/how-make-sure-aero-effect-is-enabled) – BugFinder Sep 03 '18 at 14:32
  • @BugFinder what is the relation to Aero effect? – styx Sep 03 '18 at 15:18
  • See the Remarks section of [ProgressBarRenderer](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.progressbarrenderer?view=netframework-4.7.2). And the [ProgressBarRenderer.IsSupported](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.progressbarrenderer.issupported?view=netframework-4.7.2). `rect.Inflate()` is just the line right after you try to apply a Visual Style which, as the error states, doesn't exist. You didn't say what's the `Application.SetCompatibleTextRenderingDefault()` etc. status in your `Program.cs` – Jimi Sep 03 '18 at 17:50
  • @Jimi so what can i do instead of `rect.Inflate()`? – styx Sep 04 '18 at 08:55
  • @Jimi also setting `Application.SetCompatibleTextRenderingDefault(true);` does not help – styx Sep 04 '18 at 10:16
  • From the linked document: (ProgressBarRenderer) *Provides methods used to render a progress bar control with visual styles.* Do you have visual styles enabled? – Jimi Sep 04 '18 at 16:59
  • @Jimi i have visual styles on, check via [this link](https://www.sevenforums.com/tutorials/127339-visual-styles-windows-buttons-turn-off.html) . also see my Edit – styx Sep 05 '18 at 06:03
  • Follow the example here: [ProgressBarRenderer](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.progressbarrenderer?view=netframework-4.7.2). Note the constant checks, using `ProgressBarRenderer.IsSupported` and the `.EnableVisualStyles()` call. See the comment about enabling Visual Styles in the System. Also, `Application.SetCompatibleTextRenderingDefault(True)` activate the compatibility (legacy) mode. The default, when Visual Styles are enabled, is `.SetCompatibleTextRenderingDefault(False)`. – Jimi Sep 05 '18 at 13:39

0 Answers0