Setting background Image of MDI Form

非 Y 不嫁゛ 提交于 2021-02-08 10:00:27

问题


I am using this technique, it works well to change Background Color but not the image. My Image is Small in Size than the window so I use BackgroundImageLayout to Stretch but it's not making any difference.

In my MDI Form's Constructor I am using following code:

InitializeComponent();

            Image img = Image.FromFile("C:\\duk.jpg");
            foreach (Control control in this.Controls)
            {
                if (control is MdiClient)
                {


                    control.BackgroundImageLayout = ImageLayout.Stretch;
                    control.BackgroundImage = System.Drawing.Image.FromFile("C:\\duk.jpg");

                   // control.BackColor = Color.AliceBlue;
                    //Properties.Resources.duk;
                    MessageBox.Show("MDI");
                    break;
                }
            }

回答1:


It's perfectly clear why this problem is happening. MDIClient objects don't support ImageLayout.Stretch. This is documented. To actually do this correctly is a huge pain. Try inheriting your MDI form from something similar to this (edit as needed):

public class MdiForm : System.Windows.Forms.Form
{
    private static readonly float _bg_scale = FormGraphics.mdi_background.Width / (float)FormGraphics.mdi_background.Height;

    private MdiClient _mdi_client = null;

    private Image _background_cache = null;

    public MdiForm()
    {
        SetStyle(
            ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer, true);

        Shown += MdiForm_Shown;
        SizeChanged += MdiForm_SizeChanged;

        IsMdiContainer = true;
    }

    private void MdiForm_Shown(object sender, EventArgs eventArgs)
    {
        foreach (MdiClient control in Controls.OfType<MdiClient>())
        {
            _mdi_client = control;
            control.Paint += MdiClient_Paint;
            control.BackColor = Color.White;

            //LA LA LA I CAN'T HEAR YOU
            //this DOES work and IS required to avoid flicker
            MethodInfo mInfoMethod = typeof(MdiClient).GetMethod(
                                               "SetStyle",
                                               BindingFlags.Instance | BindingFlags.NonPublic,
                                               Type.DefaultBinder,
                                               new[] { typeof(ControlStyles), typeof(bool) },
                                               null);
            mInfoMethod.Invoke(control, new object[] {
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer, true });
        }
    }

    private void MdiClient_Paint(object sender, PaintEventArgs e)
    {
        if(_background_cache == null) { _background_cache = new Bitmap(FormGraphics.mdi_background, (int)(Height * _bg_scale), Height); }
        e.Graphics.DrawImageUnscaled(_background_cache, Point.Empty);
    }

    private void MdiForm_SizeChanged(object sender, EventArgs e)
    {
        if (_background_cache != null) { _background_cache.Dispose(); }
        _background_cache = null;
        if (_mdi_client != null) { _mdi_client.Invalidate(); }
    }
}

You're on your own for error handling here, obviously.




回答2:


I think this code will help you to correct, to set the both back image and its filled property in your interface...

private void MDIParent1_Load(object sender, EventArgs e)
    {
        BackgroundImage =              
  System.Drawing.Image.FromFile("C:\\Users\\sanoop\\Downloads\\2137969.png");
        BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    }


来源:https://stackoverflow.com/questions/15666656/setting-background-image-of-mdi-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!