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 に答える