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
Printable View
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
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 eventQuote:
Originally Posted by jmcilhinney
Well then you've done something wrong because here's my code:and here's my form: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
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.BackgroundImage = System.Drawing.Image.FromFile("C:\IEHJFILE\dloads\phone\xpr.jpg")
Are we paying attention?Quote:
Originally Posted by iehjsucker
Setting the BackgroundImage of the form itself makes no difference because it is completely covered by the MdiClient control.Quote:
Originally Posted by jmcilhinney
he only wants to change the background...
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.
so is to my code. that will do also the trick
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
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.Quote:
Originally Posted by iehjsucker
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.
so, I 've to use .net 2.0 ?, or with vs2005 also ?
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.
ok then, thanks jmcilhinney, does anyone know the way , so I have not to change to VS 2005, if does, please help me , thanks
Yes, you can use GDI+ to draw the image on the MDIClient surface:
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. :thumb: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
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 formQuote:
Originally Posted by Parallax