Keeping the image in form

前端 未结 3 958
隐瞒了意图╮
隐瞒了意图╮ 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: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.
    }
    

提交回复
热议问题