Here we go!!!!!!!
As you have found out, the BackgroundImage and BackColor properties of a
form appear to be redundant when dealing with a form where the
IsMdiContainer property is set. When the IsMdiContainer property is set, the
form displays a sunken client area with a raised border. This area is
actually a control of type 'MdiClient' with a default BackColour of
SystemColors.AppWorkspace and it covers all of the normal client area of the
form that is not occupied by other controls. I cannot emphasise enough, that
you MUST differentiate between this MdiClient control and the client area of
the form. If you fail to do so you will find yourself in deep brown stuff.
The following assumes that the BackColor property of the form is set to the
desired colour, the BackgroundImage property of the form is set to the
desired image and that the IsMdiClient property of the form is set.
You will need 3 variables with module scope:
Private m_MDIClient As MdiClient
Private m_OriginalBackgroundImage As Image
Private m_IgnoreSetBackgroundImage As Boolean
The first thing you need to do is obtain a reference to the MdiClient
control of the form and store it the appropriate variable. You can use your
favourite algorithm for doing this. The form will have only one such
control. Once you have it, you need to set its BackColor property to that of
the form. This MUST be done in the forms constructor.
In the forms Load event handler, code:
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
The next thing you need is a procedure to do the work:
Private Sub UpdateBackGround()
Dim g As Graphics
If Not (m_OriginalBackgroundImage Is Nothing) Then
If m_MDIClient.Width > 0 And m_MDIClient.Height > 0 Then
Dim b As BitMap = New BitMap(m_MDIClient.Width,
m_MDIClient.Height)
Dim g As Graphics = Me.CreateGraphics.FromImage(b)
g.Clear(Me.BackColor)
g.DrawImage(m_OriginalBackgroundImage, New
Point((m_MDIClient.Width - m_OriginalBackgroundImage.Width) / 2,
(m_MDIClient.Height - m_OriginalBackgroundImage.Height) / 2))
m_IgnoreSetBackgroundImage = True
Me.BackgroundImage = b
m_IgnoreSetBackgroundImage = false
End If
End If
End Sub
In the forms BackgroundImageChanged event handler, code:
If Not m_IgnoreSetBackgroundImage Then
m_OriginalBackgroundImage = me.BackgroundImage
UpdateBackGround()
End If
In the forms Resize event handler, code:
If Not (m_MDIClient Is Nothing) then UpdateBackGround()