I have the following tool. The presets at the bottom are loaded from an XML file. I can't apply the gradients to the presets (labels) on load though, only when a button invokes the code.
See below, on load, and on button click:
The form is called Main, and the relevant code looks like this:
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();
}
I've commented out things I've tried. Also, I know the method of selecting the XML node is fugly... that's something I need to get round to fixing. Here's the 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>
...
Or, create your own class that inherits from the standard Label
:
public class GradientLabel : Label {
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), this.ForeColor, this.BackColor), ClientRectangle);
}
}
Then, it will be drawn constantly. Whenever you change either the BackColor
or ForeColor
property of the GradientLabel
, it will update by itself:
gradientLabelInstance.BackColor = Color.Green;