WinForm透明背景文字轮廓有表格背景颜色

Posted

技术标签:

【中文标题】WinForm透明背景文字轮廓有表格背景颜色【英文标题】:WinForm transparent background text outline have the form background color 【发布时间】:2022-01-09 12:46:29 【问题描述】:

现在我面临着透明度键的问题。我将所需的表单(希望显示为对话框)TransparencyKey 设置为Color.White 和BackColor Color.White。该表单使用的文本 ForeColor 是ControlText

当有后台应用程序具有白色/控制背景颜色时的外观(

当对话框显示在应用程序顶部时,如下所示:

我可以看到表单显示透明背景,当背景颜色为白色时也很好看。但是当背景颜色是深色主题色(黑色,灰色等)时,它看起来很可怕,并且有一些白色的轮廓。我应该怎么做才能防止这种情况发生?

编辑: 很抱歉缺少信息。表单中显示的文本是Label 对象。对于那些设置像TransparencyKeyBackColor 等属性的代码在designer.cs 中。这是设计师和.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);

        

【问题讨论】:

请插入卡片 ... 哪里来的?你是画它还是使用Label?向我们展示相关的代码或布局。如果您将TransparencyKey(白色不是一个好)更改为Color.Fuchsia,会发生什么情况?请提供一些细节。 抗锯齿像素。告诉我们谁画了文字?如果是您的 Paint 事件,您必须设置正确的图形属性.. this.TransparencyKey = System.Drawing.Color.White 是的,这是一个相当糟糕的选择;您需要一种您根本不希望出现在您的表单中的颜色.. 是的。要摆脱这些像素,请处理lbl_Message.Paint 事件,清除画布,自行设置e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;e.Graphics.DrawString。通过将对齐属性设置为中心的StringFormat。不过,使用不同的颜色来绘制字符串,使其在深色和浅色背景中可见。 @dr.null 抱歉更新晚了。请发表您的评论作为答案。虽然我有一些绘画事件实现问题,但这是另一个问题。 【参考方案1】:

你有这个问题:

我已将表单的 BackColorTransparencyKey 更改为 Fuchsia,将 Label.ForeColor 更改为 Gray。注意TransparencyKey 是如何勾勒文本的。 Here 是一个很好的解释。

建议的修复 - 上面评论 - 是自己绘制字符串,Gdi+ 方式通过 TextRenderingHint.SingleBitPerPixelGridFit 提示控制字符串渲染质量,该提示为这个问题提供了更好的抗锯齿。

// +
// System.Drawing.Text;

private void lbl_Message_Paint(object sender, PaintEventArgs e)

    e.Graphics.Clear(lbl_Message.BackColor);

    using (var sf = new StringFormat())
    using (var br = new SolidBrush(lbl_Message.ForeColor))
    
        sf.Alignment = sf.LineAlignment = StringAlignment.Center;
        e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
        e.Graphics.DrawString(lbl_Message.Text, lbl_Message.Font, br,
            lbl_Message.ClientRectangle, sf);
    

你会得到:

【讨论】:

以上是关于WinForm透明背景文字轮廓有表格背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

c#winform怎么把控件背景设为半透明,像这样

WinForm窗体,在VS2010上背景颜色不能设置透明,我把背景颜色和TransparencyK

CSS怎么设置让背景颜色透明,而文字不透明

在 CSS 中制作透明文本并在文本形状内拟合背景图像

Html表格背景颜色透明度怎么调,只用HTML和css

winform中如何使groupbox背景透明?