简体   繁体   中英

Flickering with transparent panel

I've created a simple program and now i'm in the stages of doing my designing. I've got a multiple Panels which i make visible / invisible to switch between "tabs" (EG. 1 panel for the login screen and 1 panel for the create account screen). Now i've made these panels invisible because i want them just as containers to be able to quickly move around controls and create buttons in.

My problem is that i've set my forms background image to a image i made in photoshop and whenever i switch between panels it flickers, whenever i just use a system color (white,black) this doesn't happen. Is there any way for me to remove the flickering?

i've tried :

  • Setting double buffer to true
  • protected overrideing OnPaint, CreateBackground, and Createparam

my code is extremely basic :

private void btnNewAcc_Click(object sender, EventArgs e)
    {
        PanelNewAccount.Visible = true;
        PanelLogin.Visible = false;
    }

尝试将表单属性 DoubleBuffered 设置为 true,在 winforms 中通常会发生闪烁,因为 GDI+ 多次尝试绘制控件,因此在这种情况下,对图形进行双缓冲应该会有所帮助

form.DoubleBuffered = true;

Thanks to Patrick i've solved my problem, instead of using panels i'm using a TabControl and i assigned the same background to each tab. Just as easy to add dynamic buttons as well. Same features as panels but without the flickering.

#region .. Double Buffered function ..
       public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;
            System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            aProp.SetValue(c, true, null);
        }

       #endregion


#region .. code for Flucuring ..

       protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }

        #endregion

Even though i am late but if somebody else is also suffering from the same problem then this code fixed the flickering for me even though i dont know how it works. I found it here . Add the above code snippet in your program and in the contructor of your app add this line:

SetDoubleBuffered(YourPanelName);
protected override CreateParams CreateParams {
      get {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;
        return cp;
      }
    }

code copy from codeproject solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM