Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318811
  • 博文数量: 23
  • 博客积分: 2115
  • 博客等级: 大尉
  • 技术积分: 371
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 16:36
文章分类

全部博文(23)

文章存档

2013年(4)

2012年(4)

2011年(3)

2010年(6)

2009年(5)

2008年(1)

我的朋友

分类: 嵌入式

2011-11-12 23:13:08

想法非常单纯,就是通过重载OnPaint函数来通过识别一定的串格式,并画出彩色的效果
串格式是按\n分隔行
其次以|分隔每行中的单独元素
以|分隔的元素中,奇数字符串表示颜色,偶数表示显示单独文本
如果奇数串为空,那么就等于是label的ForeColor
 
代码如下:
 
  1. public partial class ColorLabel : Label
  2.     {
  3.         public ColorLabel()
  4.         {
  5.             InitializeComponent();
  6.         }

  7.         protected override void OnPaint(PaintEventArgs e)
  8.         {
  9.             string info = Text;
  10.             string[] datas = info.Split('\n');
  11.             int line = 0;
  12.             
  13.             foreach (string data in datas)
  14.             {
  15.                 int xoff = 0;
  16.                 if (data.IndexOf('|')>=0)
  17.                 {
  18.                     string[] text = data.Split('|');
  19.                     for(int i=0;i<text.Length;i+=2)
  20.                     {
  21.                         Color color = ForeColor;
  22.                         if (text[i]!="")
  23.                         {
  24.                             color = DrawTool.GetColorFromHtml(text[i]);
  25.                         }
  26.                         Brush brush = new SolidBrush(color);
  27.                         e.Graphics.DrawString(text[i + 1], Font, brush, xoff, Font.Height * line);
  28.                         brush.Dispose();
  29.                         xoff += (int)e.Graphics.MeasureString(text[i + 1], Font).Width-5;
  30.                     }
  31.                 }
  32.                 else
  33.                 {
  34.                     Brush brush = new SolidBrush(ForeColor);
  35.                     e.Graphics.DrawString(data, Font, brush, 0, Font.Height * line);
  36.                     brush.Dispose();
  37.                 }
  38.                 line++;
  39.             }
  40.             
  41.         }
  42.     }
 
|游戏中隐藏着由|#ff9900|某4种元素||按一定顺序拼接的串,你要做的就
|是猜出|#44cc00|这个顺序。
|每次猜测后,下方面板会给予一定的提示,|#ffcc00|黄色问号表示位
#ffcc00|置和符号都正确,|#66ccff|蓝色问号表示该符号存在,但位置错误。
|当完全猜对了四个元素和位置后,游戏胜利,|#ff0000|猜测次数以少
#ff0000|为佳!
 
 
上面是我程序中的一个sample
阅读(2779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~