C# 与视觉样式相关的操作导致Windows经典主题出错

C# 与视觉样式相关的操作导致Windows经典主题出错,c#,winforms,themes,progress-bar,custom-controls,C#,Winforms,Themes,Progress Bar,Custom Controls,我正在运行Windows7嵌入式,为了节省性能,我将视觉样式从Windows7 basic更改为WindowsClassic 在其上运行WinForms appl,并带有自定义进度条。错误在线路上 在Windows7上,基本主题都很好,但在我切换到WindowsClassic后,我发现上面的错误。找不到有关此的任何信息,是否有相关信息 这是我的自定义控件 public class CustomProgressBar : ProgressBar { //Property t

我正在运行Windows7嵌入式,为了节省性能,我将视觉样式从Windows7 basic更改为WindowsClassic

在其上运行WinForms appl,并带有自定义进度条。错误在线路上

在Windows7上,基本主题都很好,但在我切换到WindowsClassic后,我发现上面的错误。找不到有关此的任何信息,是否有相关信息

这是我的自定义控件

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);
            }
        }
    }
这是我的堆栈跟踪

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)
编辑


由于某种原因,在我客户的机器上,即使检查了视觉样式,并且他使用了Windows 7 Aero主题,我仍然会遇到上述错误,我也尝试设置
Application.SetCompatibleTextRenderingDefault(true)哪些没有帮助

可能重复@BugFinder与空气动力效果有什么关系?请参阅的备注部分。还有
rect.Inflate()
只是尝试应用视觉样式之后的一行,正如错误所述,视觉样式并不存在。你没有说你的
程序.cs
@Jimi中的
应用程序.SetCompatibleTextRenderingDefault()
等状态是什么,那么我能做什么来代替
rect.Inflate()
?@Jimi也设置
应用程序.SetCompatibleTextRenderingDefault(true)没有帮助