在FlowLayoutPanel滚动期间,背景扭曲+闪烁

And*_*Hin 14 c# scroll transparent flicker flowlayoutpanel

我有一个具有背景的Windows窗体应用程序.在其中,我有一个透明背景的flowlayoutpanel.当我滚动时,会发生以下情况:

在此输入图像描述

我也看到一些闪烁.我已经尝试了所有的双缓冲​​业务,但它不起作用.

有什么建议?

小智 18

这对我有用.

public class CustomFlowLayoutPanel : FlowLayoutPanel
{
    public CustomFlowLayoutPanel()
        : base()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnScroll(ScrollEventArgs se)
    {
        this.Invalidate();

        base.OnScroll(se);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000; // WS_CLIPCHILDREN
            return cp;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Han*_*ant 10

是的,那不起作用.这是一个有所改进的类:

using System;
using System.Windows.Forms;

class MyFlowLayoutPanel : FlowLayoutPanel {
    public MyFlowLayoutPanel() {
        this.DoubleBuffered = true;
    }
    protected override void OnScroll(ScrollEventArgs se) {
        this.Invalidate();
        base.OnScroll(se);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译并将其从工具箱顶部拖放到表单上.然而它无法解决根本问题,"拖动时显示窗口内容"选项.这是一个系统选项,它将为更高版本的Windows打开.当它打开时,Windows本身会滚动面板的内容,然后要求应用程序绘制滚动显示的部分.OnScroll方法会覆盖该方法,确保重新绘制整个窗口以保持背景图像的位置.最终结果不是很好,你会看到图像在做"pogo",在滚动时上下跳跃.

唯一的解决方法是关闭系统选项.这不是一个实际的修复,用户喜欢这个选项,它会影响每个程序,而不仅仅是你的程序.如果你不能与pogo一起生活,那么你将不得不放弃透明度.