SoFunction
Updated on 2025-03-07

How to display the top or bottom layer of the desktop by c# winform window.

one,

At the front:
using ;
Introduce the following two functions in the definition section:
[DllImport( "user32 ")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport( "user32 ")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Add (Santos' code):
IntPtr hDeskTop=FindWindow( "Progman ", "Program Manager ");
SetParent(,hDeskTop);
Another way to modify desktop wallpaper implementation
After testing, win2000-----------------------------------------------------------------------------------------------------------------------

two,

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
namespace ShowInDesk 
{ 
publicpartialclass Form1 : Form 
{ 
IntPtr hDesktop; 
publicconstint GW_CHILD =5; 
public Form1() 
{ 
InitializeComponent(); 
 = GetDesktopHandle(); 
EmbedDesktop(this, , ); 
isMouseDown =false; 
} 
public IntPtr GetDesktopHandle(DesktopLayer layer) { //hWnd = new HandleRef(); 
HandleRef hWnd; 
IntPtr hDesktop =new IntPtr(); 
switch (layer) 
{ 
case : 
hDesktop = ("Progman", null);//The first layer of desktopbreak; 
case : 
hDesktop = ("Progman", null);//The first layer of desktophWnd =new HandleRef(this, hDesktop); 
hDesktop = (hWnd, GW_CHILD);//Level 2 desktopbreak; 
case : 
hDesktop = ("Progman", null);//The first layer of desktophWnd =new HandleRef(this, hDesktop); 
hDesktop = (hWnd, GW_CHILD);//Level 2 desktophWnd =new HandleRef(this, hDesktop); 
hDesktop = (hWnd, GW_CHILD);//Level 3 desktopbreak; 
} 
return hDesktop; 
} 
publicvoid EmbedDesktop(Object embeddedWindow, IntPtr childWindow, IntPtr parentWindow) 
{ 
Form window = (Form)embeddedWindow; 
HandleRef HWND_BOTTOM =new HandleRef(embeddedWindow, new IntPtr(1)); 
constint SWP_FRAMECHANGED =0x0020;//Send window size change message(childWindow, parentWindow); 
(new HandleRef(window, childWindow), HWND_BOTTOM, 300, 300, , , SWP_FRAMECHANGED); 
} 
} 
} 

2、

using System; 
using ; 
using ; 
using ; 
namespace ShowInDesk 
{ 
class Win32Support 
{ 
[DllImport("", CharSet = )] 
publicstaticextern IntPtr FindWindow(string className, string windowName); 
[DllImport("", CharSet = , ExactSpelling =true)] 
publicstaticextern IntPtr GetWindow(HandleRef hWnd, int nCmd); 
[DllImport("")] 
publicstaticextern IntPtr SetParent(IntPtr child, IntPtr parent); 
[DllImport("", EntryPoint ="GetDCEx", CharSet = , ExactSpelling =true)] 
publicstaticextern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags); 
[DllImport("", CharSet = , ExactSpelling =true)] 
publicstaticexternbool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy, int flags); 
[DllImport("")] 
publicstaticexternint ReleaseDC(IntPtr window, IntPtr handle); 
} 
} 

3、

namespace ShowInDesk 
{ 
publicenum DesktopLayer 
{ 
Progman =0, 
SHELLDLL =1, 
FolderView =2 
} 
} 

three,
Enter the bottom layer of the desktop window and provide detailed implementation code for reference.
This type always places the form at the bottom of the window.
First, call some WinAPI functions.

internal class User32 
{ 
public const int SE_SHUTDOWN_PRIVILEGE =0x13; 
[DllImport("")] 
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
[DllImport("")] 
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 
[DllImport("")] 
public static externbool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, 
int cy, uint uFlags); 
} 

Then, inside WinForm:

public MainForm()
{
InitializeComponent();
try
{
if ( <6)
{
();
IntPtr hWndNewParent = ("Progman", null);
(, hWndNewParent);
}
else
{
(, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
}
}
catch (ApplicationException exx)
{
(this, , "Pin to Desktop");
}
}
private void MainForm_Activated(object sender, EventArgs e)
{
if ( >=6)
{
(, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
}
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
if ( >=6)
{
(, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
}
}


The above introduction is how C# can embed WinForm into the bottom layer of the desktop window. I hope it will be helpful to you.