我在代码中动态地将标签添加到面板中。

我想做的是能够概述字体,以便它可以从面板的背景颜色中脱颖而出。

问题是我不知道如何使用winforms在C#中为我的字体创建大纲。

有人知道我应该看什么还是可以指向正确的方向?如果您不明白我的意思,下面的图片就是我想要的:(外衬)

enter image description here

有帮助吗?

解决方案

我认为您必须自定义自己的控制。这是一个示例 Label. 。请注意,这只是一个演示,您应该尝试在Winforms中找到有关自定义绘画的更多信息:

public class CustomLabel : Label
{
    public CustomLabel()
    {
        OutlineForeColor = Color.Green;
        OutlineWidth = 2;
    }
    public Color OutlineForeColor { get; set; }
    public float OutlineWidth { get; set; }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
        using (GraphicsPath gp = new GraphicsPath())
        using (Pen outline = new Pen(OutlineForeColor, OutlineWidth)
            { LineJoin = LineJoin.Round})
        using(StringFormat sf = new StringFormat())
        using(Brush foreBrush = new SolidBrush(ForeColor))
        {
            gp.AddString(Text, Font.FontFamily, (int)Font.Style,
                Font.Size, ClientRectangle, sf);                                
            e.Graphics.ScaleTransform(1.3f, 1.35f);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.DrawPath(outline, gp);                
            e.Graphics.FillPath(foreBrush, gp);                            
        }
    }
}

您可以通过 OutlineForeColor 属性,您可以通过 OutlineWidth 财产。当您在设计人员中更改这些属性时,不会立即应用效果(因为没有任何代码可以做到这一点,我想简短而简单),仅在焦点进行表单时才能应用效果。

您可以添加更多的是映射 TextAlignAlignmentStringFormat (命名 sf 在代码中),您还可以覆盖一些事件提升方法,以增加对外观和感觉的更多控制(例如,更改 ForeColor 当鼠标覆盖在标签上时...)。您甚至可以创建一些阴影效果和发光效果(它需要更多的代码)。

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top