我在微调控件中工作。我希望控件支持透明背色。当绘制圆弧时,中间有一个空白区域,我希望这个空间是真正透明的,这样我就可以把另一个控件放在它后面,而且它不会被微调器覆盖。在其他控件上方显示透明加载微调器
我试着重写CreateParams void。
另外我设置样式以支持TransparentColor。
尝试覆盖OnPaintBackground void,但我无法实现真正的透明背色。
那么,你能建议我做什么?
我在微调控件中工作。我希望控件支持透明背色。当绘制圆弧时,中间有一个空白区域,我希望这个空间是真正透明的,这样我就可以把另一个控件放在它后面,而且它不会被微调器覆盖。在其他控件上方显示透明加载微调器
我试着重写CreateParams void。
另外我设置样式以支持TransparentColor。
尝试覆盖OnPaintBackground void,但我无法实现真正的透明背色。
那么,你能建议我做什么?
要制作透明图层,应该重写控件的绘制并按此顺序绘制控件,第一个将位于您的控制下的所有控件(基于z-index)绘制在位图上。 然后在您的控件的图形上绘制位图。 最后绘制控件的内容。 另外您控制的BackColor
应该是Color.Transparent
。
此外,作为制作透明图层的另一选项,您可以在绘制时从控件中排除某些区域。
在下面的示例中,我使用了第一种技术并创建了2个控件。 A透明控制spinning circles。和一个transparent picturebox控制。
在这两个示例中,我使用加载行之间的延迟来显示微调器是否有意义。
样品1 - 使用SpinningCircles控制
SpinningCircles
控制绘制圆圈和支持透明度。控件在设计时没有动画,但在运行时动画。当它不可见时,它也不消耗资源。
样品2 - 使用TransparentPictureBox控制和透明动画GIF TransparentPictureBox
控制支持透明度,所以就用GIF动画作为其图像和作为你可以看到,GIF被正确显示。
示例代码1 - SpinningCircles
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
public class SpinningCircles : Control
{
int increment = 1;
int radius = 4;
int n = 8;
int next = 0;
Timer timer;
public SpinningCircles()
{
timer = new Timer();
this.Size = new Size(100, 100);
timer.Tick += (s, e) => this.Invalidate();
if (!DesignMode)
timer.Enabled = true;
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null && this.BackColor == Color.Transparent)
{
using (var bmp = new Bitmap(Parent.Width, Parent.Height))
{
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));
e.Graphics.DrawImage(bmp, -Left, -Top);
}
}
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
int length = Math.Min(Width, Height);
PointF center = new PointF(length/2, length/2);
int bigRadius = length/2 - radius - (n - 1) * increment;
float unitAngle = 360/n;
if (!DesignMode)
next++;
next = next >= n ? 0 : next;
int a = 0;
for (int i = next; i < next + n; i++)
{
int factor = i % n;
float c1X = center.X + (float)(bigRadius * Math.Cos(unitAngle * factor * Math.PI/180));
float c1Y = center.Y + (float)(bigRadius * Math.Sin(unitAngle * factor * Math.PI/180));
int currRad = radius + a * increment;
PointF c1 = new PointF(c1X - currRad, c1Y - currRad);
e.Graphics.FillEllipse(Brushes.Black, c1.X, c1.Y, 2 * currRad, 2 * currRad);
using (Pen pen = new Pen(Color.White, 2))
e.Graphics.DrawEllipse(pen, c1.X, c1.Y, 2 * currRad, 2 * currRad);
a++;
}
}
protected override void OnVisibleChanged(EventArgs e)
{
timer.Enabled = Visible;
base.OnVisibleChanged(e);
}
}
样品2码 - TransparentPictureBox代码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
class TransparentPictureBox : PictureBox
{
public TransparentPictureBox()
{
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null && this.BackColor == Color.Transparent)
{
using (var bmp = new Bitmap(Parent.Width, Parent.Height))
{
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));
e.Graphics.DrawImage(bmp, -Left, -Top);
}
}
base.OnPaint(e);
}
}
你可以看到在这个岗位透明微调。 [为什么当两个自定义控件添加了计时器时,设计器变慢?](http://stackoverflow.com/questions/36195226/why-does-the-designer-slowing-when-two-custom-controls-has-timer-添加?)这里描述了相同的技术,使一个透明的图片框和标签。 [如何用c#制作两个透明图层?](http:// stackoverflow。com/questions/36099017/how-to-make-two-transparent-layer-with-c) –
@Reza Aghaei看到这两个帖子,但我无法获得真正的透明度。我还复制了您链接的第一篇文章中的SpinningCircles控件。但是那个没有实现真正的透明背景。 – ChrisCreateBoss
至少对于我分享的第二个链接,您可以看到包含透明图片框和透明标签的屏幕截图。那究竟是什么问题? –