Adding a background image that resizes along with the MDI form.

M

Matt

I've been searching around for a simple way to display a background
image in my MDI form's client area.
I want to be able to load the image from file on start up
and have it resize as the MDI form is resized.

It's the resizing part that I am having trouble with.

I've seen a few old posts that point in the right direction but
nothing simple.
I wondered if they has been any new ideas I have missed.

Working with an MDI child form is easy and the following code works a
treat.

Public Class Form2
Inherits System.Windows.Forms.Form

Private newImage As Image =
Image.FromFile("D:\Pics\Background.bmp")


Private Sub Form2_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

' Create a rectangle the size of the child form.
Dim destRect As New Rectangle(1, 1, Me.ClientSize.Width,
Me.ClientSize.Height)

' Draw image to screen.
e.Graphics.DrawImage(newImage, destRect)
End Sub
End Class

Is there anything as simple for the MDI form?
Failing that, anything at all that works?

Thank you in advance.

Matt
 
H

Herfried K. Wagner [MVP]

Matt said:
I've been searching around for a simple way to display a background
image in my MDI form's client area.
I want to be able to load the image from file on start up
and have it resize as the MDI form is resized.
[...]
Is there anything as simple for the MDI form?
Failing that, anything at all that works?

Customizing the MDI frame background
<URL:http://www.bobpowell.net/mdiback.htm>
 
G

Guest

Hi Matt

The general technique is highlighted very well at Bob Powell's site:

http://www.bobpowell.net/mdiback.htm

I've written the code that follows, and it works, but there is horrible
flashing when you resize - the background of the MdiClient control is painted
first, followed by the drawing of the Image on top. I can't see a way to
suppress the drawing of the background though - any ideas out there?

Hope it's some help anyway...

Nigel Armstrong

Code (Form called Form1.vb, with IsMdiContainer set to true...) :


Dim b As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' probably better to load this from a resource file, but this works for a
demo!
b = New Bitmap("C:\test.jpg")
For Each c As Control In Me.Controls
' Find the MdiClient control
If TypeOf c Is MdiClient Then
' Add in handlers for resize and paint
AddHandler c.Paint, AddressOf MyPaint
AddHandler c.SizeChanged, AddressOf MySizeChanged
End If
Next
End Sub

' Here are the handlers
Private Sub MyPaint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawImage(b, 0, 0, Me.ClientSize.Width,
Me.ClientSize.Height)
End Sub

Private Sub MySizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.Invalidate()
End Sub
 
M

Matt Taylor

Works really well Nigel, thanks.

The flashing is odd though but not a big deal.
If time allows I'll mess about with it.
Let me know if you come up with anything.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top