0

enter image description here

Video link: https://drive.google.com/open?id=1pErIL2TcE_wMNYaH0FlWxuijqSwbkYY3

There is something that I cannot translate the problem/bug as I don't know how to address this kind of problem. When the font family selection change, all the label inside the groupbox will be changed too. The code for it as per below:-

    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);
        }
    }

There is 3 code that I try to see if there any change but seems not. The font shadow trail will be disappear if I change to another tab and go back to the ScreenPage tab again.

The code for Transparent Label class is below which I use to transparent the background as it has dock fill image inside the groupbox on the bottom of all label:-

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;
            }
        }
    }
}

There is a way to overcome this?

Diado
  • 2,229
  • 3
  • 18
  • 21
Luiey
  • 843
  • 2
  • 23
  • 50
  • Why did you tag this as VB.NET? – video.baba Oct 08 '18 at 09:31
  • because maybe someone with the VB.Net facing this issue or know without difference in language since it's Winforms. I was write on VB.Net and currently moved to C# – Luiey Oct 08 '18 at 09:59
  • You are using `this.SetStyle(ControlStyles.Opaque, true);`. This means that the Control's BackGround is not painted. You need to `Invalidate()` the Control (to re-paint it) when you change the Font property. Override this property, set the Font and call `Invalidate()`. The Paint event should also repaint the BackGround (`.FillRectangle()` or `.Clear(BackColor)` or something else, depending on the internal design). There's also the chance that, using `.ExStyle |= WS_EX_TRANSPARENT;`, the Opaque style is not necessary. This one too depending on the implementation. – Jimi Oct 08 '18 at 11:04
  • @Jimi I cannot get your idea. Fyi, this class code is get from someone solutions on make the transparent background label where it will be place on top of images as currently the label unable to transparent by default. – Luiey Oct 09 '18 at 08:16
  • The snipped you posted looks much alike something I posted a couple of times (here). Or Reza Aghaei's. But he doesn't use `ControlStyles.Opaque` (that I remember). So, I know about this. As I commented earlier, when you set this style (Opaque), the underlying system doesn't draw the control's background. That can be good, if you paint it yourself. To do this, you have to invalidate the control each time you change something *visual*. The Font style is one of these. If you don't repaint the background, the previous text will persist, and you see it as *trails*. – Jimi Oct 09 '18 at 08:30
  • See [this one:](https://stackoverflow.com/questions/51396681/circle-with-text?answertab=active#tab-top). It considers the Font change. Mind that it's not fully implemented. It's just an example. – Jimi Oct 09 '18 at 08:32
  • @Jimi Regarding `internal Font m_CustomFont = new Font("Segoe UI", 9, FontStyle.Regular, GraphicsUnit.Pixel);` how to make it follow what been set in the properties or programmatically like `private void UpdateScreenSample()`? – Luiey Oct 10 '18 at 01:39
  • 1
    In reference to the linked example (btw, I've just updated it), you just need to set the Custom Label Font property value. In the linked example, the inner Font size is calculated based on the specific padding, calling the `FontAdapter()` method. You don't need to call this method. In the `OnPaint` event, comment out `this.FontAdapter(this.m_CustomFont, e.Graphics.DpiY);` Use the updated code as reference, it's quite better than before. – Jimi Oct 10 '18 at 07:32

1 Answers1

0

I have go through code by @Jimi answer linked from comment. And modified the code combined with solution from here and it a bit OK. Here is the full code with commenting

Sample Screen2Gif from solutions by Reza Aghaei here result with delay effect:-

Based from answer by Reza Aghaei

Combine with solutions from Jimi + Reza result with these:- Combine 2 solutions

Code:-

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;
            }
        }
    }
}

Open to any amendment for improvement.

Luiey
  • 843
  • 2
  • 23
  • 50