我有以下工具。底部的预设是从 XML 文件加载的。我不能在加载时将渐变应用于预设(标签),只有当按钮调用代码时。
见下文,加载时,点击按钮:
该表单名为Main,相关代码如下所示:
private void Main_Load(object sender, EventArgs e)
{
Stop1.BackColor = Gradient.ForeColor;
Stop2.BackColor = Gradient.BackColor;
Populate(Gradient.ForeColor);
// If XML exists etc snipped
XmlDoc = XDocument.Load("palette.xml");
IEnumerable<XElement> cssGM = XmlDoc.Root.Elements();
Label[] Label = new Label[cssGM.Count()];
for(int i = 0; i < cssGM.Count(); i++)
{
Label[i] = new Label();
Label[i].Name = "Saved" + i.ToString();
Label[i].ForeColor = ColorTranslator.FromHtml(cssGM.ElementAt(i).Elements().ElementAt(0).Elements().ElementAt(1).Value);
Label[i].BackColor = ColorTranslator.FromHtml(cssGM.ElementAt(i).Elements().ElementAt(1).Elements().ElementAt(1).Value);
Label[i].BorderStyle = BorderStyle.FixedSingle;
Label[i].Location = new System.Drawing.Point(i*54+2, 0);
Label[i].Size = new System.Drawing.Size(50, 50);
SavedPanel.Controls.Add(Label[i]);
//FillGradient(Label[i]);
//Label[i].Invalidate();
//SavedPanel.Refresh();
}
FillPalettes();
//Application.DoEvents();
//this.Invalidate();
}
public void FillPalettes()
{
foreach(Label Palette in this.SavedPanel.Controls.OfType<Label>())
{
FillGradient(Palette);
}
}
public void FillGradient(Label Target)
{
Graphics e = Target.CreateGraphics();
e.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(0, Target.Height), Target.ForeColor, Target.BackColor), ClientRectangle);
}
private void button1_Click(object sender, EventArgs e)
{
FillPalettes();
}
我已经注释掉了我尝试过的东西。另外,我知道选择 XML 节点的方法很糟糕……这是我需要解决的问题。这是 XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- cssGM - Gradient palette data -->
<cssGM Version="0.3">
<Gradient name="White to Black">>
<Stop>
<Location>0</Location>
<Colour>#fffffa</Colour>
</Stop>
<Stop>
<Location>100</Location>
<Colour>#000001</Colour>
</Stop>
</Gradient>
...