作者:ql3zqi | 来源:互联网 | 2022-12-10 12:49
1> Jimi..:
这是一个源自标准的自定义控件Label
,可以制成半透明的.
界面是一个彩色圆圈,可以包含几个数字.
Control公开这些自定义属性:
Opacity
:控件的不透明度级别BackGround
[0, 255]
InnerPadding
:内部矩形之间的距离,用于定义圆边界和控件边界.
FontPadding
:文本和内部矩形之间的距离.
获取透明度覆盖CreateParams,然后进行设置ExStyle |= WS_EX_TRANSPARENT;
所述Control.SetStyle()方法被用来修改控制行为,添加这些的ControlStyles:
ControlStyles.Opaque
防止绘制控件BackGround
,因此它不受系统管理.
ControlStyles.SupportsTransparentBackColor
控件接受Alpha值的BackGround
颜色.
要查看它的工作原理,请创建一个新的Class文件,用此代码替换所有代码,
保留NameSpace并构建Project/Solution.
新的自定义控件将出现在ToolBox中.
把它放在表格上.根据需要修改其自定义属性.
控件的直观表示:
显然,ScreenToGif
忽略Pixel在完全不透明度上的变化.
它认为没有任何变化,所以它optimizes
什么也没有显示出来.
注意:由于其填充,
我在这里没有使用TextRenderer.在这种情况下更难控制:垂直中心位置需要调整,并且不提供任何质量增强.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Globalization;
using System.Windows.Forms;
[DesignerCategory("Code")]
class RoundCenterLabel : Label, INotifyPropertyChanged
{
internal const int WS_EX_TRANSPARENT = 0x00000020;
internal Font m_CustomFOnt= null;
internal Color m_BackGroundColor;
internal int m_InnerPadding = 0;
internal int m_FOntPadding= 25;
internal int m_Opacity = 128;
private readonly int fOntPadding= 8;
public event PropertyChangedEventHandler PropertyChanged;
public RoundCenterLabel() => InitializeComponent();
private void InitializeComponent()
{
this.SetStyle(ControlStyles.Opaque |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
this.m_CustomFOnt= new Font("Segoe UI", 50, FontStyle.Regular, GraphicsUnit.Pixel);
this.BackColor = Color.LimeGreen;
this.ForeColor = Color.White;
}
private void NotifyPropertyChanged(string PropertyName)
{
this.Invalidate();
this.FindForm()?.Refresh();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
public new Font Font
{
get => this.m_CustomFont;
set { this.m_CustomFOnt= value;
FontAdapter(value, this.DeviceDpi);
NotifyPropertyChanged(nameof(this.Font));
}
}
public override string Text {
get => base.Text;
set { base.Text = value;
NotifyPropertyChanged(nameof(this.Text));
}
}
public int InnerPadding {
get => this.m_InnerPadding;
set { this.m_InnerPadding = CheckValue(value, 0, this.ClientRectangle.Height - 10);
NotifyPropertyChanged(nameof(this.InnerPadding)); }
}
public int FontPadding {
get => this.m_FontPadding;
set { this.m_FOntPadding= CheckValue(value, 0, this.ClientRectangle.Height - 10);
NotifyPropertyChanged(nameof(this.FontPadding));
}
}
public int Opacity {
get => this.m_Opacity;
set { this.m_Opacity = CheckValue(value, 0, 255);
UpdateBackColor(this.m_BackGroundColor);
NotifyPropertyChanged(nameof(this.Opacity));
}
}
public override Color BackColor {
get => this.m_BackGroundColor;
set { UpdateBackColor(value);
NotifyPropertyChanged(nameof(this.BackColor));
}
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
base.AutoSize = false;
}
protected override void OnPaint(PaintEventArgs e)
{
using (StringFormat format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoWrap, CultureInfo.CurrentUICulture.LCID))
{
format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
using (SolidBrush circleBrush = new SolidBrush(this.m_BackGroundColor))
using (SolidBrush foreBrush = new SolidBrush(this.ForeColor))
{
this.FontAdapter(this.m_CustomFont, e.Graphics.DpiY);
RectangleF rect = InnerRectangle();
e.Graphics.FillEllipse(circleBrush, rect);
e.Graphics.DrawString(this.Text, this.m_CustomFont, foreBrush, rect, format);
};
};
}
private RectangleF InnerRectangle()
{
Tuple refSize = GetMinMax(this.ClientRectangle.Height, this.ClientRectangle.Width);
SizeF size = new SizeF(refSize.Item1 - (this.m_InnerPadding / 2),
refSize.Item1 - (this.m_InnerPadding / 2));
PointF position = new PointF((this.ClientRectangle.Width - size.Width) / 2,
(this.ClientRectangle.Height - size.Height) / 2);
return new RectangleF(position, size);
}
private void FontAdapter(Font font, float Dpi)
{
RectangleF rect = InnerRectangle();
float FOntSize= CheckValue((int)(rect.Height - this.m_FontPadding), 6,
(int)(rect.Height - this.m_FontPadding)) / (Dpi / 72.0F) - fontPadding;
using (Font customfOnt= new Font(font.FontFamily, FontSize, font.Style, GraphicsUnit.Pixel))
this.m_CustomFOnt= (Font)customfont.Clone();
}
private void UpdateBackColor(Color color)
{
this.m_BackGroundColor = Color.FromArgb(this.m_Opacity, Color.FromArgb(color.R, color.G, color.B));
base.BackColor = this.m_BackGroundColor;
}
private int CheckValue(int Value, int Min, int Max)
{
return (Value Max) ? Max : Value);
}
private Tuple GetMinMax(ValueType Value1, ValueType Value2)
{
if ((Value1 is Enum) || (Value1.GetType().IsNested)) return null;
if ((Value2 is Enum) || (Value2.GetType().IsNested)) return null;
return new Tuple(Math.Min(Convert.ToSingle(Value1), Convert.ToSingle(Value2)),
Math.Max(Convert.ToSingle(Value1), Convert.ToSingle(Value2)));
}
protected override CreateParams CreateParams
{
get
{
CreateParams parameters = base.CreateParams;
parameters.ExStyle |= WS_EX_TRANSPARENT;
return parameters;
}
}
}