C#解决在Winform中绘图异常闪烁问题

1,问题描述:

  • 在winform中使用Graphics进行绘图,绘图效果却呈现异常闪烁。

2,解决办法:

  • 使用  SetStyle()  设置相应特性:
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);   //   禁止擦除背景.
this.SetStyle(ControlStyles.DoubleBuffer, true);   //   双缓冲
  • 建立临时对象,在临时对象上绘制完成后再一次性赋值给控件。
 Bitmap map = new Bitmap(panel1.Width, panel1.Height);
            using (Graphics g = Graphics.FromImage(map))
            {
                g.Clear(SystemColors.Control);
                g.FillPie(Brushes.SlateBlue, rec, 0, 360 - availDegree);
                //计算字符串插入位置坐标
                double degree = ((360 - availDegree) / 180 * Math.PI) / 2;
                Point center = new Point(rec.X + panel1.Width / 2, rec.Y + panel1.Height / 2);
                PointF p1 = new PointF((float)(center.X + Math.Cos(degree) * (min / 2)), (float)(center.Y + Math.Sin(degree) * (min / 2)));
                g.DrawString($"已使用:{Math.Round((1 - pc.AvailablePhysicalMemory * 1.0 / pc.TotalPhysicalMemory) * 100, 2, MidpointRounding.AwayFromZero)}%", new Font("微软雅黑", 8, FontStyle.Bold), Brushes.Red, new Point(10, 10));
                g.FillPie(Brushes.Green, rec, 360 - availDegree, availDegree);

            }
            panel1.BackgroundImage = map;

3,注意点:

 protected void SetStyle(ControlStyles flag, bool value);

该方法属于protected类型位于Control类中,即只能在Control类中被调用或者在继承自Control的子类中被调用

所以在继承自Control的类Form1中可以被直接调用:

this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);   //   禁止擦除背景.

 this.SetStyle(ControlStyles.DoubleBuffer, true);   //   双缓冲

但是如果想将其应用于Form1类中定义的panel01对象则必须通过反射进行设置。

//因为该方法是保护类型protect 只能在自己类或者其继承类中调用,所以这里用反射
            System.Reflection.MethodInfo method = typeof(Panel).GetMethod("SetStyle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            method.Invoke(panel1, new object[] { ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true });

亦或是在派生自Panel的类型中进行设置。

 class CustomPanel:Panel
    {
        public CustomPanel()
        {
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        }
    }

相关推荐

  1. C#解决Winform绘图异常闪烁问题

    2024-02-03 20:56:02       46 阅读
  2. CachedNetworkImage listview 返回页面闪烁问题

    2024-02-03 20:56:02       33 阅读
  3. XML 解析异常问题解决

    2024-02-03 20:56:02       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-03 20:56:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-03 20:56:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-03 20:56:02       87 阅读
  4. Python语言-面向对象

    2024-02-03 20:56:02       96 阅读

热门阅读

  1. 1291. Sequential Digits

    2024-02-03 20:56:02       55 阅读
  2. 【前端】弹框组件

    2024-02-03 20:56:02       50 阅读
  3. MYSQL函数

    2024-02-03 20:56:02       45 阅读
  4. Elastic Search

    2024-02-03 20:56:02       57 阅读
  5. 开源软件的影响力

    2024-02-03 20:56:02       49 阅读
  6. 【MySQL】MySQL 查询两个日期内的每一天

    2024-02-03 20:56:02       48 阅读
  7. 在nodejs中使用mysql2

    2024-02-03 20:56:02       48 阅读
  8. themeleaf:入门(一)

    2024-02-03 20:56:02       44 阅读
  9. kotlin 多字段去重

    2024-02-03 20:56:02       42 阅读
  10. 【React】React预览docx文件

    2024-02-03 20:56:02       50 阅读
  11. UDP和TCP的区别和联系

    2024-02-03 20:56:02       45 阅读