在其他控件上方显示透明加载微调器

编程入门 行业动态 更新时间:2024-10-27 20:28:07
本文介绍了在其他控件上方显示透明加载微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在微调控件中工作。我希望控件支持透明的背景色。绘制圆弧时,中间有一个空白区域,我希望该区域是真正透明的,以便可以在其后放置另一个控件,并且不会被微调器覆盖。

我尝试覆盖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); } }

更多推荐

在其他控件上方显示透明加载微调器

本文发布于:2023-11-04 05:54:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   加载   透明

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!