RainbowLabel.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:1k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. namespace PCSCustomWebControls
  7. {
  8.    public class RainbowLabel : System.Web.UI.WebControls.Label
  9.    {
  10.       private Color[] colors = new Color[] {Color.Red, Color.Orange,
  11.                                               Color.Yellow,
  12.                                               Color.GreenYellow,
  13.                                               Color.Blue, Color.Indigo,
  14.                                               Color.Violet};
  15.       private int offset
  16.       {
  17.          get
  18.          {
  19.             object rawOffset = ViewState["_offset"];
  20.             if (rawOffset != null)
  21.             {
  22.                return (int)rawOffset;
  23.             }
  24.             else
  25.             {
  26.                ViewState["_offset"] = 0;
  27.                return 0;
  28.             }
  29.          }
  30.          set
  31.          {
  32.             ViewState["_offset"] = value;
  33.          }
  34.       }
  35.       protected override void Render(HtmlTextWriter output)
  36.       {
  37.          string text = Text;
  38.          for (int pos=0; pos < text.Length; pos++)
  39.          {
  40.             int rgb = colors[(pos + offset) % colors.Length].ToArgb()
  41.                & 0xFFFFFF;
  42.             output.Write("<font color='#" + rgb.ToString("X6") + "'>"
  43.                + text[pos] + "</font>");
  44.          }
  45.       }
  46.       public void Cycle()
  47.       {
  48.          offset = ++offset; 
  49.       }
  50.    }
  51. }