Search code examples
c#winformsz-order

How can I bring a second (child) form to the front of the windows z-order along with main (parent) form


As the title suggests I have a situation where I want to bring a secondary form (FormB which does not have a taskbar icon) to the front (z-order) when my main form (FormA) is activated (has focus).

For example, I have my program running with two forms (FormA and FormB). I then open up notepad maximized (just to cover both of the forms [FormA and FormB] that my application contains on the screen). Then I click the Windows Taskbar item for my application (which is FormA) at the bottom of the screen to open my application back up. When that happens FormB is not shown and is left in the background of the z-order. I would like for both forms be brought to the front of all other forms.

In the end when restoring FormA like any normal application, FormB should be the same Z-ORDER minus ONE.

Current Source Example

public partial class FormA : Form
{
  public FormA()
  {
    Log FormB = new FormB();
    FormB.Show();

    Log FormB = new FormB();
    FormB.ShowDialog();
  }

  private void FormA_Activated(object sender, EventArgs e)
  {
    if (FormB.Visible)
    {
      FormB.Show();
    }
  }
}

Solution

  • I ended up finding out how to do this with a little trial and error. In hopes this helps someone else out in the future I wanted to post my findings here.

    Because this isn't directly supported by the .NET Form class you need to use the the SetWindowPos API. Four things need to be done to make this happen. All of the source below can be added to your FORM's source (FormA).

    As shown below the Usage (#4) is showing an example that I had using tool strip menu items (File, Edit, etc) at the top of my main form (FormA). Then the second form which is my log form (FormB) is considered the secondary form. This can be done with multiple forms (more than 2 like my example shows).

    Note: All of my tool strip menu items have CheckOnClick set to TRUE. This way each time you select the menu items they are being turned on and off.

    1. Declare The Enums

      #region Enums
      public enum SpecialWindowHandles
      {
          /// <summary>
          ///     Places the window at the top of the Z order.
          /// </summary>
          HWND_TOP = 0,
      
          /// <summary>
          ///     Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
          /// </summary>
          HWND_BOTTOM = 1,
      
          /// <summary>
          ///     Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
          /// </summary>
          HWND_TOPMOST = -1,
      
          /// <summary>
          ///     Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
          /// </summary>
          HWND_NOTOPMOST = -2
      }
      
      [Flags]
      public enum SetWindowPosFlags : uint
      {
          /// <summary>
          ///     If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
          /// </summary>
          SWP_ASYNCWINDOWPOS = 0x4000,
      
          /// <summary>
          ///     Prevents generation of the WM_SYNCPAINT message.
          /// </summary>
          SWP_DEFERERASE = 0x2000,
      
          /// <summary>
          ///     Draws a frame (defined in the window's class description) around the window.
          /// </summary>
          SWP_DRAWFRAME = 0x0020,
      
          /// <summary>
          ///     Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
          /// </summary>
          SWP_FRAMECHANGED = 0x0020,
      
          /// <summary>
          ///     Hides the window.
          /// </summary>
          SWP_HIDEWINDOW = 0x0080,
      
          /// <summary>
          ///     Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
          /// </summary>
          SWP_NOACTIVATE = 0x0010,
      
          /// <summary>
          ///     Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
          /// </summary>
          SWP_NOCOPYBITS = 0x0100,
      
          /// <summary>
          ///     Retains the current position (ignores X and Y parameters).
          /// </summary>
          SWP_NOMOVE = 0x0002,
      
          /// <summary>
          ///     Does not change the owner window's position in the Z order.
          /// </summary>
          SWP_NOOWNERZORDER = 0x0200,
      
          /// <summary>
          ///     Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
          /// </summary>
          SWP_NOREDRAW = 0x0008,
      
          /// <summary>
          ///     Same as the SWP_NOOWNERZORDER flag.
          /// </summary>
          SWP_NOREPOSITION = 0x0200,
      
          /// <summary>
          ///     Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
          /// </summary>
          SWP_NOSENDCHANGING = 0x0400,
      
          /// <summary>
          ///     Retains the current size (ignores the cx and cy parameters).
          /// </summary>
          SWP_NOSIZE = 0x0001,
      
          /// <summary>
          ///     Retains the current Z order (ignores the hWndInsertAfter parameter).
          /// </summary>
          SWP_NOZORDER = 0x0004,
      
          /// <summary>
          ///     Displays the window.
          /// </summary>
          SWP_SHOWWINDOW = 0x0040,
      }
      #endregion
      
    2. Declare The APIs

      #region APIs
      [DllImport("user32.dll", SetLastError = true)]
      private static extern bool SetWindowPos(IntPtr hWnd, SpecialWindowHandles hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
      #endregion
      
    3. Create The Functions

      #region Functions
      private void BringVisibleWindowsToFront()
      {
          //Get parent form handle
          IntPtr hndParentWindow = this.Handle;
      
          //Bring all childern forms to the front
          foreach (Form frmChild in Application.OpenForms)
          {
              //If form is not this form
              if (frmChild.Handle != this.Handle)
              {
                  //If form is visible and not minimized
                  if (frmChild.WindowState != FormWindowState.Minimized && frmChild.Visible == true)
                      SetWindowPos(frmChild.Handle, SpecialWindowHandles.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
              }
          }
      
          //Set the parent form to the top most z order
          SetWindowPos(hndParentWindow, SpecialWindowHandles.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
      }
      
      private void SetAllFormWindowsAlwaysOnTop(bool active)
      {
          SpecialWindowHandles OnTop = (active ? SpecialWindowHandles.HWND_TOPMOST : SpecialWindowHandles.HWND_NOTOPMOST);
      
          //Get parent form handle
          IntPtr hndParentWindow = this.Handle;
      
          //Bring all childern forms to the front
          foreach (Form frmChild in Application.OpenForms)
          {
              //If form is not this form
              if (frmChild.Handle != this.Handle)
              {
                  //If form is visible and not minimized
                  if (frmChild.WindowState != FormWindowState.Minimized && frmChild.Visible == true)
                      SetWindowPos(frmChild.Handle, OnTop, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
              }
          }
      
          //Set the parent form to the top most z order
          SetWindowPos(hndParentWindow, OnTop, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
      }
      #endregion
      
    4. Usage (tool strip menu items example)

      #region ToolStripMenuItems events
      //Shows or hides the log form (FormB)
      private void showLogToolStripMenuItem_Click(object sender, EventArgs e)
      {
          if (showLogToolStripMenuItem.Checked)
          {
              FormB.ShowLog(true);
              SetAllFormWindowsAlwaysOnTop(alwaysOnTopToolStripMenuItem.Checked);
          }
          else
              FormB.ShowLog(false);
      }
      
      //Sets all the forms (in this case FormA and FormB) to always be on top or not
      private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
      {
          SetAllFormWindowsAlwaysOnTop(alwaysOnTopToolStripMenuItem.Checked);
      }
      #endregion