2フォーム作りました。Form2には背景画像を設定するボタンがあります。私はこれを得た :
this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 500));
//_1334821694552 is name of the image
ボタンをクリックしてすべてのフォームの背景を変更し、別の画像が選択されるまでその状態を維持する方法は?
2フォーム作りました。Form2には背景画像を設定するボタンがあります。私はこれを得た :
this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 500));
//_1334821694552 is name of the image
ボタンをクリックしてすべてのフォームの背景を変更し、別の画像が選択されるまでその状態を維持する方法は?
リソースの名前をアプリケーション設定に保存すると、フォームが開くたびにそこから画像を読み込むことができます。(これを機能させるには、「formBackgroundImage」という名前のアプリケーション設定が既に配置されている必要があることに注意してください)。
このようなもの:
Settings.Default["formBackgroundImage"] = "_1334821694552";
そして、Form1_Load
あなたが書いたのと同じ画像を持つべきすべてのフォームで:
this.BackgroundImage = new Bitmap( Properties.Resources.ResourceManager.GetObject(Settings.Default["formBackgroundImage"]), new Size(800, 500));
すでに開いているフォームの画像を変更したい場合は、次のことができます。
a) フォームのActivatedイベントが発生したら、backgroundImage がまだ同じかどうかを確認し、そうでない場合は変更します。
また
b) backgroundImage を変更した直後に、アプリケーションの開いているフォームをループして、そこにも設定します。
private void changeBackgroundImage()
{
//set the backgroundImage for this form
this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 500));
//save the backgroundImage in an application settings
Settings.Default["formBackgroundImage"] = "_1334821694552";
//set the backgroundImage for all open forms in the application:
foreach (Form f in Application.OpenForms) {
f.BackgroundImage = new Bitmap(Properties.Resources.ResourceManager.GetObject(Settings.Default["formBackgroundImage"]),new Size(800, 500));
}
}
これはすべて、開く予定のフォームの数と、ユーザーがフォームの画像を変更できるようにする場所の数によって異なります。多くのフォームを扱っている場合は、このページで他の人が投稿したソリューションが適切かもしれません。
すべてのフォームに同じ背景を持たせたい場合は、継承を使用する必要があります。背景情報を保持するマスター フォームを作成し、そのマスターから他のすべてのフォームを派生させます。そうすれば、各派生フォームでロジックを繰り返す必要がなくなり、すべてのフォームが同じ動作を共有します。
セットアップは非常に簡単です。
public class MasterBackgroundForm : Form
{
public override Image BackgroundImage
{
get
{
return m_backgroundImage;
}
set
{
if (m_backgroundImage != value)
{
m_backgroundImage = value;
this.OnBackgroundImageChanged(EventArgs.Empty);
}
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Set default background image.
this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552,
new Size(800, 500));
}
private static Image m_backgroundImage;
}
public class Form1 : MasterBackgroundForm
{
public Form1()
{
InitializeComponent();
}
// ... etc.
}
public class Form2 : MasterBackgroundForm
{
public Form2()
{
InitializeComponent();
}
// ... etc.
}