简体   繁体   English

设置 MDI Form 的背景图片

[英]Setting background Image of MDI Form

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.我的图像比窗口小,所以我使用 BackgroundImageLayout 来拉伸,但它没有任何区别。

In my MDI Form's Constructor I am using following code:在我的 MDI 表单的构造函数中,我使用以下代码:

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

It's perfectly clear why this problem is happening.很清楚为什么会出现这个问题。 MDIClient objects don't support ImageLayout.Stretch. MDIClient 对象不支持 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):尝试从类似于此的内容继承您的 MDI 表单(根据需要进行编辑):

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.显然,您需要自己处理这里的错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM