私のマインスイーパゲームでは、イーパ-ミディアム-ハードを切り替えるために、動的にコントロールを作成する必要があります。質問のために、ハードは100個のボタンで構成されているとしましょう。
これが私がそれらを作成する方法です:
this.SuspendLayout(); //Creating so many things that I assume it would be faster to suspend and resume.
foreach (string i in playingField)
{
Button button = new Button();
button.Name = i;
button.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
button.Margin = new Padding(0);
button.TabIndex = 0;
button.Location = new System.Drawing.Point(3, 3);
button.Size = new System.Drawing.Size(25, 25);
button.BackgroundImage = blockImage; //##//
button.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GetUnderSide);
button.UseVisualStyleBackColor = false;
minesPanel.Controls.Add(button); //Adding the controls to the container
}
this.ResumeLayout(); //Refer to above. Correct me if I'm wrong please.
ご覧のとおり、forループを介してすべてのコントロールを作成し、それらを追加しています。その結果、各ボタンが一度に1回ペイントされます。私も試しname.Controls.AddRange(arrayButtons)
ましたが、それでも同じ問題が発生しました。個別の絵画。
私が必要としているのは、すべてのアイテムを作成し、その後、単一のビットマップをでペイントするように、それらをすべてペイントする方法ですがDoubleBuffered
、残念ながら、それも機能しません。
さらに、私はこれを試しました:
protected override CreateParams CreateParams
{
get
{
// Activate double buffering at the form level. All child controls will be double buffered as well.
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return cp;
}
}
どのような作品。アプリケーションの起動時にのみ機能します。ただし、実行時にグリッドサイズを変更し、コントロールを追加することを考えると、これは実行可能なオプションではありません。
Graphics
クラスのメソッドを使うのと同じくらい簡単だと言われました。問題は、どこから始めればいいのかわからないということです。
誰かが私のすべてのコントロールを同時にペイントするための助けを提供できれば、それは素晴らしいことです。