Controls in container form come over child form?

后端 未结 8 2178
天涯浪人
天涯浪人 2020-12-01 20:40

In a container form I have menu and buttons to open ther forms. \"enter

Here I am fac

相关标签:
8条回答
  • 2020-12-01 20:46

    It appears as though that form is a sibling of those other child controls. Do you have to open it as a child of that window? Can't it be like a non-modal dialog box and not a child window of that main form?

    If it has to be within that main form and a sibling of those controls, then you're going to have to set the Z-Order of it. There's no property for that, so you're going to have to look toward the Win32 API call, SetWindowPos:

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern bool SetWindowPos(
    int hWnd, // window handle
    int hWndInsertAfter, // placement-order handle
    int X, // horizontal position
    int Y, // vertical position
    int cx, // width
    int cy, // height
    uint uFlags); // window positioning flags
    
    
    const uint SWP_NOSIZE = 0x1;
    const uint SWP_NOMOVE = 0x2;
    const uint SWP_SHOWWINDOW = 0x40;
    const uint SWP_NOACTIVATE = 0x10;
    

    And call it something like this:

    SetWindowPos((int)form.Handle,   // that form
                 (int)insertAfter.Handle,  // some other control
                 0, 0, 0, 0,
                 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
    
    0 讨论(0)
  • 2020-12-01 20:49

    @Hans Passant has the correct answer, but you could also solve your issue by not using MDI forms at all. Some options:

    • Use separate forms: have a menu form, typically large/maximized, and launch child forms in front of it by setting their Parent property to the menu form, or
    • Use a single form, but with a docking library (I've used DockPanel Suite in the past). This is essentially a re-implementation of MDI forms, with extra features. This is a bit of work to get running, but it can let you build some nice UIs.

    Both of these would require significant changes to your UI code, though.

    0 讨论(0)
  • 2020-12-01 20:50

    I've also got the same problem. I got an alternative solution as described below:

    1. Insert a timer control
    2. I've added the controls in a panel container
    3. And did the following

      private void timer1_Tick(object sender, EventArgs e)
      {
          if ((int)MdiChildren.GetLength(0) > 0)
          {
              panel1.Visible = false;
          }
          else
          {
              panel1.Visible = true;
          }
      }
      
    0 讨论(0)
  • 2020-12-01 20:52

    If it is a MDI application and you put controls in the parent window then they will be shown on top of any created child windows. You need to have your menu in a child window also, not on the parent form.

    Look at this Article and this.

    expecially this:

    The parent Form may not contain any controls. >

    Edit: Added Additional Information

    0 讨论(0)
  • 2020-12-01 20:55

    Call BringToFront() on each child form after showing them. Alternately, hook it to each child form's OnLoad method:

    childForm.OnLoad += (s, e) => (s as Form).BringToFront();
    
    0 讨论(0)
  • 2020-12-01 20:56

    The main trick here is to treat child forms as Controls. You'll create a child form just like any other control. When you use this method, you have to set it's TopLevel to false - otherwise it won't work.

    The following lines of code are used to create a child form:

    Form childForm = new Form(); //initialize a child form
    
    childForm.TopLevel = false; //set it's TopLevel to false
    
    Controls.Add(childForm); //and add it to the parent Form
    childForm.Show(); //finally display it
    
    childForm.BringToFront(); //use this it there are Controls over your form.
    

    more details here

    0 讨论(0)
提交回复
热议问题