Results 1 to 16 of 16

Thread: [ask] change background in mdi parent form(runtime) ?

  1. #1

    Thread Starter
    Member paul_heru's Avatar
    Join Date
    Nov 2005
    Posts
    32

    [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. For Each ctl As Control In Me.Controls
    2.     If TypeOf ctl Is MdiClient Then
    3.         ctl.BackgroundImage = myImage
    4.         Exit For
    5.     End If
    6. Next ctl
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member paul_heru's Avatar
    Join Date
    Nov 2005
    Posts
    32

    Re: [ask] change background in mdi parent form(runtime) ?

    Quote 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:
    1. For Each ctl As Control In Me.Controls
    2.     If TypeOf ctl Is MdiClient Then
    3.         ctl.BackgroundImage = myImage
    4.         Exit For
    5.     End If
    6. Next ctl
    Im sorry, but it did not work, as I placed that code in the FrmLoad event

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [ask] change background in mdi parent form(runtime) ?

    Well then you've done something wrong because here's my code:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         For Each ctl As Control In Me.Controls
    3.             If TypeOf ctl Is MdiClient Then
    4.                 ctl.BackgroundImage = Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg")
    5.                 Exit For
    6.             End If
    7.         Next
    8.  
    9.         Dim child As New Form
    10.  
    11.         child.Text = "Child"
    12.         child.Size = New Size(150, 150)
    13.         child.MdiParent = Me
    14.         child.Show()
    15.     End Sub
    and here's my form:
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    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")

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [ask] change background in mdi parent form(runtime) ?

    Quote Originally Posted by iehjsucker
    Me.BackgroundImageLayout = ImageLayout.Stretch
    Me.BackgroundImage = System.Drawing.Image.FromFile("C:\IEHJFILE\dloads\phone\xpr.jpg")
    Are we paying attention?
    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    Re: [ask] change background in mdi parent form(runtime) ?

    he only wants to change the background...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    Re: [ask] change background in mdi parent form(runtime) ?

    so is to my code. that will do also the trick

  10. #10

    Thread Starter
    Member paul_heru's Avatar
    Join Date
    Nov 2005
    Posts
    32

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [ask] change background in mdi parent form(runtime) ?

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member paul_heru's Avatar
    Join Date
    Nov 2005
    Posts
    32

    Re: [ask] change background in mdi parent form(runtime) ?

    so, I 've to use .net 2.0 ?, or with vs2005 also ?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Member paul_heru's Avatar
    Join Date
    Nov 2005
    Posts
    32

    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

  15. #15
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Re: [ask] change background in mdi parent form(runtime) ?

    Yes, you can use GDI+ to draw the image on the MDIClient surface:
    VB Code:
    1. Dim m_Client As MdiClient
    2.     Dim m_Image As Image
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         'Find the MDIClient control
    6.         For Each ctl As Control In Me.Controls
    7.             If TypeOf ctl Is MdiClient Then
    8.                 m_Client = ctl
    9.                 Exit For
    10.             End If
    11.         Next ctl
    12.  
    13.         'Load the required background image
    14.         m_Image = Image.FromFile("\image.bmp")
    15.  
    16.         'Apply the background on loading
    17.         m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    18.     End Sub
    19.  
    20.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    21.         If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    22.     End Sub
    23.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    24.         If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    25.     End Sub
    26.     Private Sub Form1_Invalidated(ByVal sender As Object, ByVal e As System.Windows.Forms.InvalidateEventArgs) Handles MyBase.Invalidated
    27.         If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    28.     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"

  16. #16

    Thread Starter
    Member paul_heru's Avatar
    Join Date
    Nov 2005
    Posts
    32

    Re: [ask] change background in mdi parent form(runtime) ?

    Quote Originally Posted by Parallax
    Yes, you can use GDI+ to draw the image on the MDIClient surface:
    VB Code:
    1. Dim m_Client As MdiClient
    2.     Dim m_Image As Image
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         'Find the MDIClient control
    6.         For Each ctl As Control In Me.Controls
    7.             If TypeOf ctl Is MdiClient Then
    8.                 m_Client = ctl
    9.                 Exit For
    10.             End If
    11.         Next ctl
    12.  
    13.         'Load the required background image
    14.         m_Image = Image.FromFile("\image.bmp")
    15.  
    16.         'Apply the background on loading
    17.         m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    18.     End Sub
    19.  
    20.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    21.         If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    22.     End Sub
    23.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    24.         If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    25.     End Sub
    26.     Private Sub Form1_Invalidated(ByVal sender As Object, ByVal e As System.Windows.Forms.InvalidateEventArgs) Handles MyBase.Invalidated
    27.         If Not m_Client Is Nothing Then m_Client.CreateGraphics.DrawImage(m_Image, 0, 0, m_Client.Width, m_Client.Height)
    28.     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
  •  



Click Here to Expand Forum to Full Width