在文本上具有光晕或辉光效果的Winform标签

问题描述 投票:0回答:1

以下方法是winform标签继承类的一部分。目的是对文本产生光晕或发光效果。到目前为止,效果实际上只是轮廓。它值得保留,因为它还不错,但还没有到那儿。

protected override void OnPaint(PaintEventArgs e)
{
  if (this.Text.Length == 0) return;

  e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
  Rectangle rc = new Rectangle(ClientRectangle.X + Padding.Left,
                                ClientRectangle.Y + Padding.Top,
                                ClientRectangle.Width - (Padding.Left + Padding.Right),
                                ClientRectangle.Height - (Padding.Top + Padding.Bottom));
  StringFormat fmt = new StringFormat(StringFormat.GenericTypographic)
  {
    Alignment = TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.BottomLeft
    ? StringAlignment.Near
    : TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.BottomCenter
    ? StringAlignment.Center
    : StringAlignment.Far
  };

  if ((haloSize < 1) | (haloColor == Color.Transparent))
  {
    using (var brush = new SolidBrush(this.ForeColor))
    {
      e.Graphics.DrawString(Text, Font, brush, rc, fmt);
    }
  }
  else
  {
    using (var path = new GraphicsPath())
    using (var halopen = new Pen(new SolidBrush(this.haloColor), haloSize) { LineJoin = LineJoin.Round })
    using (var brush = new SolidBrush(ForeColor))
    {
      if (fmt.Alignment == StringAlignment.Center) rc.X += rc.Width / 2;
      if (fmt.Alignment == StringAlignment.Far) rc.X += rc.Width;
      path.AddString(Text, Font.FontFamily, (int)Font.Style, rc.Height, rc.Location, fmt);
      //path.AddString(Text, Font.FontFamily, (int)Font.Style, rc.Height, rc, fmt);
      e.Graphics.DrawPath(halopen, path);
      e.Graphics.FillPath(brush, path);
    }
  }
}

两个问题。

  1. 为什么用AddStringRectangle注释掉的行不起作用,但是却与Point一起起作用?
  2. 晕光效果可以通过逐渐减小画笔大小和颜色变化来实现,但这就像在发明一个新的滚轮一样。有没有简单的方法可以做到这一点,或者已经有了一个开源库?
c# winforms label
1个回答
0
投票
  1. 原始的添加字符串对于包装盒而言太大。我认为它取消了平局,因为它是剪裁。
  2. 多次通过可以创建渐变,但是它可能不够快,无法在辉光中添加闪烁效果。

辉光/光晕效果可为恰好与文本基色匹配的背景图像提供良好的对比度。这主要是针对目标,但对于标签太长的文本则不起作用。我可能会在以后添加。

    protected override void OnPaint(PaintEventArgs e)
    {
      if (this.Text.Length == 0) return;

      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
      e.Graphics.CompositingMode = CompositingMode.SourceOver;
      Rectangle rc = new Rectangle(ClientRectangle.X + Padding.Left,
                                    ClientRectangle.Y + Padding.Top,
                                    ClientRectangle.Width - (Padding.Left + Padding.Right),
                                    ClientRectangle.Height - (Padding.Top + Padding.Bottom));
      StringFormat fmt = new StringFormat(StringFormat.GenericTypographic)
      {
        Alignment = TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.BottomLeft
        ? StringAlignment.Near
        : TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.BottomCenter
        ? StringAlignment.Center
        : StringAlignment.Far
      };
      float EmSize = rc.Height * 0.70f //Font.SizeInPoints  disreguard fontsize and fit height of label
                     * (Font.FontFamily.GetCellAscent(Font.Style) + Font.FontFamily.GetCellDescent(Font.Style))
                     / Font.FontFamily.GetEmHeight(Font.Style);
      using (var path = new GraphicsPath())
      using (var brush = new SolidBrush(ForeColor))
      {
        path.AddString(Text, Font.FontFamily, (int)Font.Style, EmSize, rc, fmt);
        if ((haloSize > 0) & (haloColor != Color.Transparent))
        {
          using (var halopen = new Pen(new SolidBrush(Color.FromArgb(255 / (int)haloSize, haloColor)), haloSize) { LineJoin = LineJoin.Round })
          {
            for (int i = (int)haloSize; i > 0; --i)
            {
              halopen.Width = i;
              e.Graphics.DrawPath(halopen, path);
            }
          }
        }
        e.Graphics.FillPath(brush, path);
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.