|
-
Nov 2nd, 2005, 12:19 AM
#1
Thread Starter
Member
[ask] change background in mdi parent form(runtime) ?
Hello all, I would like to ask how to change background of a mdi parent form in runtime from a file(or from a stream because i would like to get the image from database).Thx Before
-
Nov 2nd, 2005, 01:50 AM
#2
Re: [ask] change background in mdi parent form(runtime) ?
You cannot actually see the background of an MDI parent window. When you make a window an MDI container, an MdiClient control is placed on the form and all the child windows are contained within that. You are not intended to access this control directly, but you can. Just be aware that you are not supposed to and I can't guarantee that there would not be side-effects:
VB Code:
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackgroundImage = myImage
Exit For
End If
Next ctl
-
Nov 2nd, 2005, 02:16 AM
#3
Thread Starter
Member
Re: [ask] change background in mdi parent form(runtime) ?
 Originally Posted by jmcilhinney
You cannot actually see the background of an MDI parent window. When you make a window an MDI container, an MdiClient control is placed on the form and all the child windows are contained within that. You are not intended to access this control directly, but you can. Just be aware that you are not supposed to and I can't guarantee that there would not be side-effects:
VB Code:
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackgroundImage = myImage
Exit For
End If
Next ctl
Im sorry, but it did not work, as I placed that code in the FrmLoad event
-
Nov 2nd, 2005, 02:35 AM
#4
Re: [ask] change background in mdi parent form(runtime) ?
Well then you've done something wrong because here's my code:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackgroundImage = Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg")
Exit For
End If
Next
Dim child As New Form
child.Text = "Child"
child.Size = New Size(150, 150)
child.MdiParent = Me
child.Show()
End Sub
and here's my form:
-
Nov 2nd, 2005, 03:22 AM
#5
Addicted Member
Re: [ask] change background in mdi parent form(runtime) ?
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.BackgroundImage = System.Drawing.Image.FromFile("C:\IEHJFILE\dloads\phone\xpr.jpg")
-
Nov 2nd, 2005, 03:29 AM
#6
Re: [ask] change background in mdi parent form(runtime) ?
 Originally Posted by iehjsucker
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.BackgroundImage = System.Drawing.Image.FromFile("C:\IEHJFILE\dloads\phone\xpr.jpg")
Are we paying attention?
 Originally Posted by jmcilhinney
You cannot actually see the background of an MDI parent window. When you make a window an MDI container, an MdiClient control is placed on the form and all the child windows are contained within that.
Setting the BackgroundImage of the form itself makes no difference because it is completely covered by the MdiClient control.
-
Nov 2nd, 2005, 03:42 AM
#7
Addicted Member
Re: [ask] change background in mdi parent form(runtime) ?
he only wants to change the background...
-
Nov 2nd, 2005, 03:47 AM
#8
Re: [ask] change background in mdi parent form(runtime) ?
He wants to change the background of an MDI parent form. The entire surface of an MDI parent form is covered with an MdiClient control. If you change the BackgroundImage of the form you will not see it because it will be completely covered by the nice grey MdiClient control. For that reason you need to set the BackgroundImage of the MdiClient control, as my code does, as proven by my pretty screengrab.
-
Nov 2nd, 2005, 03:49 AM
#9
Addicted Member
Re: [ask] change background in mdi parent form(runtime) ?
so is to my code. that will do also the trick
-
Nov 6th, 2005, 09:48 PM
#10
Thread Starter
Member
Re: [ask] change background in mdi parent form(runtime) ?
thanks jmchilhinney, it worked , but how to make the picture in the background to be stretched ?, I've tried the iehjsucker's code but it didnt work
-
Nov 6th, 2005, 10:28 PM
#11
Re: [ask] change background in mdi parent form(runtime) ?
 Originally Posted by iehjsucker
so is to my code. that will do also the trick
BackgroundImageLayout is new in .NET 2.0. Perhaps something has changed and your code will work in 2.0. I have grave reservations about that but I can't test it at the moment as I don't have VS 2005 on this machine. I can tell you for a fact that it won't work in .NET 1.x.
paul_heru, in 2.0 you would use the BackgroundImageLayout property to stretch
the image, as iehjsucker suggests, but in 1.x I'm not aware of a simple way, although others might be.
-
Nov 7th, 2005, 01:18 AM
#12
Thread Starter
Member
Re: [ask] change background in mdi parent form(runtime) ?
so, I 've to use .net 2.0 ?, or with vs2005 also ?
-
Nov 7th, 2005, 03:13 AM
#13
Re: [ask] change background in mdi parent form(runtime) ?
Unless someone else knows a way to do what you want without using the BackgroundImageLayout property then yes. Note that if your form can be resized to whatever dimensions the user wants, a stretched image is likely to look bad in some circumstances. If your form can't be resized then you just need to make your image the correct size in the first place.
-
Nov 7th, 2005, 03:18 AM
#14
Thread Starter
Member
Re: [ask] change background in mdi parent form(runtime) ?
ok then, thanks jmcilhinney, does anyone know the way , so I have not to change to VS 2005, if does, please help me , thanks
-
Nov 7th, 2005, 09:12 AM
#15
Hyperactive Member
Re: [ask] change background in mdi parent form(runtime) ?
Yes, you can use GDI+ to draw the image on the MDIClient surface:
VB Code:
Dim m_Client As MdiClient
Dim m_Image As Image
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Find the MDIClient control
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
m_Client = ctl
Exit For
End If
Next ctl
'Load the required background image
m_Image = Image.FromFile("\image.bmp")
'Apply the background on loading
m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Private Sub Form1_Invalidated(ByVal sender As Object, ByVal e As System.Windows.Forms.InvalidateEventArgs) Handles MyBase.Invalidated
If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Try resizing the form and you should find the image changes size with it. Note that you'll have to find a way to stop the child forms invalidating the background surface; the best way is probably to trap their resize/move events and repaint the surface then.
"Make it idiot-proof and someone will make a better idiot"
-
Nov 7th, 2005, 08:59 PM
#16
Thread Starter
Member
Re: [ask] change background in mdi parent form(runtime) ?
 Originally Posted by Parallax
Yes, you can use GDI+ to draw the image on the MDIClient surface:
VB Code:
Dim m_Client As MdiClient
Dim m_Image As Image
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Find the MDIClient control
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
m_Client = ctl
Exit For
End If
Next ctl
'Load the required background image
m_Image = Image.FromFile("\image.bmp")
'Apply the background on loading
m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Private Sub Form1_Invalidated(ByVal sender As Object, ByVal e As System.Windows.Forms.InvalidateEventArgs) Handles MyBase.Invalidated
If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
End Sub
Try resizing the form and you should find the image changes size with it. Note that you'll have to find a way to stop the child forms invalidating the background surface; the best way is probably to trap their resize/move events and repaint the surface then. 
yes, i've tried the code , but it didn't work, the image did not show up at the beginning, but if I resize the parent window, the image will show up, but if I tried to expand the size of the form too fast the image will not perfectly stretch to the main form
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|