C# 如何使用windows窗体应用程序C在母版页中设置背景图像?

C# 如何使用windows窗体应用程序C在母版页中设置背景图像?,c#,image,winforms,C#,Image,Winforms,我在主窗体中设置了背景图像,然后在子窗体中调用它不工作 我在主窗体的属性中设置了背景图像。如果我把任何其他工具,如按钮面板,它的子窗体上的工作 主表格 public partial class MasterForm: Form { public MasterForm() { InitializeComponent(); } } 子窗体 我这样叫你 public partial class item: MasterForm { } 我认为它不起作用,因为

我在主窗体中设置了背景图像,然后在子窗体中调用它不工作

我在主窗体的属性中设置了背景图像。如果我把任何其他工具,如按钮面板,它的子窗体上的工作

主表格

public partial class MasterForm: Form
{
    public MasterForm()
    {
        InitializeComponent();
    }
}
子窗体

我这样叫你

public partial class item: MasterForm
{
}

我认为它不起作用,因为每个页面都有自己的属性。你必须在母版中设置图像

public partial class MasterForm: Form
{
    public MasterForm()
    {
        InitializeComponent();
        this.BackgroundImage = Properties.Resources.Image;
    }
}
请看这里:

  • 首先创建BaseMasterForm.cs并继承表单。如果您想在构造函数中添加一些内容,请编写代码
  • 其次,创建子Form.cs并继承BaseMasterForm。您可以覆盖“显示背景”。如果要显示背景,则需要设置为true。
  • BaseMasterForm.cs

    public class BaseMasterForm : Form
    {
        // You can override this Show Background. If you want or not to show background.
        public virtual bool ShowBackground { get { return true; } }
    
        public BaseMasterForm()
        {
            this.Load += (s, e) =>
            {
                // your image
                this.BackgroundImage = (ShowBackground) ? Properties.Resources.LeeQc : null;
                // you can custom
                this.BackgroundImageLayout = (ShowBackground) ? ImageLayout.Stretch : ImageLayout.None;
            };
        }
    }
    
    ChildForm.cs

    public partial class ChildForm : BaseMasterForm
    {
        // set true or false. to show MasterForm background.
        public override bool ShowBackground { get { return true; } }
    
        public ChildForm()
        {
            InitializeComponent();
        }
    }
    

    “然后我打电话给儿童表格,它不工作了”确切地说是什么?子窗体没有出现吗?或者背景图像的设置在子窗体中不起作用吗?@Mong Zhu我使用的是WinForms而不是webapplication@Mong朱子窗体不显示背景image@Mong朱:我在主窗体的属性中设置了背景图像,所以我这样说。我需要调用和显示子窗体的代码。因为当我尝试它时,我的子窗体具有相同的背景图像!但它也会有相同的控制元素布局!工作完美。谢谢大家的指导