Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi, I have been searching online for some sample codes on how to correctly make Windows Application bottom most. After digging and some trials and errors, it can't seem to find the right code for what I want. I wonder if it is possible or not.

I want to put the windows application right on top of the desktop, but under all other applications and taskbar.

Here is what I found so far:
C#
private IntPtr m_DesktopClass;
private IntPtr m_DesktopFileListView;
private IntPtr m_PreviousParent;

[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpClassName, String lpWindowName);

public bool DesktopAttached
{
get { return m_DesktopAttached; }
set
{
      m_DesktopAttached = value;
      this.MinimizeBox = !value;
      if (value)
      {
            m_DesktopClass = FindWindowEx(FindWindow("Progman", null), new IntPtr(0), "shelldll_defview", null);
                    
            m_DesktopFileListView = FindWindowEx(m_DesktopClass, new IntPtr(0), "syslistview32", null);

            m_PreviousParent = SetParent(this.Handle, m_DesktopFileListView);
      }
      else
      {
            SetParent(this.Handle, m_PreviousParent);
      }
}


The above code does put the windows application right on top of the desktop and under all other applications and taskbar; however, all buttons on the application don't work, not what I want.

C#
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
   int Y, int cx, int cy, uint uFlags);

const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOACTIVATE = 0x0010;

static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

public static void SetBottom()
{
    SetWindowPos(this.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
}


The above code does put the windows application right on top of the desktop and under all other applications and all buttons on the application work; however, the application is on top of the taskbar, not what I want.
Posted

1 solution

Thanks for your help, Steven Pinto, but your code doesn't allow the application to fall behind the taskbar. After playing with the codes again, I found that the second set of the code works. It didn't work before, because there was a line to the code which interfere with SetBottom method as shown below:

C#
public Form1()
{
       InitializeComponent();
       Bounds = Screen.PrimaryScreen.Bounds; //Interfere with SetBottom()
       SetBottom();
}


In order to correctly cause the application to be full screen and remain on the bottom of the z-order, I have to use WorkingArea instead of Bounds and override WndProc function and call SetWindowPos for WM_WINDOWPOSCHANGING Windows message. The complete code is shown below:

C#
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOACTIVATE = 0x0010;
const int WM_WINDOWPOSCHANGING = 0x0046;

protected override void WndProc(ref Message m)
{
       // Listen for operating system messages.
       switch (m.Msg)
       {
              // The WM_WINDOWPOSCHANGING message occurs when a window whose
              // size, position, or place in the Z order is about to change.
              case WM_WINDOWPOSCHANGING:
                  SetWindowPos(this.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
                  break;
       }
       base.WndProc(ref m);
}

public Form1()
{
       InitializeComponent();
       Bounds = Screen.PrimaryScreen.WorkingArea;
       CenterToParent();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900