none
MDI Parent Form Image background fill

    Question

  • Hi there all good people,

    I am using MDI in my application. I would like to make my Main MDI Form (is MDI Container = true) to have an image fill for all Form. I have been tried the many examples but no one takes success results. I tried for example this one, but like always It gives me an image on Form in tile...

    How to solve this problem?

    private void frmMain_Load(object sender, EventArgs e)
            {
                foreach (Control cntrl in this.Controls)
                {
                    if (cntrl is MdiClient)
                    {
                        cntrl.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                        cntrl.BackgroundImage = Properties.Resources.Apartment_Rentals_For_Families;
                    }
                }
            }
    


    Monday, January 02, 2012 10:29 AM

Answers

  • Hi,

    I understood, that you want the background Image on your parent. The code you gave is going through all child windows and sets the background image on them.

    So maybe you want to put inside the Load handler just these lines:
    BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    BackgroundImage = Properties.Resources.Apartment_Rentals_For_Families;

    With kind regards,

    Konrad

    • Marked as answer by Paul Zhou Tuesday, January 10, 2012 7:34 AM
    Monday, January 02, 2012 11:08 AM
  • Hi,

    a construct like:

     

        public partial class Form1 : Form
        {
            private Bitmap _backBmp = null;
            private MdiClient _f = null;
    
            public Form1()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
    
                this.Width *= 3;
                this.Height *= 2;
    
                SetUpBitmap();
    
                this.BackgroundImage = _backBmp;
                //or
                //foreach (Control c in this.Controls)
                //{
                //    try
                //    {
                //        _f = (MdiClient)c;
                //        break;
                //    }
                //    catch
                //    {
    
                //    }
                //}
                //_f.BackgroundImage = _backBmp;
    
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    
                Form f = new Form();
                f.MdiParent = this;
                f.Show();
    
                this.FormClosing += delegate { if (_backBmp != null) _backBmp.Dispose(); };
            }
    
            private void SetUpBitmap()
            {
                Bitmap bOLd = this._backBmp;
    
                _backBmp = new Bitmap(400, 300);
    
                if (bOLd != null)
                    bOLd.Dispose();
    
                using (Graphics g = Graphics.FromImage(_backBmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.AliceBlue);
    
                    Random rnd = new Random();
    
                    for (int i = 0; i < 3; i++)
                    {
                        Rectangle r = new Rectangle(rnd.Next(_backBmp.Width / 4),
                            rnd.Next(_backBmp.Height / 4), rnd.Next(_backBmp.Width), rnd.Next(_backBmp.Height));
    
                        using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256))))
                        {
                            g.FillEllipse(sb, r);
                        }
                    }
    
                    string s = "MyApplication";
                    SizeF sf = g.MeasureString(s, this.Font);
                    g.DrawString(s, this.Font, Brushes.Black, new PointF((_backBmp.Width - sf.Width) / 2F, (_backBmp.Height - sf.Height) / 2F));
                }
            }
        }
    

     

    will work, but when resizing the form, things might not look as you want it to...

    You can use something like this to re-stretch the bg-image:

     

        public partial class Form1 : Form
        {
            private MdiClient _f = null;
            Rectangle _r = new Rectangle();
            bool _loaded = false;
    
            private Bitmap _backBmp = null;
    
            public Form1()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
    
                this.Width *= 3;
                this.Height *= 2;
    
                foreach (Control c in this.Controls)
                {
                    try
                    {
                        _f = (MdiClient)c;
                        break;
                    }
                    catch
                    {
    
                    }
                }
    
                SetUpBitmap();
    
                _f.Paint += new PaintEventHandler(_f_Paint);
                this.Load += new EventHandler(Form1_Load);
                _f.Layout += new LayoutEventHandler(_f_Layout);
                this.Shown += new EventHandler(Form1_Shown);
    
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            }
    
            void Form1_Shown(object sender, EventArgs e)
            {
                _loaded = true;
            }
    
            void _f_Layout(object sender, LayoutEventArgs e)
            {
                if (_f != null && _loaded)
                {
                    _r = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                    Graphics g = _f.CreateGraphics();
                    this.InvokePaint(_f, new PaintEventArgs(g, _r));
                    g.Dispose();
                }
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_backBmp != null)
                    _backBmp.Dispose();
            }
    
            private void SetUpBitmap()
            {
                Bitmap bOLd = this._backBmp;
    
                _backBmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
    
                if (bOLd != null)
                    bOLd.Dispose();
    
                using (Graphics g = Graphics.FromImage(_backBmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.AliceBlue);
    
                    Random rnd = new Random();
    
                    for (int i = 0; i < 3; i++)
                    {
                        Rectangle r = new Rectangle(rnd.Next(_backBmp.Width / 4),
                            rnd.Next(_backBmp.Height / 4), rnd.Next(_backBmp.Width), rnd.Next(_backBmp.Height));
    
                        using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256))))
                        {
                            g.FillEllipse(sb, r);
                        }
                    }
    
                    string s = "MyApplication";
                    SizeF sf = g.MeasureString(s, this.Font);
                    g.DrawString(s, this.Font, Brushes.Black, new PointF((_backBmp.Width - sf.Width) / 2F, (_backBmp.Height - sf.Height) / 2F));
                }
            }
    
            void _f_Paint(object sender, PaintEventArgs e)
            {
                if (_f != null)
                    e.Graphics.DrawImage(_backBmp, new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height));
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form f = new Form();
                f.MdiParent = this;
                f.Show();
            }
        }
    

    I'll post two more examples to play around with in the next reply (I dont know if you really need them but I like the first more than this one that stretches the complete image)

     

    Regards,

      Thorsten


    Monday, January 02, 2012 10:57 PM
  • first one:

     

        public partial class Form1 : Form
        {
            private MdiClient _f = null;
            Rectangle _r = new Rectangle();
            bool _loaded = false;
    
            private Bitmap _backBmp = null;
    
            public Form1()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
    
                this.Width *= 3;
                this.Height *= 2;
    
                foreach (Control c in this.Controls)
                {
                    try
                    {
                        _f = (MdiClient)c;
                        break;
                    }
                    catch
                    {
    
                    }
                }
    
                SetUpBitmap();
    
                _f.Paint += new PaintEventHandler(_f_Paint);
                _f.Layout += new LayoutEventHandler(_f_Layout);
    
                this.Shown += new EventHandler(Form1_Shown);
                this.Load += new EventHandler(Form1_Load);
    
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_backBmp != null)
                    _backBmp.Dispose();
            }
    
            private void SetUpBitmap()
            {
                _backBmp = new Bitmap(400, 300);
                using (Graphics g = Graphics.FromImage(_backBmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.AliceBlue);
    
                    Random rnd = new Random();
    
                    for (int i = 0; i < 3; i++)
                    {
                        Rectangle r = new Rectangle(rnd.Next(_backBmp.Width / 4),
                            rnd.Next(_backBmp.Height / 4), rnd.Next(_backBmp.Width), rnd.Next(_backBmp.Height));
    
                        using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256))))
                        {
                            g.FillEllipse(sb, r);
                        }
                    }
    
                    string s = "MyApplication";
                    SizeF sf = g.MeasureString(s, this.Font);
                    g.DrawString(s, this.Font, Brushes.Black, new PointF((_backBmp.Width - sf.Width) / 2F, (_backBmp.Height - sf.Height) / 2F));
                }
            }
    
            void Form1_Shown(object sender, EventArgs e)
            {
                _loaded = true;
            }
    
            void _f_Paint(object sender, PaintEventArgs e)
            {
                if (_f != null)
                {
                    Region reg = new Region(e.ClipRectangle);
    
                    Rectangle r = new Rectangle((_f.Width - 400) / 2, (_f.Height - 300) / 2, 400, 300);
    
                    reg.Exclude(r);
                    e.Graphics.Clip = reg;
    
                    e.Graphics.Clear(SystemColors.ControlDark);
    
                    reg.Union(r);
                    e.Graphics.Clip = reg;
    
                    e.Graphics.DrawImageUnscaled(_backBmp, r);
    
                    reg.Dispose();
                }
            }
    
            void _f_Layout(object sender, LayoutEventArgs e)
            {
                if (_f != null && _loaded)
                {
                    _r = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                    Graphics g = _f.CreateGraphics();
                    this.InvokePaint(_f, new PaintEventArgs(g, _r));
                    g.Dispose();
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form f = new Form();
                f.MdiParent = this;
                f.Show();
            }
        }
    

    ######################################################################

     

    second one, use only for testing, see the comments ...

     

        public partial class Form1 : Form
        {
            Bitmap _bmp = null;
            Rectangle _r = new Rectangle();
            Rectangle _r2 = new Rectangle();
            MdiClient _f = null;
            bool _b = false;
            Bitmap _bmp2 = null;
            bool _loaded = false;
            Bitmap _bmp2f = null;
            Rectangle _rf = new Rectangle();
            Bitmap _bmpDisplay = null;
    
            //**********************************************
            //change path in CreateBitmaps to existing image
            //**********************************************
    
            public Form1()
            {
                InitializeComponent();
                this.IsMdiContainer = true;
                //this.WindowState = FormWindowState.Maximized;
                this.Shown += new EventHandler(Form1_Shown);
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
                this.Load += new EventHandler(Form1_Load);
    
                this.ClientSize = new System.Drawing.Size(1240, 800);
    
                Init();
            }
    
            private void Init()
            {
                foreach (Control c in this.Controls)
                {
                    try
                    {
                        _f = (MdiClient)c;
                        break;
                    }
                    catch
                    {
    
                    }
                }
    
                _r = new Rectangle(0, 0, Screen.GetWorkingArea(this).Width - SystemInformation.Border3DSize.Width * 2,
                    Screen.GetWorkingArea(this).Height - SystemInformation.CaptionHeight - SystemInformation.Border3DSize.Height);
    
                CreateBitmaps();
    
                _f.BackColor = Color.Maroon;
                _f.Paint += new PaintEventHandler(_f_Paint);
                _f.Layout += new LayoutEventHandler(_f_Layout);
                _f.Click += new EventHandler(_f_Click);
            }
    
            private void CreateBitmaps()
            {
                Bitmap bmp = new Bitmap(1200, 900);
    
                //change path to existing image
                string imgPath = @"C:\Users\...\Desktop\tmp\71f.tgi.png";
                if (System.IO.File.Exists(imgPath))
                {
                    bmp.Dispose();
                    bmp = (Bitmap)Image.FromFile(imgPath);
                }
                else
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.AliceBlue);
                    }
                }
    
                _bmp = new Bitmap((int)(bmp.Width * 0.71), (int)(bmp.Height * 0.71));
                _r2 = new Rectangle((_r.Width - _bmp.Width) / 2, (_r.Height - _bmp.Height) / 2, _bmp.Width, _bmp.Height);
    
                using (Graphics g = Graphics.FromImage(_bmp))
                {
                    g.DrawImage(bmp, 0, 0, _bmp.Width, _bmp.Height);
                }
    
                bmp.Dispose();
    
                //normal Bmp
                _bmp2 = new Bitmap(_r.Width, _r.Height);
    
                using (Graphics g = Graphics.FromImage(_bmp2))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                    using (System.Drawing.Drawing2D.LinearGradientBrush l =
                        new System.Drawing.Drawing2D.LinearGradientBrush(_r, Color.Red, Color.Blue,
                            System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal))
                    {
                        g.FillRectangle(l, _r);
                    }
    
                    using (TextureBrush t = new TextureBrush(_bmp))
                    {
                        t.TranslateTransform((_r.Width - _bmp.Width) / 2, (_r.Height - _bmp.Height) / 2);
                        g.FillEllipse(t, _r2);
                    }
                }
    
                //larger Bmp for Maximized Window without border
                _rf = Screen.GetWorkingArea(this);
                _rf.Inflate(SystemInformation.Border3DSize.Width * 2, SystemInformation.Border3DSize.Height * 2);
                _bmp2f = new Bitmap(_rf.Width, _rf.Height);
    
                using (Graphics g = Graphics.FromImage(_bmp2f))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                    using (System.Drawing.Drawing2D.LinearGradientBrush l =
                        new System.Drawing.Drawing2D.LinearGradientBrush(_rf, Color.Red, Color.Blue,
                            System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal))
                    {
                        g.FillRectangle(l, _rf);
                    }
    
                    using (TextureBrush t = new TextureBrush(_bmp))
                    {
                        t.TranslateTransform((_rf.Width - _bmp.Width) / 2, (_rf.Height - _bmp.Height) / 2);
                        g.FillEllipse(t, _r2);
                    }
                }
    
                _bmpDisplay = _bmp2;
            }
    
            //there are better ways of doing this, but I wanted to
            //use only common .net objects/methods (no dll-imports)
            //see: http://dotnetrix.co.uk/form.htm
            //section: Customise an MDI application
            //and: http://vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp
            void _f_Paint(object sender, PaintEventArgs e)
            {
                if (_bmpDisplay != null)
                {
                    Region reg = new Region(e.ClipRectangle);
    
                    bool b = false;
    
                    if (!_b)
                    {
                        foreach (Control c in this.MdiChildren)
                        {
                            if (c.Location.X < 0 || c.Location.Y < 0 ||
                                c.Location.X + c.Size.Width > _f.ClientSize.Width || c.Location.Y + c.Size.Height > _f.ClientSize.Height)
                            {
                                b = true;
                                break;
                            }
                        }
    
                        if (!b)
                            e.Graphics.Clip = reg;
                    }
    
                    if (!b)
                        e.Graphics.DrawImageUnscaled(_bmpDisplay, 0, 0);
                    else
                    {
                        //this is working (in this scenario), *but* this is *not* a recommended action!
                        _b = true;
                        Graphics g = _f.CreateGraphics();
                        this.InvokePaint(_f, new PaintEventArgs(g, _r));
                        g.Dispose();
                        _b = false;
                    }
    
                    reg.Dispose();
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form f = new Form();
                //f.ControlBox = false;
                //f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                f.Click += new EventHandler(f_Click);
                f.BackColor = Color.MediumBlue;
                f.MdiParent = this;
                f.Show();
            }
    
            void Form1_Shown(object sender, EventArgs e)
            {
                _loaded = true;
            }
    
            void _f_Layout(object sender, LayoutEventArgs e)
            {
                if (_f != null && _loaded)
                {
                    _r = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                    Graphics g = _f.CreateGraphics();
                    this.InvokePaint(_f, new PaintEventArgs(g, _r));
                    g.Dispose();
                }
            }
    
            void f_Click(object sender, EventArgs e)
            {
                Form f = (Form)sender;
                f.ControlBox = !f.ControlBox;
    
                if (f.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                else
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            }
    
            void _f_Click(object sender, EventArgs e)
            {
                Form f = (Form)((MdiClient)sender).Parent;
                f.ControlBox = !f.ControlBox;
    
                if (f.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                else
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    
                if (f.WindowState == FormWindowState.Maximized && f.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
                    _bmpDisplay = _bmp2f;
                else
                    _bmpDisplay = _bmp2;
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_bmp != null)
                    _bmp.Dispose();
    
                if (_bmp2 != null)
                    _bmp2.Dispose();
    
                if (_bmp2f != null)
                    _bmp2f.Dispose();
    
                //_bmpDisplay is one of the above, no need to dispose
            }
        }
    

    Regards,

     

      Thorsten


    Monday, January 02, 2012 10:59 PM

All replies

  • Hi,

    I understood, that you want the background Image on your parent. The code you gave is going through all child windows and sets the background image on them.

    So maybe you want to put inside the Load handler just these lines:
    BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    BackgroundImage = Properties.Resources.Apartment_Rentals_For_Families;

    With kind regards,

    Konrad

    • Marked as answer by Paul Zhou Tuesday, January 10, 2012 7:34 AM
    Monday, January 02, 2012 11:08 AM
  • Hi,

    a construct like:

     

        public partial class Form1 : Form
        {
            private Bitmap _backBmp = null;
            private MdiClient _f = null;
    
            public Form1()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
    
                this.Width *= 3;
                this.Height *= 2;
    
                SetUpBitmap();
    
                this.BackgroundImage = _backBmp;
                //or
                //foreach (Control c in this.Controls)
                //{
                //    try
                //    {
                //        _f = (MdiClient)c;
                //        break;
                //    }
                //    catch
                //    {
    
                //    }
                //}
                //_f.BackgroundImage = _backBmp;
    
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    
                Form f = new Form();
                f.MdiParent = this;
                f.Show();
    
                this.FormClosing += delegate { if (_backBmp != null) _backBmp.Dispose(); };
            }
    
            private void SetUpBitmap()
            {
                Bitmap bOLd = this._backBmp;
    
                _backBmp = new Bitmap(400, 300);
    
                if (bOLd != null)
                    bOLd.Dispose();
    
                using (Graphics g = Graphics.FromImage(_backBmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.AliceBlue);
    
                    Random rnd = new Random();
    
                    for (int i = 0; i < 3; i++)
                    {
                        Rectangle r = new Rectangle(rnd.Next(_backBmp.Width / 4),
                            rnd.Next(_backBmp.Height / 4), rnd.Next(_backBmp.Width), rnd.Next(_backBmp.Height));
    
                        using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256))))
                        {
                            g.FillEllipse(sb, r);
                        }
                    }
    
                    string s = "MyApplication";
                    SizeF sf = g.MeasureString(s, this.Font);
                    g.DrawString(s, this.Font, Brushes.Black, new PointF((_backBmp.Width - sf.Width) / 2F, (_backBmp.Height - sf.Height) / 2F));
                }
            }
        }
    

     

    will work, but when resizing the form, things might not look as you want it to...

    You can use something like this to re-stretch the bg-image:

     

        public partial class Form1 : Form
        {
            private MdiClient _f = null;
            Rectangle _r = new Rectangle();
            bool _loaded = false;
    
            private Bitmap _backBmp = null;
    
            public Form1()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
    
                this.Width *= 3;
                this.Height *= 2;
    
                foreach (Control c in this.Controls)
                {
                    try
                    {
                        _f = (MdiClient)c;
                        break;
                    }
                    catch
                    {
    
                    }
                }
    
                SetUpBitmap();
    
                _f.Paint += new PaintEventHandler(_f_Paint);
                this.Load += new EventHandler(Form1_Load);
                _f.Layout += new LayoutEventHandler(_f_Layout);
                this.Shown += new EventHandler(Form1_Shown);
    
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            }
    
            void Form1_Shown(object sender, EventArgs e)
            {
                _loaded = true;
            }
    
            void _f_Layout(object sender, LayoutEventArgs e)
            {
                if (_f != null && _loaded)
                {
                    _r = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                    Graphics g = _f.CreateGraphics();
                    this.InvokePaint(_f, new PaintEventArgs(g, _r));
                    g.Dispose();
                }
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_backBmp != null)
                    _backBmp.Dispose();
            }
    
            private void SetUpBitmap()
            {
                Bitmap bOLd = this._backBmp;
    
                _backBmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
    
                if (bOLd != null)
                    bOLd.Dispose();
    
                using (Graphics g = Graphics.FromImage(_backBmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.AliceBlue);
    
                    Random rnd = new Random();
    
                    for (int i = 0; i < 3; i++)
                    {
                        Rectangle r = new Rectangle(rnd.Next(_backBmp.Width / 4),
                            rnd.Next(_backBmp.Height / 4), rnd.Next(_backBmp.Width), rnd.Next(_backBmp.Height));
    
                        using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256))))
                        {
                            g.FillEllipse(sb, r);
                        }
                    }
    
                    string s = "MyApplication";
                    SizeF sf = g.MeasureString(s, this.Font);
                    g.DrawString(s, this.Font, Brushes.Black, new PointF((_backBmp.Width - sf.Width) / 2F, (_backBmp.Height - sf.Height) / 2F));
                }
            }
    
            void _f_Paint(object sender, PaintEventArgs e)
            {
                if (_f != null)
                    e.Graphics.DrawImage(_backBmp, new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height));
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form f = new Form();
                f.MdiParent = this;
                f.Show();
            }
        }
    

    I'll post two more examples to play around with in the next reply (I dont know if you really need them but I like the first more than this one that stretches the complete image)

     

    Regards,

      Thorsten


    Monday, January 02, 2012 10:57 PM
  • first one:

     

        public partial class Form1 : Form
        {
            private MdiClient _f = null;
            Rectangle _r = new Rectangle();
            bool _loaded = false;
    
            private Bitmap _backBmp = null;
    
            public Form1()
            {
                InitializeComponent();
    
                this.IsMdiContainer = true;
    
                this.Width *= 3;
                this.Height *= 2;
    
                foreach (Control c in this.Controls)
                {
                    try
                    {
                        _f = (MdiClient)c;
                        break;
                    }
                    catch
                    {
    
                    }
                }
    
                SetUpBitmap();
    
                _f.Paint += new PaintEventHandler(_f_Paint);
                _f.Layout += new LayoutEventHandler(_f_Layout);
    
                this.Shown += new EventHandler(Form1_Shown);
                this.Load += new EventHandler(Form1_Load);
    
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_backBmp != null)
                    _backBmp.Dispose();
            }
    
            private void SetUpBitmap()
            {
                _backBmp = new Bitmap(400, 300);
                using (Graphics g = Graphics.FromImage(_backBmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.AliceBlue);
    
                    Random rnd = new Random();
    
                    for (int i = 0; i < 3; i++)
                    {
                        Rectangle r = new Rectangle(rnd.Next(_backBmp.Width / 4),
                            rnd.Next(_backBmp.Height / 4), rnd.Next(_backBmp.Width), rnd.Next(_backBmp.Height));
    
                        using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256))))
                        {
                            g.FillEllipse(sb, r);
                        }
                    }
    
                    string s = "MyApplication";
                    SizeF sf = g.MeasureString(s, this.Font);
                    g.DrawString(s, this.Font, Brushes.Black, new PointF((_backBmp.Width - sf.Width) / 2F, (_backBmp.Height - sf.Height) / 2F));
                }
            }
    
            void Form1_Shown(object sender, EventArgs e)
            {
                _loaded = true;
            }
    
            void _f_Paint(object sender, PaintEventArgs e)
            {
                if (_f != null)
                {
                    Region reg = new Region(e.ClipRectangle);
    
                    Rectangle r = new Rectangle((_f.Width - 400) / 2, (_f.Height - 300) / 2, 400, 300);
    
                    reg.Exclude(r);
                    e.Graphics.Clip = reg;
    
                    e.Graphics.Clear(SystemColors.ControlDark);
    
                    reg.Union(r);
                    e.Graphics.Clip = reg;
    
                    e.Graphics.DrawImageUnscaled(_backBmp, r);
    
                    reg.Dispose();
                }
            }
    
            void _f_Layout(object sender, LayoutEventArgs e)
            {
                if (_f != null && _loaded)
                {
                    _r = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                    Graphics g = _f.CreateGraphics();
                    this.InvokePaint(_f, new PaintEventArgs(g, _r));
                    g.Dispose();
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form f = new Form();
                f.MdiParent = this;
                f.Show();
            }
        }
    

    ######################################################################

     

    second one, use only for testing, see the comments ...

     

        public partial class Form1 : Form
        {
            Bitmap _bmp = null;
            Rectangle _r = new Rectangle();
            Rectangle _r2 = new Rectangle();
            MdiClient _f = null;
            bool _b = false;
            Bitmap _bmp2 = null;
            bool _loaded = false;
            Bitmap _bmp2f = null;
            Rectangle _rf = new Rectangle();
            Bitmap _bmpDisplay = null;
    
            //**********************************************
            //change path in CreateBitmaps to existing image
            //**********************************************
    
            public Form1()
            {
                InitializeComponent();
                this.IsMdiContainer = true;
                //this.WindowState = FormWindowState.Maximized;
                this.Shown += new EventHandler(Form1_Shown);
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
                this.Load += new EventHandler(Form1_Load);
    
                this.ClientSize = new System.Drawing.Size(1240, 800);
    
                Init();
            }
    
            private void Init()
            {
                foreach (Control c in this.Controls)
                {
                    try
                    {
                        _f = (MdiClient)c;
                        break;
                    }
                    catch
                    {
    
                    }
                }
    
                _r = new Rectangle(0, 0, Screen.GetWorkingArea(this).Width - SystemInformation.Border3DSize.Width * 2,
                    Screen.GetWorkingArea(this).Height - SystemInformation.CaptionHeight - SystemInformation.Border3DSize.Height);
    
                CreateBitmaps();
    
                _f.BackColor = Color.Maroon;
                _f.Paint += new PaintEventHandler(_f_Paint);
                _f.Layout += new LayoutEventHandler(_f_Layout);
                _f.Click += new EventHandler(_f_Click);
            }
    
            private void CreateBitmaps()
            {
                Bitmap bmp = new Bitmap(1200, 900);
    
                //change path to existing image
                string imgPath = @"C:\Users\...\Desktop\tmp\71f.tgi.png";
                if (System.IO.File.Exists(imgPath))
                {
                    bmp.Dispose();
                    bmp = (Bitmap)Image.FromFile(imgPath);
                }
                else
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.AliceBlue);
                    }
                }
    
                _bmp = new Bitmap((int)(bmp.Width * 0.71), (int)(bmp.Height * 0.71));
                _r2 = new Rectangle((_r.Width - _bmp.Width) / 2, (_r.Height - _bmp.Height) / 2, _bmp.Width, _bmp.Height);
    
                using (Graphics g = Graphics.FromImage(_bmp))
                {
                    g.DrawImage(bmp, 0, 0, _bmp.Width, _bmp.Height);
                }
    
                bmp.Dispose();
    
                //normal Bmp
                _bmp2 = new Bitmap(_r.Width, _r.Height);
    
                using (Graphics g = Graphics.FromImage(_bmp2))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                    using (System.Drawing.Drawing2D.LinearGradientBrush l =
                        new System.Drawing.Drawing2D.LinearGradientBrush(_r, Color.Red, Color.Blue,
                            System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal))
                    {
                        g.FillRectangle(l, _r);
                    }
    
                    using (TextureBrush t = new TextureBrush(_bmp))
                    {
                        t.TranslateTransform((_r.Width - _bmp.Width) / 2, (_r.Height - _bmp.Height) / 2);
                        g.FillEllipse(t, _r2);
                    }
                }
    
                //larger Bmp for Maximized Window without border
                _rf = Screen.GetWorkingArea(this);
                _rf.Inflate(SystemInformation.Border3DSize.Width * 2, SystemInformation.Border3DSize.Height * 2);
                _bmp2f = new Bitmap(_rf.Width, _rf.Height);
    
                using (Graphics g = Graphics.FromImage(_bmp2f))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                    using (System.Drawing.Drawing2D.LinearGradientBrush l =
                        new System.Drawing.Drawing2D.LinearGradientBrush(_rf, Color.Red, Color.Blue,
                            System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal))
                    {
                        g.FillRectangle(l, _rf);
                    }
    
                    using (TextureBrush t = new TextureBrush(_bmp))
                    {
                        t.TranslateTransform((_rf.Width - _bmp.Width) / 2, (_rf.Height - _bmp.Height) / 2);
                        g.FillEllipse(t, _r2);
                    }
                }
    
                _bmpDisplay = _bmp2;
            }
    
            //there are better ways of doing this, but I wanted to
            //use only common .net objects/methods (no dll-imports)
            //see: http://dotnetrix.co.uk/form.htm
            //section: Customise an MDI application
            //and: http://vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp
            void _f_Paint(object sender, PaintEventArgs e)
            {
                if (_bmpDisplay != null)
                {
                    Region reg = new Region(e.ClipRectangle);
    
                    bool b = false;
    
                    if (!_b)
                    {
                        foreach (Control c in this.MdiChildren)
                        {
                            if (c.Location.X < 0 || c.Location.Y < 0 ||
                                c.Location.X + c.Size.Width > _f.ClientSize.Width || c.Location.Y + c.Size.Height > _f.ClientSize.Height)
                            {
                                b = true;
                                break;
                            }
                        }
    
                        if (!b)
                            e.Graphics.Clip = reg;
                    }
    
                    if (!b)
                        e.Graphics.DrawImageUnscaled(_bmpDisplay, 0, 0);
                    else
                    {
                        //this is working (in this scenario), *but* this is *not* a recommended action!
                        _b = true;
                        Graphics g = _f.CreateGraphics();
                        this.InvokePaint(_f, new PaintEventArgs(g, _r));
                        g.Dispose();
                        _b = false;
                    }
    
                    reg.Dispose();
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form f = new Form();
                //f.ControlBox = false;
                //f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                f.Click += new EventHandler(f_Click);
                f.BackColor = Color.MediumBlue;
                f.MdiParent = this;
                f.Show();
            }
    
            void Form1_Shown(object sender, EventArgs e)
            {
                _loaded = true;
            }
    
            void _f_Layout(object sender, LayoutEventArgs e)
            {
                if (_f != null && _loaded)
                {
                    _r = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                    Graphics g = _f.CreateGraphics();
                    this.InvokePaint(_f, new PaintEventArgs(g, _r));
                    g.Dispose();
                }
            }
    
            void f_Click(object sender, EventArgs e)
            {
                Form f = (Form)sender;
                f.ControlBox = !f.ControlBox;
    
                if (f.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                else
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            }
    
            void _f_Click(object sender, EventArgs e)
            {
                Form f = (Form)((MdiClient)sender).Parent;
                f.ControlBox = !f.ControlBox;
    
                if (f.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                else
                    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    
                if (f.WindowState == FormWindowState.Maximized && f.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
                    _bmpDisplay = _bmp2f;
                else
                    _bmpDisplay = _bmp2;
            }
    
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_bmp != null)
                    _bmp.Dispose();
    
                if (_bmp2 != null)
                    _bmp2.Dispose();
    
                if (_bmp2f != null)
                    _bmp2f.Dispose();
    
                //_bmpDisplay is one of the above, no need to dispose
            }
        }
    

    Regards,

     

      Thorsten


    Monday, January 02, 2012 10:59 PM