C#Form.TransparencyKey针对不同的颜色工作不同,为什么?

昨天我发现了一些非常奇怪的东西(我想)。 看起来Form.TransparencyKey根据使用哪种颜色作为BackgroundColorTransparencyKey给出不同的结果。 如果要重现此,请执行以下操作:

  1. 创建新的Windows窗体应用程序
  2. 在表格上删除一个Panel
  3. 将它作为BackgroundColor “Green”并将Form1的TransparencyKey也设置为Green
  4. 运行程序并将带有“洞”的表单放在某些内容上,您将看到可以单击该洞(如MSDN所述)
  5. 现在将两种颜色更改为“红色”并运行应用程序 – 您将看到“洞”,但您不再可以点击它

你知道为什么会这样吗? 规则是什么? 我正在使用带有VS2010的.NET 4,在具有相同配置的两台计算机上进行了测试。

这个代码不多……但我可以在设计师中发布设置:

 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; 

我之前听说过这个问题,但从未意识到它与TransparencyKey的选择有关。 很好找。 几乎可以肯定是由Aero引起的。 禁用它后,通过在video适配器中使用硬件覆盖来实现效果。 启用它后,桌面窗口合成function会实现它。 在DWM捕获之前,您通常可以通过非常简短的透明度颜色来判断,并使用背景中的窗口中的像素替换该区域。 关闭窗口的DWM可能会解决问题但你也会失去玻璃效果。

我可以看到小韵或颜色值的理由,看起来很随机给我。 除了bug之外,很难称之为其他任何东西。 我从来没有遇到过这种情况,我总是使用相同的透明度键。 Color.Fuchsia,一种极好的彩色色彩。 推荐的。