我在Windows窗体User-Control (由一个单选按钮、三个标签和一个进度条组成)中创建了一个简单的工具条。
我将新用户控件的背景颜色设置为透明,以便当我将其拖动到窗体上时,它与窗体上的其他颜色和绘图混合在一起。我没有得到我想要实现的目标。
图片如下:

发布于 2013-01-14 01:55:53
UserControl已经支持这一点,它的ControlStyles.SupportsTransparentBackColor样式标志已经打开。您所要做的就是将BackColor属性设置为Color.Transparent。
下一件事你必须记住,这个透明度是模拟的,它是通过要求控件的父控件自己绘制背景来完成的。所以重要的是你正确地得到了父集。如果父对象不是容器控件,这就有点麻烦了。就像PictureBox一样。设计器会将窗体设置为父窗体,这样您就可以看到窗体的背景,而不是图片框。您需要在代码中修复它,编辑表单构造函数,并使其看起来类似于:
var pos = this.PointToScreen(userControl11.Location);
userControl11.Parent = pictureBox1;
userControl11.Location = pictureBox1.PointToClient(pos);发布于 2013-01-14 01:18:27
在支持透明背景色的控件的构造函数集样式中
SetStyle(ControlStyles.SupportsTransparentBackColor, true);然后将背景设置为透明颜色
this.BackColor = Color.Transparent;来自MSDN
一种更复杂的方法(可能也是可行的)是described here --覆盖了CreateParams和OnPaint。
发布于 2015-12-22 04:48:10
为什么要做这些事情?UserControl类具有属性Region。将此设置为您喜欢的任何形状,不需要其他调整。
public partial class TranspBackground : UserControl
{
public TranspBackground()
{
InitializeComponent();
}
GraphicsPath GrPath
{
get
{
GraphicsPath grPath = new GraphicsPath();
grPath.AddEllipse(this.ClientRectangle);
return grPath;
}
}
protected override void OnPaint(PaintEventArgs e)
{
// set the region property to the desired path like this
this.Region = new System.Drawing.Region(GrPath);
// other drawing goes here
e.Graphics.FillEllipse(new SolidBrush(ForeColor), ClientRectangle);
}
}结果如下图所示:

没有低级代码,没有调整,简单和干净。然而,有一个问题,但在大多数情况下,它可能没有被检测到,边缘不平滑,抗锯齿也没有帮助。但是,解决方法相当简单。事实上,比起所有这些复杂的后台处理,要容易得多。
https://stackoverflow.com/questions/14305950
复制相似问题