使用透明键时标签上的边框
本文关键字:边框 标签 透明 | 更新日期: 2023-09-27 18:10:19
我有一个winform(带有透明度键)使其透明。但是当我添加一个标签时,我在标签周围得到了这个"fushia"边框。我知道这是因为没有"设置"背景的控制。但是有没有办法去掉这个"border"呢?
窗体背景设置为Fuchsia(透明度键Fuchsia),标签设置为transparent。试着把它画在面板上,结果也一样。
我错过了什么?
它的样子,http://oi62.tinypic.com/2wq9z7c.jpg
public class CLabel : Label
{
protected override CreateParams CreateParams { get { CreateParams parms = base.CreateParams; parms.ExStyle |= 0x20; return parms; } }
public CLabel()
{
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// null
}
GraphicsPath GetStringPath(RectangleF rect, StringFormat format)
{
GraphicsPath Path = new GraphicsPath();
Path.AddString(this.Text, this.Font.FontFamily, (int)Font.Style, this.Font.Size, rect, format);
return Path;
}
protected override void OnPaint(PaintEventArgs e)
{
RectangleF rect = this.ClientRectangle;
Font font = this.Font;
StringFormat format = StringFormat.GenericDefault;
using (GraphicsPath path = GetStringPath(rect, format))
{
SmoothingMode sm = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Brush b = new SolidBrush(Color.White);
e.Graphics.FillPath(b, path);
e.Graphics.DrawPath(Pens.Black, path);
b.Dispose();
}
}
}
public partial class Dy : Form
{
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; cp.ExStyle |= 0x80; return cp; } }
public Dy()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.StartPosition = FormStartPosition.Manual;
// Here I add the label
CLabel p = new CLabel{
Location = new Point(768, 702),
Text = "25",
AutoSize = false,
Size = new Size(50,50),
TextAlign = ContentAlignment.MiddleCenter,
BackColor = Color.Transparent,
ForeColor = Color.White,
BorderStyle = BorderStyle.None,
Font = new Font("Segoe UI", 30, FontStyle.Bold)
};
}
}
看起来像抗混叠伪影。
你可以试着修改
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
或者你可以设置反混叠完全关闭:
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
编辑:我已经把这个移出了评论,因为它可能会帮助一些人:
这里是一个小的额外的aa -工件..
AA将始终创建插值像素,这将始终导致像素在某种程度上错过透明键颜色。
显然,它们只有在不使用AA时才会消失;但如果你真的需要一些平滑,这个小技巧可能有助于改善结果:
通常TransparencyKey
用类似Fuchsia
或HotPink
的粗色来完成。这些很少使用,所以它们不太可能切割洞,但是当它们穿过半透明的AA像素时,它们是令人讨厌的。如果使用一种不那么刺激的颜色,这种效果会变得不显眼。
你只需要确保你没有在其他地方使用它。通过Color.FromArgb(255, r,g,b)
选择3个独特的值,你不会在其他地方使用,但它与前景和更重要的背景混合在一起。非常低的饱和度,根据你的图像,中等亮度可能会有帮助。
Black
或中性灰色不是很好的选择,即使它们混合得很好,因为它们经常被使用。但是选择一些几乎是黑色的东西,比如ARGB(255,7,11,5),也有很好的机会混合,而不用在图形中使用,也不用在输出中切割洞。
最好的结果是当你可以使用一些接近背景色。如果你不能预见到这一点,那么这个技巧多半会失败。