//我的代码如下:要是在web下编成就直接加上标签就可以了可是在form里是不是只能用GDI+画出来?
Font myfont = new Font("", 14, FontStyle.Bold);
string[] vs = { "1", "3", "4", "4", "4", "4", "4", "34", "43678", "43678", "43678", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "245", "43678", "43678", "43678", "43678" };
var vsGroup = vs.GroupBy(p => p).OrderByDescending(p => int.Parse(p.Key)).ToArray();
int w = 1;
for (var i = 0; i < vsGroup.Count(); i++)
{
Label l = new Label();
l.AutoSize = true;
l.Text = vsGroup[i].Key + "(";
l.Font = myfont;
l.Location = new Point(w, 20);
l.Width = (int)(l.Font.SizeInPoints * l.Text.Length);
w += l.Width;
this.Controls.Add(l);
l = new Label();
l.AutoSize = true;
l.Location = new Point(w, 20);
l.Font = myfont;
l.ForeColor = Color.Red;
l.Text = vsGroup[i].Count().ToString();
l.Width = (int)(l.Font.SizeInPoints * l.Text.Length);
w += l.Width;
this.Controls.Add(l);
l.AutoSize = true;
l = new Label();
l.Font = myfont;
l.Location = new Point(w, 20);
l.Text = ")";
l.Width = (int)(l.Font.SizeInPoints * l.Text.Length);
w += l.Width;
this.Controls.Add(l);
有点问题 ,达不到效果吧。而且这些new放在for循环里面,这样做不觉得有点
费内存吗?(费资源) var g=this.CreateGraphics();
g.DrawString(...); 好象不错,可以选择绘制颜色,我范围,你拼接下范围进行变色 应该
说得过去好象。。当然用不同的Label并接变色 也是一样的,
//在重绘中已经可以实现把()中的内容编成了红色,只是有点重影
//代码如下
//Label.Text="1(10)2(5)3(334)"
void l_Paint(object sender, PaintEventArgs e)
{
var l = sender as Label;
Point point = new Point(l.Padding.Left, l.Padding.Top);
var str = l.Text;
string newStr = "";
int n = 0;
while (true)
{
var index = str.IndexOf("(", n);
if (index == -1)
break;
n = str.IndexOf(")", index);
newStr = str.Substring(index + 1, n - index - 1);
//用白色文字做底色
TextRenderer.DrawText(e.Graphics, newStr.PadLeft(index + newStr.Length + 1), l.Font, point, Color.White);
//红色字体
TextRenderer.DrawText(e.Graphics, newStr.PadLeft(index + newStr.Length + 1), l.Font, point, Color.Red);
}
}
//理想的做法是把要替换成红色的文本用空格替换掉再写红色的字体