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

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

问题描述



我想要做的事情是能够勾画字体,以便它能够从面板的背景颜色。



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

任何人都知道我应该看什么,或者可以指向正确的方向?
如果你不明白我的意思,下面的图片是我想要的:(外层)

解决方案

你必须自定义绘制你自己的控制。这是一个标签的例子。请注意,这只是一个演示,您应该尝试了解更多关于自定义绘制在winforms:

  public class CustomLabel:Label 
{
public CustomLabel()
{
OutlineForeColor = Color.Green;
OutlineWidth = 2;
}
public Color OutlineForeColor {get;组; }
public float OutlineWidth {get;组; }
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(BackColor),ClientRectangle);
using(StringFormat sf = 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 属性更改轮廓宽度。当您在设计器中更改这些属性时,效果不会立即应用(因为没有任何代码可以这样做,所以我希望保持简洁),只有在表单焦点的情况下才会应用效果。 >

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




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.

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)

解决方案

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);                            
        }
    }
}

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.

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天全站免登陆