C# Form.TransparencyKey working different for different colors, why?
Yesterday I found something very strange (I think). It looks like Form.TransparencyKey
gives different results based on which color is used as BackgroundColor
and TransparencyKey
. If you want to reproduce this, do following:
- Create new Windows Forms application
- Drop a
Panel
on the form - Give it "Green" as
BackgroundColor
and set Form1'sTransparencyKey
also to Green - Run program and put Form with "hole" over something and you'll see that you can click through that hole (as described by MSDN)
- Now change both colors to "Red" and run application - you'll see the "hole" but you no longer can click though it
Do you know why is that happening? What's the rule? I'm using .NET 4 with VS2010, tested on two computers with same configuration.
Not much code for this... But I can post settings in designer:
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Red;
this.panel1.Location = new System.Drawing.Point(23, 26);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(229, 176);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.TransparencyKey = System.Drawing.Color.Red;
this.ResumeLayout(false);
}
//that outside:
private System.Windows.Forms.Panel panel1;
Solution 1:
I've heard of this problem before but never realized it was related to the TransparencyKey choice. Nice find. It is almost certainly caused by Aero. With it disabled, the effect is implemented by use of a hardware overlay in the video adapter. With it enabled, the desktop window compositing feature implements it. You can typically tell by a very brief flash of the transparency color before DWM catches up and replaces the area with the pixels from the windows in the background. Turning DWM off for your window might fix the problem but you'll also lose the glass effects.
I can see little rhyme or reason to the color value, looks pretty random to me. Hard to call this anything else than a bug. I never ran into this before myself, I always use the same transparency key. Color.Fuchsia, an excellent fuchsed-up color. Recommended.