14

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:

  1. Create new Windows Forms application
  2. Drop a Panel on the form
  3. Give it "Green" as BackgroundColor and set Form1's TransparencyKey also to Green
  4. Run program and put Form with "hole" over something and you'll see that you can click through that hole (as described by MSDN)
  5. 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;
Episodex
  • 4,259
  • 1
  • 38
  • 55
  • No repro with XP SP3 and .NET 2.0, sorry. – Bobby Dec 15 '10 at 10:18
  • Can somebody try that with .NET 4.0? – Episodex Dec 15 '10 at 11:13
  • 1
    Yeah, this is reproducible: VS 2010, .NET 4.0, Server 2008 R2. So... +1 – Cody Gray Dec 15 '10 at 11:16
  • I can click through, no matter what color is selected. VS2010,,NET 4, XP SP3. – Damien_The_Unbeliever Dec 15 '10 at 14:00
  • 1
    While I do not know what causes it, I can tell you that you can achieve click through for 100% of colors where the red and blue channels are equal. For instance, Color.FromArgb(255, 61, 139, 61) will allow click through, but Color.FromArgb(255, 10, 20, 30) will not. – roncli Jul 10 '15 at 20:16
  • @roncli Nice find. This explains why Green from my example (0, 255, 0) works and Red (255, 0, 0) doesn't. Interesting that after almost 5 years this bug is still there. I wonder how it behaves on Windows 10. – Episodex Jul 14 '15 at 08:17
  • @Episodex Exactly the same, sadly. – roncli Sep 30 '15 at 19:21

1 Answers1

10

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.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • Thanks for your reply. I tested it and you're right. I can click through without Aero enabled. Now the funny thing is that I actually made use of this bug. I have one transparent window that I can't click through (so I can use OnMouseMove event with it) and second with other transparency key that I can click through (what is what I want). Now I know that without Aero or on XP it won't work... Fortunately it's for my personal use only ;). – Episodex Dec 17 '10 at 08:27
  • Hehe, yeah, this bug has been around for a while. When they don't fix it, it becomes a feature making our lives pretty rough. – Hans Passant Dec 17 '10 at 11:23