16

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;
7
  • No repro with XP SP3 and .NET 2.0, sorry.
    – Bobby
    Commented Dec 15, 2010 at 10:18
  • Can somebody try that with .NET 4.0?
    – Episodex
    Commented Dec 15, 2010 at 11:13
  • 1
    Yeah, this is reproducible: VS 2010, .NET 4.0, Server 2008 R2. So... +1 Commented Dec 15, 2010 at 11:16
  • I can click through, no matter what color is selected. VS2010,,NET 4, XP SP3. Commented Dec 15, 2010 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
    Commented Jul 10, 2015 at 20:16

1 Answer 1

12

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.

Update: user Simpleton dug in deeper into this behavior and came up with some valuable conclusions.

It appears to be related to a new Win8 feature, transparency used to be restricted to top-level windows only (Form in .NET) but starting in Win8 a child window can have it as well. Enabling that feature requires a manifest entry, when present it fixes the quirky behavior. All you have to do is to declare your app to be compatible with Win8. Project > Add New Item > pick "Application Manifest File" and ensure this entry is present:

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
    </application>
  </compatibility>
2
  • 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
    Commented Dec 17, 2010 at 8: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. Commented Dec 17, 2010 at 11:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.