WinForm透明背景文字轮廓有窗体背景色
WinForm transparent background text outline have the form background color
现在我遇到了透明键的问题。我将所需的表单(希望显示为对话框)TransparencyKey 设置为 Color.White
和 BackColor Color.White
。该表单使用的文本 ForeColor 是 ControlText
.
后台应用程序具有 white/control 背景颜色时的外观(
当对话框显示在应用程序顶部时,看起来像:
我可以看到显示透明背景的窗体,当背景色为白色时也很好看。但是当背景颜色是深色主题颜色(黑色,灰色等)时,它看起来很糟糕并且有一些白色轮廓。我应该怎么做才能防止这种情况发生?
编辑:
很抱歉缺少信息。表单中显示的文本是 Label
对象。对于设置 TransparencyKey
、BackColor
等属性的代码在 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);
}
您遇到这个问题:
我已将表单的 BackColor
和 TransparencyKey
更改为紫红色,并将 Label.ForeColor
更改为灰色。请注意 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);
}
}
您将获得:
现在我遇到了透明键的问题。我将所需的表单(希望显示为对话框)TransparencyKey 设置为 Color.White
和 BackColor Color.White
。该表单使用的文本 ForeColor 是 ControlText
.
后台应用程序具有 white/control 背景颜色时的外观(
当对话框显示在应用程序顶部时,看起来像:
我可以看到显示透明背景的窗体,当背景色为白色时也很好看。但是当背景颜色是深色主题颜色(黑色,灰色等)时,它看起来很糟糕并且有一些白色轮廓。我应该怎么做才能防止这种情况发生?
编辑:
很抱歉缺少信息。表单中显示的文本是 Label
对象。对于设置 TransparencyKey
、BackColor
等属性的代码在 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);
}
您遇到这个问题:
我已将表单的 BackColor
和 TransparencyKey
更改为紫红色,并将 Label.ForeColor
更改为灰色。请注意 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);
}
}
您将获得: