我试图与用户控制网页样式的按钮。 我需要让用户控制的背景是透明的。 如何做到这一点而不进行控制看不见的。 此外,我还需要透明的标签和图片框。
试图让这样的事情:
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
我试图与用户控制网页样式的按钮。 我需要让用户控制的背景是透明的。 如何做到这一点而不进行控制看不见的。 此外,我还需要透明的标签和图片框。
试图让这样的事情:
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
All three controls you list already have the ControlStyles.SupportsTransparentBackColor style turned on. Just set the BackColor property in the designer, Web tab. It is very unclear why that's not good enough for you.
It is otherwise an illusion, Windows Forms implements it by asking the Parent control to draw itself in the OnPaintBackground() method so it provides the background pixels. One notable thing that doesn't work is overlapping controls. You only see the Parent pixels, not the pixels of the overlapped control. That's fixable but the code is fugly.
The only other transparency option is Form.TransparencyKey. That's true transparency, implemented by using overlays in the video adapter. Problem is that this only works on toplevel windows. Forms, not controls.
These restrictions are inherent in the Windows Forms rendering model, using individual windows for the controls. A web browser doesn't have that same restriction, it emulates controls by drawing them. Layers of paint, that makes transparency trivial by just not painting. WPF uses that rendering model as well.
如果你的意思是你想要的“部分透明”,看到这样的很好的解决方案: 按钮/文本框的透明度- VB.NET
(设定使用阿尔法值的背景颜色)
我知道上面的示例是VB,但它适用于C#为好。 :)
您可以使用此代码:ImageButton的()是我的构造函数。
public ImageButton()
{
InitializeComponent();
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
}
protected override CreateParams CreateParams
{
get
{
CreateParams parms = base.CreateParams;
parms.ExStyle |= 0x20;
return parms;
}
}