在 C# 中设置带有轮廓颜色的字体 [英] Setting a Font with outline Color in C#

查看:38
本文介绍了在 C# 中设置带有轮廓颜色的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的代码中向面板动态添加标签.

I'm dynamically adding Labels to panels in my code.

我想做的是能够勾勒出字体的轮廓,使其从面板的背景颜色中脱颖而出.

Something I want to do is be able to outline the font so that it can stand out from the background color of the panel.

问题是我不知道如何使用 Winforms 在 C# 中为我的字体创建轮廓甚至阴影效果.

The problem is I don't know how to create a outline for my font or even a shadow effect in C# using Winforms.

有谁知道我应该看什么或者能指出我正确的方向吗?如果你不明白我的意思,下图是我想要的:(外衬)

Anyone know what I should look at or can point me in the right direction? If you don't understand what I mean, the following picture is what I would like: (the Outer lining)

推荐答案

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

I think you have to custom paint your own control. Here is an example for a Label. Note that it's just a demo, you should try finding out more on custom painting in 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 属性更改轮廓宽度.当您在设计器中更改这些属性时,不会立即应用效果(因为没有任何代码可以做到这一点,我想保持简短),只有在获得焦点时才会应用效果.

You can change the outline color via OutlineForeColor property, you can change the outline width via the OutlineWidth property. When you change these properties in the designer, the effect is not applied immediately (because there is not any code to do that, I want to keep it short and simple), the effect is applied only when the form is focused.

您可以添加更多的是将 TextAlign 映射到 StringFormat(命名为 sf 中的 Alignment代码),您还可以覆盖一些事件引发方法以添加对外观的更多控制(例如,当鼠标悬停在标签上时更改 ForeColor...).您甚至可以创建一些阴影效果和发光效果(它需要更多的代码).

What you can add more is mapping the TextAlign to the Alignment of the StringFormat (named sf in the code), you can also override some event raising methods to add more control over the look and feel (such as to change the ForeColor when the mouse is over the label...). You can even create some shadow effect and glow effect (it requires a little much more code).

这篇关于在 C# 中设置带有轮廓颜色的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆