当前位置: 首页 > news >正文

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

目录

  • 前言
  • 一、常见的解决方案
  • 二、自己创建渐变色组件GradientPanel
  • 三、最终效果展示
  • 更新时间


前言

参考文章:
C#画图解决闪烁问题
[解决winform中重绘时控件闪烁的问题](panel1.GetType().GetProperty(“DoubleBuffered”,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(panel1, true, null)😉
参考视频:
C# WinForm 制作一个渐变背景Panel

没想到进行自动化页面布局后,Form之间的嵌套会出现频闪问题。
在这里插入图片描述

尝试了常见的设置双缓冲方法,给组件,给Form都试过,结果都无法解决闪屏的问题。
后来直接采用视频中的自己创建组件的方式解决了问题。


一、常见的解决方案

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

或者

this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |ControlStyles.AllPaintingInWmPaint, true);

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

  • 就是创建个.cs类,只是里面的写法没见过,哈哈哈,现在长知识了。这边将完整代码放到下面:
/** 功 能:     渐变色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:找到正确的解决方案。

http://www.mrgr.cn/news/13252.html

相关文章:

  • 线性二次调节器(LQR)和模型预测控制(MPC)算法对比介绍
  • TCP并发服务端的实现
  • 深度学习学习经验——长短期记忆网络(LSTM)
  • 学术好物!推荐8款写论文神器app查重率低网站
  • 网络通信---三次握手
  • 浅谈【数据结构】链表之单链表
  • 设计模式 9 装饰器模式
  • 【python】Gpt-embedding文本建模
  • 如何使用ssm实现学生就业管理系统
  • 【UE 编译】UE C++工程的编译流程、与C++编译的区别
  • C语言04--数组超详解
  • 如何使用ssm实现开放式教学评价管理系统+vue
  • 【操作系统】实验:指示灯开关控制
  • std::futrue异步操作结果的三种搭配使用
  • 【大模型系列篇】预训练模型:BERT GPT
  • zookeeper集群安装
  • 什么是YAML?学这一篇就够了
  • 系统编程-多线程1
  • 学懂C++(四十三):深入剖析现代C++设计模式:从常用经典到前沿的全面解析
  • 关于异常断电后jmeter的jmx文件异常变成二进制文件并成功恢复的心酸历程