Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How set the background of an MDI container window?

How set the background of an MDI container window?

Scheduled Pinned Locked Moved C#
dockertutorialquestion
35 Posts 10 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jojoba20

    The area of an MDI container window is covered by an MdiClient control; consequently, Form.BackColor and Form.BackgroundImage are ineffective. So How to set the background for this form.

    P Offline
    P Offline
    Philippe Mori
    wrote on last edited by
    #2

    I would think you need to do P/Invoke for that...

    Philippe Mori

    1 Reply Last reply
    0
    • J jojoba20

      The area of an MDI container window is covered by an MdiClient control; consequently, Form.BackColor and Form.BackgroundImage are ineffective. So How to set the background for this form.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #3

      You could, but it's worthess and will slow down your applications rendering speed. Again, you can't do it as design time. You have to do it at runtime. Again, you have to enumerate through the MdiParent Controls collection. Once you find the MdiClient control, you can set its BackgroundImage property to whatever you want.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      B 1 Reply Last reply
      0
      • J jojoba20

        The area of an MDI container window is covered by an MdiClient control; consequently, Form.BackColor and Form.BackgroundImage are ineffective. So How to set the background for this form.

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #4

        If your question means you want a solution where there is one MDI container Form with a background image, and all the Forms on it ... interpreting "client" to meaning an 'mdi child form:' someForm.MdiParent = MDIContainerForm ... show a background as if they were 'transparent,' and you were seeing the portion of the MDIContainerForm's background their current bounding-box covers ... You are in for some deep-plumbing, and as others comment here, it ain't worth it. Now if the MDI container form just has a background that's a texture, then it's easy, but I'm pretty sure you are asking about the container form having a background picture, not a texture. However, if you have the option of not using the MDI architecture, there are some ways you can 'simulate' the effect of contained Forms having the same background image. You make them transparent, and you "do the right thing" so that at run-time they remain in the boundary of the intended container control. If you wish to hear about the specifics of this alternative, just ask. best, Bill

        "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

        1 Reply Last reply
        0
        • J jojoba20

          The area of an MDI container window is covered by an MdiClient control; consequently, Form.BackColor and Form.BackgroundImage are ineffective. So How to set the background for this form.

          A Offline
          A Offline
          Alisaunder
          wrote on last edited by
          #5

          You can set the background by casting the type to MDIClient and setting the MDIClient background. EXAMPLE:

              private void Form1\_Load(object sender, EventArgs e)
              {
                  MdiClient ctlMDI;
                  //' Loop through all of the form's controls looking
                  //' for the control of type MdiClient.
                  foreach (Control ctl in this.Controls)
                  {                
                      try
                      {
                          // Attempt to cast the control to type MdiClient.
                          ctlMDI = (MdiClient)ctl;
                          
                          // Set the BackColor of the MdiClient control.
                          ctlMDI.BackColor = Color.White;
                      }
                      catch (InvalidCastException exc)
                      {
                          // Catch and ignore the error if casting failed.
                      }
                  }
              }
          
          L D B 3 Replies Last reply
          0
          • A Alisaunder

            You can set the background by casting the type to MDIClient and setting the MDIClient background. EXAMPLE:

                private void Form1\_Load(object sender, EventArgs e)
                {
                    MdiClient ctlMDI;
                    //' Loop through all of the form's controls looking
                    //' for the control of type MdiClient.
                    foreach (Control ctl in this.Controls)
                    {                
                        try
                        {
                            // Attempt to cast the control to type MdiClient.
                            ctlMDI = (MdiClient)ctl;
                            
                            // Set the BackColor of the MdiClient control.
                            ctlMDI.BackColor = Color.White;
                        }
                        catch (InvalidCastException exc)
                        {
                            // Catch and ignore the error if casting failed.
                        }
                    }
                }
            
            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            Alisaunder wrote:

            try { // Attempt to cast the control to type MdiClient. ctlMDI = (MdiClient)ctl;

            That is so ugly. I suggest you read up on the is and as keywords. :doh:

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • A Alisaunder

              You can set the background by casting the type to MDIClient and setting the MDIClient background. EXAMPLE:

                  private void Form1\_Load(object sender, EventArgs e)
                  {
                      MdiClient ctlMDI;
                      //' Loop through all of the form's controls looking
                      //' for the control of type MdiClient.
                      foreach (Control ctl in this.Controls)
                      {                
                          try
                          {
                              // Attempt to cast the control to type MdiClient.
                              ctlMDI = (MdiClient)ctl;
                              
                              // Set the BackColor of the MdiClient control.
                              ctlMDI.BackColor = Color.White;
                          }
                          catch (InvalidCastException exc)
                          {
                              // Catch and ignore the error if casting failed.
                          }
                      }
                  }
              
              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #7

              Not to mention using a try/catch to find the correct control is slow as exceptions are expensive objects to create. Exceptions should be used to handle exceptional cases, not used in main logic. A much better and faster implementation would have been to check the type of the control first, then cast it to an MdiClient if appropriate.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              A 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Not to mention using a try/catch to find the correct control is slow as exceptions are expensive objects to create. Exceptions should be used to handle exceptional cases, not used in main logic. A much better and faster implementation would have been to check the type of the control first, then cast it to an MdiClient if appropriate.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                A Offline
                A Offline
                Alisaunder
                wrote on last edited by
                #8

                That's funny considering this comes from Microsoft's own help and support on the subject. http://support.microsoft.com/kb/319417[^]

                D 1 Reply Last reply
                0
                • A Alisaunder

                  That's funny considering this comes from Microsoft's own help and support on the subject. http://support.microsoft.com/kb/319417[^]

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #9

                  I don't care. Those examples are not there to demo best practices. Those examples are not considered "production-level" code.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  A 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    I don't care. Those examples are not there to demo best practices. Those examples are not considered "production-level" code.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    A Offline
                    A Offline
                    Alisaunder
                    wrote on last edited by
                    #10

                    I'm so glad your opinion isn't all that matters then.

                    D 1 Reply Last reply
                    0
                    • A Alisaunder

                      I'm so glad your opinion isn't all that matters then.

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #11

                      I'm not the only one telling you the practice sucks...

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      A 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        I'm not the only one telling you the practice sucks...

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        A Offline
                        A Offline
                        Alisaunder
                        wrote on last edited by
                        #12

                        And yet neither of you are displaying an alternative that is better?

                        L D B 3 Replies Last reply
                        0
                        • A Alisaunder

                          You can set the background by casting the type to MDIClient and setting the MDIClient background. EXAMPLE:

                              private void Form1\_Load(object sender, EventArgs e)
                              {
                                  MdiClient ctlMDI;
                                  //' Loop through all of the form's controls looking
                                  //' for the control of type MdiClient.
                                  foreach (Control ctl in this.Controls)
                                  {                
                                      try
                                      {
                                          // Attempt to cast the control to type MdiClient.
                                          ctlMDI = (MdiClient)ctl;
                                          
                                          // Set the BackColor of the MdiClient control.
                                          ctlMDI.BackColor = Color.White;
                                      }
                                      catch (InvalidCastException exc)
                                      {
                                          // Catch and ignore the error if casting failed.
                                      }
                                  }
                              }
                          
                          B Offline
                          B Offline
                          BillWoodruff
                          wrote on last edited by
                          #13

                          Hi, I don't think you deserve to be cast into the outer darkness of the dreaded one-vote here, because you were, I think, sincerely trying to respond to the OP's question. And, whether the code you provided leads to gnashing of teeth, or not, it does work. But, may I suggest, in the future, you provide a link to the MS docs, or other sources, code examples are taken from. best, Bill

                          "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

                          1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            You could, but it's worthess and will slow down your applications rendering speed. Again, you can't do it as design time. You have to do it at runtime. Again, you have to enumerate through the MdiParent Controls collection. Once you find the MdiClient control, you can set its BackgroundImage property to whatever you want.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            B Offline
                            B Offline
                            BillWoodruff
                            wrote on last edited by
                            #14

                            +5 Right on target, Dave, and you reminded me of when ... several years ago ... I did some stuff with MDI (yuck), and I went back today, and took a look at the MdiClient component that is created when you set a Form's IsMdiContainer Property = true. The WinForms designers, probably having a bad hair day, set the Text and Name properties of MdiClient to an empty string, which means you can't do something like this to find it:

                            //
                            private MdiClient theMDIClientControl;
                            //
                            // note: don't need to recurse
                            Control[] potentialMDIClientControls = this.Controls.Find("MdiClient", false);
                            //
                            if (potentialMDIClientControls.Length > 0) theMDIClientControl = potentialMDIClientControls[0] as MdiClient;

                            So, as you said, you gotta iterate/enumerate over the Controls collection of the Form.

                            "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

                            D 1 Reply Last reply
                            0
                            • A Alisaunder

                              And yet neither of you are displaying an alternative that is better?

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #15

                              I'm not in the spoon feeding business, I did provide the two keywords that exist for dealing elegantly with such situations. So if you want to learn something, look them up and read the reference material. :|

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              A 1 Reply Last reply
                              0
                              • A Alisaunder

                                And yet neither of you are displaying an alternative that is better?

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #16

                                Apparently you haven't been reading the posts. You also apparently won't be happy until you see actual code:

                                private void MdiClientExample()
                                {
                                foreach (Control c in this.Controls)
                                {
                                if (c is MdiClient)
                                {
                                MdiClient mc = (MdiClient)c;
                                ...
                                }
                                }
                                }

                                Are you happy now? Oh, and by the way, I don't consider this production quality code either. It's just cleaner than the example Microsoft gave. And if you're going to critisize us, you might want to start by creating your own code samples instead of lifting and posting others as your own work.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak

                                A B 2 Replies Last reply
                                0
                                • L Luc Pattyn

                                  I'm not in the spoon feeding business, I did provide the two keywords that exist for dealing elegantly with such situations. So if you want to learn something, look them up and read the reference material. :|

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  A Offline
                                  A Offline
                                  Alisaunder
                                  wrote on last edited by
                                  #17

                                  So sorry you don't feel like trying to answer the ops question with something less cryptic. Seems forums just aren't as helpful as they used to be. Since you feel you are such an expert that you you don't need to provide examples my opinion, for what it's worth is that you are no help at all and shouldn't even be posting.

                                  D 1 Reply Last reply
                                  0
                                  • A Alisaunder

                                    So sorry you don't feel like trying to answer the ops question with something less cryptic. Seems forums just aren't as helpful as they used to be. Since you feel you are such an expert that you you don't need to provide examples my opinion, for what it's worth is that you are no help at all and shouldn't even be posting.

                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #18

                                    Yeah, right. A couple of Code Project MVP's aren't very helpful at all. The pile of 5-voted responses to questions just doesn't offer up any evidence at all of us being helpful. I'm not in the spoon-feed business either. There are just WAY too many very basic concept questions being asked that are very easily answered simply by typing the question into Google. I'd rather have someone learn how to do research themselves than just keep asking question after question about very basic topics.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak

                                    A 1 Reply Last reply
                                    0
                                    • D Dave Kreskowiak

                                      Yeah, right. A couple of Code Project MVP's aren't very helpful at all. The pile of 5-voted responses to questions just doesn't offer up any evidence at all of us being helpful. I'm not in the spoon-feed business either. There are just WAY too many very basic concept questions being asked that are very easily answered simply by typing the question into Google. I'd rather have someone learn how to do research themselves than just keep asking question after question about very basic topics.

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak

                                      A Offline
                                      A Offline
                                      Alisaunder
                                      wrote on last edited by
                                      #19

                                      Actually Most everyone I know searches for the answers before hitting a forum with a question. Personally in my opinion it's more professional to offer assistance with a basic code example in the hopes the op learns something in the process. All anyone is learning from your responses is not to ask you for help.

                                      P D 2 Replies Last reply
                                      0
                                      • B BillWoodruff

                                        +5 Right on target, Dave, and you reminded me of when ... several years ago ... I did some stuff with MDI (yuck), and I went back today, and took a look at the MdiClient component that is created when you set a Form's IsMdiContainer Property = true. The WinForms designers, probably having a bad hair day, set the Text and Name properties of MdiClient to an empty string, which means you can't do something like this to find it:

                                        //
                                        private MdiClient theMDIClientControl;
                                        //
                                        // note: don't need to recurse
                                        Control[] potentialMDIClientControls = this.Controls.Find("MdiClient", false);
                                        //
                                        if (potentialMDIClientControls.Length > 0) theMDIClientControl = potentialMDIClientControls[0] as MdiClient;

                                        So, as you said, you gotta iterate/enumerate over the Controls collection of the Form.

                                        "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

                                        D Offline
                                        D Offline
                                        Dave Kreskowiak
                                        wrote on last edited by
                                        #20

                                        Very true. I found the lack of a name a little annoying at first, but I now prefer to find it by type. You really can't change the type at all, but the name can be changed breaking existing code.

                                        A guide to posting questions on CodeProject[^]
                                        Dave Kreskowiak

                                        1 Reply Last reply
                                        0
                                        • A Alisaunder

                                          And yet neither of you are displaying an alternative that is better?

                                          B Offline
                                          B Offline
                                          BillWoodruff
                                          wrote on last edited by
                                          #21

                                          Alisaunder wrote:

                                          And yet neither of you are displaying an alternative that is better?

                                          I think the absence of code here is not the result of any 'negative intention:' It's just obvious that the best practice here is to enumerate the controls on the Form [1], test each one using the 'Is operator, and, when the MdiClient Control is found, then cast it from Type Control back to its 'native Type, 'MdiClient ... at which point you can have your way with it.

                                          //
                                          // assume you have loaded a valid image from an embedded resource
                                          // into the variable of Type Bitmap named 'mdiBackGround'
                                          //
                                          private MdiClient theMDIClientControl;
                                          //
                                          foreach (Control theControl in this.Controls) [2]
                                          {
                                          if (theControl is MdiClient)
                                          {
                                          theMDIClientControl = theControl as MdiClient;
                                          break;
                                          }
                                          }

                                          if (theMDIClientControl != null) theMDIClientControl.BackgroundImage = mdiBackGround;

                                          [1] see my response to Dave K. above[^] confirming why it is absolutely necessary to enumerate the Controls on the Form. [2] Seems a reasonable assumption the MdiClient Control will always be in the top-level Form Control Collection: hence no need for a recursive search

                                          "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

                                          A 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups