Desktop screen overlay - new form flicker issue

℡╲_俬逩灬. 提交于 2019-12-05 15:12:48

DoubleBuffered mode is needed for sure. The reason for parent form flicker is because the overlay form is activated (thus the parent form is deactivated and need to visually indicate that) before the overlay form is painted.

In order to resolve that, the overlay form needs to be shown without being activated, and then to be activated just after the first paint. The first is achieved by overriding a rarely known virtual protected property Form.ShowWithoutActivation, and the second by hooking into OnPaint method and a form level flag. Something like this

public sealed partial class DesktopOverlayForm : Form
{
    public DesktopOverlayForm()
    {
        // ...
        this.DoubleBuffered = true;
    }

    protected override bool ShowWithoutActivation { get { return true; } }

    bool activated;

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (!activated)
        {
            activated = true;
            BeginInvoke(new Action(Activate));
        }
    }
}
Ian

I did this to my flickering Form in the past. For my case, double buffer didn't really work well.

//this.DoubleBuffered = true; //doesn't work
protected override CreateParams CreateParams { //Very important to cancel flickering effect!!
  get {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
    //cp.Style &= ~0x02000000;  // Turn off WS_CLIPCHILDREN not a good idea when combined with above. Not tested alone
    return cp;
  }
}

The idea is to replace the CreateParams argument. Also see:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!