0

私は次のツールを持っています。下部のプリセットは、XML ファイルから読み込まれます。ただし、ボタンがコードを呼び出す場合にのみ、ロード時にグラデーションをプリセット (ラベル) に適用することはできません。

以下、ロード時、およびボタンクリック時を参照してください。

CSSGM1 CSSGM2

フォームは 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>
  ...
4

2 に答える 2

1

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;
于 2013-01-29T02:49:01.213 に答える
0

FormLoadedイベントは、Form がレイアウトされる前に発生します。おそらく、すべてのコントロールがまだ準備できていません。

Shownそれまでにすべてのコントロールの準備ができているはずなので、イベントを使用してみることができます。

    private void Main_Shown(object sender, EventArgs e)
    {
        // all your code;
    }

古き良きWinformの時代から覚えている限り、イベントはこの順序で行われています

  1. ロード済み
  2. CreateControl
  3. オンアクティベート
  4. 表示
  5. オンペイント

CreateControlイベントが作成されるのを待つ必要があるSavedPanelため、Shownイベントは問題ありません。

于 2013-01-29T02:41:32.617 に答える