Reasons for why a WinForms label does not want to be transparent?

后端 未结 11 1781
抹茶落季
抹茶落季 2020-11-28 10:42

Why can\'t I set the BackColor of a Label to Transparent? I have done it before, but now it just don\'t want to...

I created a new UserControl, added a progressbar a

11条回答
  •  抹茶落季
    2020-11-28 11:00

    Add a new class to your project and post the code shown below. Build. Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Windows.Forms;
    
    public class TransparentLabel : Label {
      public TransparentLabel() {
        this.SetStyle(ControlStyles.Opaque, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
      }
      protected override CreateParams CreateParams {
        get {
          CreateParams parms = base.CreateParams;
          parms.ExStyle |= 0x20;  // Turn on WS_EX_TRANSPARENT
          return parms;
        }
      }
    }
    

提交回复
热议问题