Keeping the image in form

前端 未结 3 957
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 09:43

I made 2 forms. Form2 has button which set background image. I got this :

this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 50         


        
相关标签:
3条回答
  • 2021-01-29 10:17

    If I understand you correctly, you have a form form2 which contains buttons to change the background image on the form form1.

    I wonder why you use this.BackgroundImage in form2 to change the background image? This would change the background image on form2, not on form1.

    You should actually be passing a reference to form1 to form2 and then use form1.BackgroundImage = ....

    This is all a bit vague - I suggest you edit your question to actually add some information on how the relation between form1 and form2 really is.


    First of all, you need to pass an instance of form1 to form2. This could be done in the constructor, for example, assuming that form2 is opened in form1. For example like this:

    Code in form2

    // In form2, there's a member variable that holds the form1 reference
    private form1 m_form1Instance;
    
    // The constructor of form2 could look like this
    public form2(form1 form1Instance)
    {
        ...
    
        // Remember the form1-instance
        m_form1Instance = form1Instance;
    }
    
    // When a button is clicked in form2, the background image in form1 is changed
    private void button1_Click(object sender, EventArgs e)
    {
        m_form1Instance.BackgroundImage = ...;
    }
    

    Code in form1

    // When form2 is opened, pass "this" to the constructor. The following
    // sample opens form2 as a modal dialog
    using (form2 f2 = new form2(this))
    {
        f2.ShowDialog(this);
    }
    

    That should change the background image on form1, and not on form2.

    If you want to save, which background image is displayed across sessions (i.e. you want the same background image to appear, when the program is restarted), use Wink's answer. This saves the background image used to the settings.


    OK, so it seems you want to globally change the background image on ALL the forms in your application. You have to take another way than the one I described above.

    What I'd do is create a singleton class that describes the current background image and has events to notify subscribers when the image has been changed. Like this, for example:

    public class BackgroundImageHolder
    {
        private static BackgroundImageHolder m_instance;
        private Bitmap m_backgroundImage;
    
        public event EventHandler BackgroundImageChanged;
    
        private BackgroundImageHolder() { }
    
        public static BackgroundImageHolder Instance
        {
            get
            {
                if (m_instance == null) m_instance = new BackgroundImageHolder();
                return m_instance;
            }
        }
    
        public Bitmap BackgroundImage
        {
            get { return m_backgroundImage; }
            set {
               m_backgroundImage = value;
               OnBackgroundImageChanged();
            }
        }
    
        protected void OnBackgroundImageChanged()
        {
            if (BackgroundImageChanged != null)
                BackgroundImageChanged(this, EventArgs.Empty);
        }
    }
    

    Whenever you open a form (i.e. in the constructor) you query the BackgroundImageHolder for the current image and set it for your form:

    this.BackgroundImage = BackgroundImageHolder.Instance.BackgroundImage;
    

    Then you subscribe to the notification, so the form can change the background image whenever necessary:

    BackgroundImageHolder.Instance.BackgroundImageChanged += BackgroundImageHolder_Changed;
    

    You need to implement the event handler:

    private void BackgroundImageHolder_Changed(object sender, EventArgs e)
    {
        this.BackgroundImage = BackgroundImageHolder.Instance.BackgroundImage;
    }
    

    Then, to globally change the image, it's only necessary to tell the BackgroundImageHolder about the new image:

    BackgroundImageHolder.Instance.BackgroundImage = ...;
    

    Done.

    0 讨论(0)
  • 2021-01-29 10:19

    You could save the name of the resource in an application settings and then whenever a form opens you could load the image from there. (Please note that you have to have an application settings with the name "formBackgroundImage" already in place to make this work).

    Something like this:

    Settings.Default["formBackgroundImage"] = "_1334821694552";
    

    and in Form1_Load of every form that should have the same image you write:

    this.BackgroundImage = new Bitmap( Properties.Resources.ResourceManager.GetObject(Settings.Default["formBackgroundImage"]), new Size(800, 500));
    

    If you'd like to change the image on forms that are already open you could:

    a) when the form's Activated event fires, check if the backgroundImage is still the same and change it if it isn't.

    OR

    b) Right after you change the backgroundImage, loop through the application's open forms and set them there as well:

    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));
            }
    
    } 
    

    All this depends on how many open forms you plan on having and in how many locations you give the user the ability to change the image of the form. If you're dealing with many forms then the solution posted by others on this page might be right for you.

    0 讨论(0)
  • 2021-01-29 10:39

    If you want all forms to have the same background, then you should use inheritance. Create a master form that maintains the background information, and then derive all of your other forms from that master. That way, you don't have to repeat the logic in each of the derived forms, and all forms will share the same behavior.

    It's pretty simple to set up:

    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.
    }
    
    0 讨论(0)
提交回复
热议问题