winforms Text C#的外发光效果

bksxznpy  于 2022-11-16  发布在  C#
关注(0)|答案(1)|浏览(338)

我想在我的winform应用程序的标签中有外部发光文本,比如:

我在stackoverflow中搜索了它,发现了这个:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

{

  //Create a bitmap in a fixed ratio to the original drawing area.

  Bitmap bm=new Bitmap(this.ClientSize.Width/5, this.ClientSize.Height/5);

  //Create a GraphicsPath object. 

  GraphicsPath pth=new GraphicsPath();

  //Add the string in the chosen style. 

  pth.AddString("Text Halo",new FontFamily("Verdana"),(int)FontStyle.Regular,100,new Point(20,20),StringFormat.GenericTypographic);

  //Get the graphics object for the image. 

  Graphics g=Graphics.FromImage(bm);

  //Create a matrix that shrinks the drawing output by the fixed ratio. 

  Matrix mx=new Matrix(1.0f/5,0,0,1.0f/5,-(1.0f/5),-(1.0f/5));

  //Choose an appropriate smoothing mode for the halo. 

  g.SmoothingMode=SmoothingMode.AntiAlias;

  //Transform the graphics object so that the same half may be used for both halo and text output. 

  g.Transform=mx;

  //Using a suitable pen...

  Pen p=new Pen(Color.Yellow,3);

  //Draw around the outline of the path

  g.DrawPath(p,pth);

  //and then fill in for good measure. 

  g.FillPath(Brushes.Yellow,pth);

  //We no longer need this graphics object

  g.Dispose();

  //this just shifts the effect a little bit so that the edge isn't cut off in the demonstration

  e.Graphics.Transform=new Matrix(1,0,0,1,50,50);

  //setup the smoothing mode for path drawing

  e.Graphics.SmoothingMode=SmoothingMode.AntiAlias;

  //and the interpolation mode for the expansion of the halo bitmap

  e.Graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;

  //expand the halo making the edges nice and fuzzy. 

  e.Graphics.DrawImage(bm,ClientRectangle,0,0,bm.Width,bm.Height,GraphicsUnit.Pixel);

  //Redraw the original text

  e.Graphics.FillPath(Brushes.Black,pth);

  //and you're done. 

  pth.Dispose();

}

问题是我不能移动它请帮助我我需要它是可移动的,我想能够改变它的大小。上面的代码,只是自动添加到我的表单中的某个地方,但我想移动它。谢谢

093gszye

093gszye1#

一个更好的方法是创建一个自定义控件来使用/添加一些相关的绘图属性。主要是文本的字体和颜色,轮廓的大小和颜色。然后,您可以在任何容器中的任何位置以任何大小布局自定义控件。
这里有一个简单的例子。

[DesignerCategory("Code")]
public class GlowTextLabel : Control
{
    private Color outlineColor = SystemColors.Highlight;
    private int outlineSize = 1;

    public GlowTextLabel() : base()
    {
        SetStyle(ControlStyles.Selectable, false);
        SetStyle(ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.ResizeRedraw |
            ControlStyles.SupportsTransparentBackColor, true);
    }

    [DefaultValue(typeof(Color), "Highlight")]
    public Color OutlineColor
    {
        get => outlineColor;
        set
        {
            if (outlineColor != value)
            {
                outlineColor = value;
                Invalidate();
            }
        }
    }
        
    [DefaultValue(1)]
    public int OutlineSize
    {
        get => outlineSize;
        set
        {
            if (outlineSize != value)
            {
                outlineSize = Math.Max(1, value);
                Invalidate();
            }
        }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(BackColor);

        var w = Math.Max(8, ClientSize.Width / 5);
        var h = Math.Max(8, ClientSize.Height / 5);

        using (var bmp = new Bitmap(w, h))
        using (var gp = new GraphicsPath())
        using (var sf = new StringFormat(StringFormat.GenericTypographic))
        {
            sf.Alignment = sf.LineAlignment = StringAlignment.Center;

            gp.AddString(Text, 
                Font.FontFamily, (int)Font.Style, GetEmFontSize(Font), 
                ClientRectangle, sf);
               
            using (var g = Graphics.FromImage(bmp))
            using (var m = new Matrix(1.0f / 5, 0, 0, 1.0f / 5, -(1.0f / 5), -(1.0f / 5)))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.Transform = m;

                using (var pn = new Pen(OutlineColor, OutlineSize))
                {
                    g.DrawPath(pn, gp);
                    g.FillPath(pn.Brush, gp);
                }
            }

            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            // Optional for wider blur...
            // e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.DrawImage(bmp, 
                ClientRectangle, 0, 0, bmp.Width, bmp.Height, 
                GraphicsUnit.Pixel);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            using (var br = new SolidBrush(ForeColor))
                e.Graphics.FillPath(br, gp);
        }
    }
}

private float GetEmFontSize(Font fnt) =>
    fnt.SizeInPoints * (fnt.FontFamily.GetCellAscent(fnt.Style) +
    fnt.FontFamily.GetCellDescent(fnt.Style)) / fnt.FontFamily.GetEmHeight(fnt.Style);

重建,在项目的组件组下的“工具箱”中找到GlowTextLabel控件,放置一个示例,尝试使用不同的值设置FontForeColorOutlineColorOutlineSize属性。
笔宽1。

笔宽10。

笔宽20。

相关问题