C# 使控件透明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9358500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a control transparent
提问by Adam H
I am currently developing a simple image editing tool using Winformsand .NET 3.5(work environment).
我目前正在使用Winforms和.NET 3.5(工作环境)开发一个简单的图像编辑工具。
I have a requirement that when the user clicks a select tool button, a square (rectangle in C#) will appear that they can scale between 100x100and 400x400. I have this bit fixed - the issue comes with making the background of the rectangle transparent.
我有一个要求,当用户单击选择工具按钮时,会出现一个正方形(C# 中的矩形),他们可以在100x100和之间缩放400x400。我已经修复了这一点 - 问题在于使矩形的背景透明。
I'm a little unclear on if transparency is supported in .NET 3.5, I've tried the following:
我有点不清楚 中是否支持透明度.NET 3.5,我尝试了以下方法:
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
pnlSelectArea.BackColor = Color.Transparent;
pnlSelectArea.ForeColor = Color.Transparent;
selectArea1.BackColor = Color.Transparent;
selectArea1.ForeColor = Color.Transparent;
But this has no effect - any advice would be appreciated.
但这没有效果 - 任何建议将不胜感激。
采纳答案by Amen Ayach
This is my special Control which contains an opacity property, it 100% works:
这是我的特殊控件,它包含一个不透明度属性,它 100% 有效:
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class TranspCtrl : Control
{
public bool drag = false;
public bool enab = false;
private int m_opacity = 100;
private int alpha;
public TranspCtrl()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
}
public int Opacity
{
get
{
if (m_opacity > 100)
{
m_opacity = 100;
}
else if (m_opacity < 1)
{
m_opacity = 1;
}
return this.m_opacity;
}
set
{
this.m_opacity = value;
if (this.Parent != null)
{
Parent.Invalidate(this.Bounds, true);
}
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | 0x20;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
Color frmColor = this.Parent.BackColor;
Brush bckColor = default(Brush);
alpha = (m_opacity * 255) / 100;
if (drag)
{
Color dragBckColor = default(Color);
if (BackColor != Color.Transparent)
{
int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragBckColor = Color.FromArgb(Rb, Gb, Bb);
}
else
{
dragBckColor = frmColor;
}
alpha = 255;
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
}
else
{
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
if (this.BackColor != Color.Transparent | drag)
{
g.FillRectangle(bckColor, bounds);
}
bckColor.Dispose();
g.Dispose();
base.OnPaint(e);
}
protected override void OnBackColorChanged(EventArgs e)
{
if (this.Parent != null)
{
Parent.Invalidate(this.Bounds, true);
}
base.OnBackColorChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e)
{
this.Invalidate();
base.OnParentBackColorChanged(e);
}
}
回答by Maheep
You will need to use Opacityproperty and set it to zero to make form invisible.
您将需要使用Opacity属性并将其设置为零以使表单不可见。
If you want to make a control Transparent, as you have tried in your example, See this article
如果您想让控件透明,正如您在示例中所尝试的那样,请参阅本文
How to: Give Your Control a Transparent Background
It say the code you have written, must be in constructor of the control. Hence, I guess, you will need to create a custom control derived from your pnlSelectArea's type most probaably a button. In in that custom control's constructor you can write code to set its style and color.
它说您编写的代码必须在控件的构造函数中。因此,我想,您将需要创建一个自定义控件,该控件派生自您pnlSelectArea的 最可能是按钮的类型。在该自定义控件的构造函数中,您可以编写代码来设置其样式和颜色。
回答by Agguro
great!! I finally managed to draw transparent shapes. I've added a virtual method
伟大的!!我终于设法绘制了透明的形状。我添加了一个虚拟方法
Draw(g);
right before
就在之前
bckColor.Dispose();
g.Dispose();
base.OnPaint(e);
and at the end the declaration of the virtual method
最后是虚方法的声明
protected virtual void Draw(Graphics g){ }
Now I can continue creating my own Transparent shapes, graphics etc ...
现在我可以继续创建自己的透明形状、图形等......
回答by user3732487
Here is what worked for me with because the other solutions did not work.
这是对我有用的方法,因为其他解决方案不起作用。
This is with transparent UserControl added to ListView/TreeView Control Collection
这是添加到 ListView/TreeView 控件集合的透明 UserControl
I know it says ButtonRenderer but it should work for any controls.
我知道它说 ButtonRenderer 但它应该适用于任何控件。
In the UserControl:
在用户控件中:
protected override void OnPaint(PaintEventArgs e)
{
ButtonRenderer.DrawParentBackground(e.Graphics, this.ClientRectangle, this);
}
in the Parent control:
在家长控制中:
protected override void WndProc(ref Message m)
{
if(m.Msg == 0xF)
foreach(Control c in this.Controls) { c.Invalidate(); c.Update(); }
base.WndProc(ref m);
}
回答by Sanke
There is one simple workaround for this. You can create an image with a transparent background (PNG) and add it for the Image property of the icon. This works fine as information does not have much flexibility in styling. Sometime this might not be suitable for everyone. Remember this is only a workaround.
对此有一种简单的解决方法。您可以创建具有透明背景 (PNG) 的图像并将其添加到图标的图像属性中。这很好用,因为信息在样式上没有太大的灵活性。有时这可能并不适合所有人。请记住,这只是一种解决方法。
PS: Add where ever the text on the image and keep blank for the text property.
PS:在图像上的任何位置添加文本,并为 text 属性留空。

