视频链接:https://drive.google.com/open?id=1pErIL2TcE_wMNYaH0FlWxuijqSwbkYY3
由于我不知道如何解决此类问题,因此我无法解决问题/错误。当字体系列选择更改时,组框内的所有标签也将更改。其代码如下:-
private void ScreenPage_FontFamilyCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (ScreenPage_FontFamilyCombobox.Text != "")
{
ScreenPage_FontFamilyNotInstalled.Visible = false;
UpdateScreenSample();
}
}
catch (Exception ex)
{
UpdateEvents("Exception ScreenPage_FontFamilyCombobox_SelectedIndexChanged. " + ex.Message);
}
}
private void UpdateScreenSample()
{
try
{
//foreach (var TransLabels in ScreenPage_SampleGroupbox.Controls.OfType<GroupBox>().SelectMany(groupBox => groupBox.Controls.OfType<CodeMess.TransparentLabel>()))
//{
// float fntSize = TransLabels.Font.Size;
// FontStyle fntStyle = TransLabels.Font.Style;
// TransLabels.Font = new Font(ScreenPage_FontFamilyCombobox.Text, fntSize, fntStyle);
//}
var TransLabels = ScreenPage_SampleGroupbox.Controls.OfType<CodeMess.TransparentLabel>();
foreach (CodeMess.TransparentLabel tL in TransLabels)
{
float fntSize = tL.Font.Size;
FontStyle fntStyle = tL.Font.Style;
tL.Font = new Font(ScreenPage_FontFamilyCombobox.Text, fntSize, fntStyle);
}
//foreach (Control control in ScreenPage_SampleGroupbox.Controls)
//{
// if (control is ApplizoneConfiguration.CodeMess.TransparentLabel transLabel)
// {
// float fntSize = control.Font.Size;
// control.Font = new Font(ScreenPage_FontFamilyCombobox.Text, fntSize, FontStyle.Regular);
// }
//}
}
catch (Exception ex)
{
UpdateEvents("Exception UpdateScreenSample. " + ex.Message);
}
}
我尝试查看3个代码,看是否有任何变化,但似乎没有。如果我切换到另一个选项卡并再次返回ScreenPage选项卡,则字体阴影轨迹将消失。
透明标签类的代码在下面,我用来透明背景,因为它在所有标签底部的组框内有停靠填充图像:-
using System;
using System.Windows.Forms;
namespace ApplizoneConfiguration.CodeMess
{
public class TransparentLabel : Label
{
public TransparentLabel()
{
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
}
protected override CreateParams CreateParams
{
get
{
CreateParams parms = base.CreateParams;
parms.ExStyle |= 0x20; // Turn on WS_EX_TRANSPARENT
return parms;
}
}
}
}
有办法克服吗?
答案 0 :(得分:0)
我已经通过{J {3}}链接的@Jimi答案来检查代码。并从这里修改了与解决方案相结合的代码,就可以了。这是带有注释的完整代码
来自Reza Aghaei comment的解决方案中的示例Screen2Gif结果具有延迟效果:-
代码:-
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Globalization;
using System.ComponentModel;
using System.Windows.Forms;
using System.Linq;
namespace Configuration.CodeMess
{
public class TransparentLabel : Label, INotifyPropertyChanged
{
internal int WS_EX_TRANSPARENT = 0x00000020;
//internal Font m_CustomFont = new Font("Segoe UI", 9, FontStyle.Regular, GraphicsUnit.Pixel);
//internal Color m_BackGroundColor;
//internal int m_InnerPadding = 20;
//internal int m_FontPadding = 5;
//internal int m_Opacity = 50;
public event PropertyChangedEventHandler PropertyChanged;
public TransparentLabel() => InitializeComponent();
public void InitializeComponent()
{
this.SetStyle(ControlStyles.Opaque |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
this.Refresh();
}
//protected override void OnPaint(PaintEventArgs e)
//{
// if (Parent != null)
// {
// 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);
// using (var b = new SolidBrush(Color.FromArgb(this.Opacity, this.TransparentBackColor)))
// {
// e.Graphics.FillRectangle(b, this.ClientRectangle);
// }
// e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
// TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Transparent);
// }
// }
//}
private int opacity;
public int Opacity
{
get { return opacity; }
set
{
if (value >= 0 && value <= 255)
opacity = value;
this.Invalidate();
}
}
public Color transparentBackColor;
public Color TransparentBackColor
{
get { return transparentBackColor; }
set
{
transparentBackColor = value;
this.Invalidate();
}
}
[Browsable(false)]
public override Color BackColor
{
get
{
return Color.Transparent;
}
set
{
base.BackColor = Color.Transparent;
}
}
//public new Font Font
//{
// get => this.m_CustomFont;
// set
// {
// this.FontAdapter(value);
// NotifyPropertyChanged(nameof(this.Font));
// }
//}
//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);
// this.BackColor = Color.FromArgb(this.m_Opacity, this.BackColor);
// NotifyPropertyChanged(nameof(this.Opacity));
// }
//}
//public override Color BackColor
//{
// get => this.m_BackGroundColor;
// set
// {
// this.m_BackGroundColor = Color.FromArgb(this.m_Opacity, value);
// base.BackColor = this.m_BackGroundColor;
// NotifyPropertyChanged(nameof(this.BackColor));
// }
//}
//private void NotifyPropertyChanged(string PropertyName)
//{
// this.Refresh();
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
//}
//protected override void OnLayout(LayoutEventArgs evt)
//{
// base.OnLayout(evt);
// base.AutoSize = false;
// this.Opacity = this.m_Opacity;
//}
protected override void OnPaint(PaintEventArgs e)
{
StringFormat format = new StringFormat(StringFormatFlags.LineLimit, CultureInfo.CurrentUICulture.LCID)
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Transparent);
//using (SolidBrush CircleBrush = new SolidBrush(this.BackColor))
//using (SolidBrush ForeBrush = new SolidBrush(this.ForeColor))
//{
// this.FontAdapter(this.m_CustomFont);
// RectangleF rect = InnerRectangle();
// e.Graphics.FillEllipse(CircleBrush, rect);
// e.Graphics.DrawString(this.Text, this.m_CustomFont, format);
//}
}
//private RectangleF InnerRectangle()
//{
// Tuple<decimal, decimal> refSize = GetMinMax(this.ClientRectangle.Height, this.ClientRectangle.Width);
// SizeF size = new SizeF((float)refSize.Item1 - (this.m_InnerPadding / 2),
// (float)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)
//{
// RectangleF rect = InnerRectangle();
// float FontSize = (CheckValue((int)(rect.Height - this.m_FontPadding), 6,
// (int)(rect.Height - this.m_FontPadding)) / 1.4F);
// using (Font customfont = new Font(font.FontFamily, FontSize, font.Style, GraphicsUnit.Pixel))
// this.m_CustomFont = (Font)customfont.Clone();
//}
//private int CheckValue(int Value, int Min, int Max)
//{
// return (Value < Min) ? Min : ((Value > Max) ? Max : Value);
//}
//private Tuple<decimal, decimal> 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<decimal, decimal>(Math.Min(Convert.ToDecimal(Value1), Convert.ToDecimal(Value2)),
// Math.Max(Convert.ToDecimal(Value1), Convert.ToDecimal(Value2)));
//}
protected override CreateParams CreateParams
{
get
{
CreateParams parms = base.CreateParams;
parms.ExStyle |= 0x20; // Turn on WS_EX_TRANSPARENT
return parms;
}
}
}
}
可以进行任何修订以进行改进。