0

我正在使用这种技术,它可以很好地改变背景颜色而不是图像。我的图像比窗口小,所以我使用 BackgroundImageLayout 来拉伸,但它没有任何区别。

在我的 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;
                }
            }
4

2 回答 2

0

很清楚为什么会发生这个问题。MDIClient 对象不支持 ImageLayout.Stretch。这是记录在案的。要真正正确地做到这一点是一个巨大的痛苦。尝试从与此类似的东西继承您的 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(); }
    }
}

显然,您需要自己处理错误。

于 2015-01-06T16:12:49.563 回答
0

我认为这段代码将帮助您更正,在您的界面中设置背景图像及其填充属性......

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;
    }
于 2020-09-15T11:38:39.610 回答