记录|Form1中嵌套Form2时的频闪问题解决[不同于常见的三部曲]

目录


前言

参考文章:
C#画图解决闪烁问题

解决winform中重绘时控件闪烁的问题\](panel1.GetType().GetProperty("DoubleBuffered",System.Reflection.BindingFlags.Instance \| System.Reflection.BindingFlags.NonPublic).SetValue(panel1, true, null)😉 参考视频: [C# WinForm 制作一个渐变背景Panel](https://www.bilibili.com/video/BV1V64y1F7Ny/?spm_id_from=333.337.search-card.all.click&vd_source=b52581a539d7e1c5d09635d747995504)

没想到进行自动化页面布局后,Form之间的嵌套会出现频闪问题。

尝试了常见的设置双缓冲方法,给组件,给Form都试过,结果都无法解决闪屏的问题。

后来直接采用视频中的自己创建组件的方式解决了问题。


一、常见的解决方案

  • 就是开启组件的双缓冲方法
csharp 复制代码
panel1.GetType().GetProperty("DoubleBuffered",System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(panel1, true, null);

或者

csharp 复制代码
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |ControlStyles.AllPaintingInWmPaint, true);

二、自己创建渐变色组件GradientPanel

  • 就是创建个.cs类,只是里面的写法没见过,哈哈哈,现在长知识了。这边将完整代码放到下面:
csharp 复制代码
/*
 * 功 能:     渐变色Panel
 * 创建时间:  2024/8/27 21:18:19
 * 创建人:     小白鼠零号
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZHCH_winform_2.manager
{
    public class GradientPanel : Panel
    {
        private Color exColor1 = Color.Lime;
        [Category("EX属性")]
        [DefaultValue(typeof(Color), "Lime")]
        public Color EXColor1
        {
            get { return exColor1; }
            set { exColor1 = value; Invalidate(); }
        }

        private Color exColor2 = Color.Blue;
        [Category("EX属性")]
        [DefaultValue(typeof(Color), "Blue")]
        public Color EXColor2
        {
            get { return exColor2; }
            set { exColor2 = value; Invalidate(); }
        }

        private float exAngle = 45f;
        [Category("EX属性")]
        [DefaultValue(typeof(float), "45")]
        public float EXAngle
        {
            get { return exAngle; }
            set { exAngle = value; Invalidate(); }
        }
        public GradientPanel()
        {
            //DoubleBuffered = true;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, exColor1, exColor2, exAngle);
            e.Graphics.FillRectangle(brush, ClientRectangle);
        }

    }
}

三、最终效果展示


更新时间

  • 2024.08.27:创建
  • 2024.08.28:找到正确的解决方案。
相关推荐
mudtools11 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫14 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
CoovallyAIHub1 天前
微软发布 Visual Studio 2026 Insider:AI深度集成,性能大提升,让开发效率倍增(附下载地址)
后端·编程语言·visual studio
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程2 天前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#