想法非常单纯,就是通过重载OnPaint函数来通过识别一定的串格式,并画出彩色的效果
串格式是按\n分隔行
其次以|分隔每行中的单独元素
以|分隔的元素中,奇数字符串表示颜色,偶数表示显示单独文本
如果奇数串为空,那么就等于是label的ForeColor
代码如下:
- public partial class ColorLabel : Label
- {
- public ColorLabel()
- {
- InitializeComponent();
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- string info = Text;
- string[] datas = info.Split('\n');
- int line = 0;
-
- foreach (string data in datas)
- {
- int xoff = 0;
- if (data.IndexOf('|')>=0)
- {
- string[] text = data.Split('|');
- for(int i=0;i<text.Length;i+=2)
- {
- Color color = ForeColor;
- if (text[i]!="")
- {
- color = DrawTool.GetColorFromHtml(text[i]);
- }
- Brush brush = new SolidBrush(color);
- e.Graphics.DrawString(text[i + 1], Font, brush, xoff, Font.Height * line);
- brush.Dispose();
- xoff += (int)e.Graphics.MeasureString(text[i + 1], Font).Width-5;
- }
- }
- else
- {
- Brush brush = new SolidBrush(ForeColor);
- e.Graphics.DrawString(data, Font, brush, 0, Font.Height * line);
- brush.Dispose();
- }
- line++;
- }
-
- }
- }
|游戏中隐藏着由|#ff9900|某4种元素||按一定顺序拼接的串,你要做的就
|是猜出|#44cc00|这个顺序。
|每次猜测后,下方面板会给予一定的提示,|#ffcc00|黄色问号表示位
#ffcc00|置和符号都正确,|#66ccff|蓝色问号表示该符号存在,但位置错误。
|当完全猜对了四个元素和位置后,游戏胜利,|#ff0000|猜测次数以少
#ff0000|为佳!
上面是我程序中的一个sample
阅读(2779) | 评论(0) | 转发(0) |