我正在微调控件中工作。我希望控件支持透明的背景色。绘制圆弧时,中间有一个空白区域,我希望该区域是真正透明的,以便可以在其后放置另一个控件,并且不会被微调器覆盖。
我尝试覆盖CreateParams无效。 我还设置了样式以支持TransparentColor。 尝试覆盖OnPaintBackground无效,但是我无法实现真正的透明
那么,您能建议我做什么?
解决方案要制作透明层,您应覆盖控件的绘制并按此顺序绘制控件, First 绘制控件所在的同一容器中的所有控件(基于z-index)在位图上。 然后在控件的图形上绘制该位图。 最后绘制控件的内容。 也
示例2-使用TransparentPictureBox控件和透明动画gif TransparentPictureBox 控件支持透明度,因此我使用了动画gif作为其图像,如您所见,该gif显示正确。
示例1代码-SpinningCircles
使用系统; 使用System.Drawing; 使用System.Drawing.Drawing2D; 使用System.Linq; 使用System.Windows.Forms; 公共类SpinningCircles:控制 { int增量= 1; int半径= 4; int n = 8; int next = 0; 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; } 受保护的覆盖无效OnPaint(PaintEventArgs e) { if(Parent!= null&& this.BackColor == Color.Transparent) { 使用(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 =长度/ 2-半径-(n-1)*增量; float unitAngle = 360 / n; if(!DesignMode) next ++; next = next> = n吗? 0:下一个; 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 =半径+ a *增量; PointF c1 =新PointF(c1X-currRad,c1Y-currRad); e.Graphics.FillEllipse(Brushes.Black,c1.X,c1.Y,2 * currRad,2 * currRad); 使用(钢笔=新的Pen(Color.White,2)) e.Graphics.DrawEllipse(pen,c1.X,c1.Y,2 * currRad,2 * currRad); a ++; } } 受保护的覆盖无效OnVisibleChanged(EventArgs e) { timer.Enabled =可见; base.OnVisibleChanged(e); } }示例2代码-TransparentPictureBox代码
使用系统; 使用System.Drawing; 使用System.Drawing.Drawing2D; 使用System.Linq; 使用System.Windows.Forms; class TransparentPictureBox:PictureBox { public TransparentPictureBox() { this.BackColor = Color.Transparent; } 受保护的覆盖无效OnPaint(PaintEventArgs e) { if(Parent!= null&& this.BackColor == Color.Transparent) { 使用(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); } }
I am working in a spinner control. I want the control to support transparent backcolor. When the arc is drawn, there is a blank space in the middle, I want that space to be truly transparent, so that I could put another control behind it and it would not be covered by the spinner.
I tried overriding the CreateParams void. Also I set the style to support TransparentColor. Tried overriding OnPaintBackground void, but I cannot achieve the real transparent backcolor.
So, what can you suggest me to do?
解决方案To make a transparent layer, you should override painting of the control and draw the control in this order, First draw all controls in the same container which are under your control (based on z-index) on a bitmap. Then draw that bitmap on graphics of your control. At last draw content of your control. Also the BackColor of your control should be Color.Transparent.
Also as another option to make transparent layer, you can exclude some regions from your control when drawing.
In the following samples I used the first technique and created 2 controls. A spinning circles transparent control. and a transparent picturebox control.
In both samples I used a delay between loading rows to showing a spinner make sense.
Sample 1 - Using a SpinningCircles Control
SpinningCircles control draws circles and supports transparency. The control doesn't animate at design-time, but it animates at run-time. Also it doesn't consume resources when it is not visible.
Sample 2 - Using a TransparentPictureBox Control and a transparent animated gif TransparentPictureBox control supports transparency, so I used an animated gif as its image and as you can see, the gif is showing correctly.
Sample 1 Code - 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); } }Sample 2 Code - TransparentPictureBox Code
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); } }
更多推荐
在其他控件上方显示透明加载微调器
发布评论