Home > OS >  WinForm transparent background text outline have the form background color
WinForm transparent background text outline have the form background color

Time:12-06

Right now I'm facing a problem of transparency key. I set the desired Form (That want to be show as a dialog) TransparencyKey as Color.White and BackColor Color.White as well. The text ForeColor of that Form using is ControlText.

How it looks when there is a background application has white/control background color (

enter image description here

When the dialog show on top of an application, things looking like:

I can see the Form showing transparent background, looking good when the background color is white as well. But when the background color is dark theme color (Something like black, grey etc.), it looks horrible and there are some white outlines. What should I do to prevent that?

EDIT: Sorry for the missing information. The text that shows in the Form is Label object. For those code that set properties like TransparencyKey, BackColor etc. are in the designer.cs. Here are the codes of both designer and .cs.

ReaderInitializer.cs

public partial class ReaderInitializer : Form
    {
        Reader _reader;

        /// <summary>
        /// Occur when being notify to close this screen.
        /// </summary>
        private event EventHandler NotifyClose;

        public ReaderInitializer()
        {
            InitializeComponent();
            NotifyClose  = OnClose;
            try
            {
                lbl_Message.Text = "Please insert the card.";
                pb_Icon.Image = new Bitmap($@"{ProgramInfo.RootPath}\img\init\load.png");
            }
            catch (Exception e)
            {
                SystemLog.Write(e);
            }
        }

        public void SetReaderNotify(Reader reader)
        {
            reader.MapSuccess  = ReaderInitSuccess;
            _reader = reader;
        }

        private void ReaderInitSuccess(object sender, MapSuccessEventArgs e)
        {
            Action uiAction = () =>
            {
                lbl_Message.Text = e.Message;
                pb_Icon.Image = e.DisplayImage;
                if (_reader != null)
                    _reader.MapSuccess -= ReaderInitSuccess;
            };

            if (InvokeRequired)
            {
                Invoke((MethodInvoker)delegate { uiAction(); });
            }
            else
            {
                uiAction();
            }

            NotifyClose?.Invoke(this, new EventArgs());
        }

        private void OnClose(object sender, EventArgs e)
        {
            // Suspecting causing hang when closing dialog.
            Task.Delay(3000).Wait();
            Close();
        }
    }

ReaderInitializer.Designer.cs

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tlp_Icon = new System.Windows.Forms.TableLayoutPanel();
            this.pn_Header = new System.Windows.Forms.Panel();
            this.pn_Footer = new System.Windows.Forms.Panel();
            this.pn_Message = new System.Windows.Forms.Panel();
            this.pb_Icon = new System.Windows.Forms.PictureBox();
            this.lbl_Message = new System.Windows.Forms.Label();
            this.tlp_Icon.SuspendLayout();
            this.pn_Message.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pb_Icon)).BeginInit();
            this.SuspendLayout();
            // 
            // tlp_Icon
            // 
            this.tlp_Icon.ColumnCount = 3;
            this.tlp_Icon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15F));
            this.tlp_Icon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 70F));
            this.tlp_Icon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15F));
            this.tlp_Icon.Controls.Add(this.pb_Icon, 1, 0);
            this.tlp_Icon.Location = new System.Drawing.Point(0, 42);
            this.tlp_Icon.Margin = new System.Windows.Forms.Padding(0);
            this.tlp_Icon.Name = "tlp_Icon";
            this.tlp_Icon.RowCount = 1;
            this.tlp_Icon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlp_Icon.Size = new System.Drawing.Size(800, 258);
            this.tlp_Icon.TabIndex = 0;
            // 
            // pn_Header
            // 
            this.pn_Header.Dock = System.Windows.Forms.DockStyle.Top;
            this.pn_Header.Location = new System.Drawing.Point(0, 0);
            this.pn_Header.Margin = new System.Windows.Forms.Padding(0);
            this.pn_Header.Name = "pn_Header";
            this.pn_Header.Size = new System.Drawing.Size(800, 42);
            this.pn_Header.TabIndex = 1;
            // 
            // pn_Footer
            // 
            this.pn_Footer.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pn_Footer.Location = new System.Drawing.Point(0, 400);
            this.pn_Footer.Margin = new System.Windows.Forms.Padding(0);
            this.pn_Footer.Name = "pn_Footer";
            this.pn_Footer.Size = new System.Drawing.Size(800, 50);
            this.pn_Footer.TabIndex = 2;
            // 
            // pn_Message
            // 
            this.pn_Message.Controls.Add(this.lbl_Message);
            this.pn_Message.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pn_Message.Location = new System.Drawing.Point(0, 300);
            this.pn_Message.Margin = new System.Windows.Forms.Padding(0);
            this.pn_Message.Name = "pn_Message";
            this.pn_Message.Size = new System.Drawing.Size(800, 100);
            this.pn_Message.TabIndex = 3;
            // 
            // pb_Icon
            // 
            this.pb_Icon.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pb_Icon.Location = new System.Drawing.Point(120, 0);
            this.pb_Icon.Margin = new System.Windows.Forms.Padding(0);
            this.pb_Icon.Name = "pb_Icon";
            this.pb_Icon.Size = new System.Drawing.Size(560, 258);
            this.pb_Icon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pb_Icon.TabIndex = 0;
            this.pb_Icon.TabStop = false;
            // 
            // lbl_Message
            // 
            this.lbl_Message.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbl_Message.Font = new System.Drawing.Font("Roboto", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbl_Message.Location = new System.Drawing.Point(0, 0);
            this.lbl_Message.Margin = new System.Windows.Forms.Padding(0);
            this.lbl_Message.Name = "lbl_Message";
            this.lbl_Message.Size = new System.Drawing.Size(800, 100);
            this.lbl_Message.TabIndex = 0;
            this.lbl_Message.Text = "...";
            this.lbl_Message.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // ReaderInitializer
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.pn_Message);
            this.Controls.Add(this.pn_Footer);
            this.Controls.Add(this.pn_Header);
            this.Controls.Add(this.tlp_Icon);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "ReaderInitializer";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Guest Service Station";
            this.TransparencyKey = System.Drawing.Color.White;
            this.tlp_Icon.ResumeLayout(false);
            this.pn_Message.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pb_Icon)).EndInit();
            this.ResumeLayout(false);

        }

CodePudding user response:

You have this problem:

SO70209408A

I've changed the Form's BackColor and TransparencyKey to Fuchsia, and the Label.ForeColor to Gray. Note how the TransparencyKey outlines the text. SO70209408B

  • Related