The c winform window is always placed at the top of the table or at the bottom of the table

  • 2020-05-09 19:06:24
  • OfStack

1.

At the front:
using System.Runtime.InteropServices;
In the definition section, introduce the following two functions:
[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) to the form On_Load event:
IntPtr hDeskTop=FindWindow( "Progman ", "Program Manager ");
SetParent(this.Handle,hDeskTop);
Another method can modify the desktop wallpaper implementation
After testing, win2000--win2003, xp embedded desktop, vista and win7 above system is not supported

2.


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
namespace ShowInDesk 
{ 
publicpartialclass Form1 : Form 
{ 
IntPtr hDesktop; 
publicconstint GW_CHILD =5; 
public Form1() 
{ 
InitializeComponent(); 
this.hDesktop = GetDesktopHandle(DesktopLayer.Progman); 
EmbedDesktop(this, this.Handle, this.hDesktop); 
isMouseDown =false; 
} 
public IntPtr GetDesktopHandle(DesktopLayer layer) { //hWnd = new HandleRef(); 
HandleRef hWnd; 
IntPtr hDesktop =new IntPtr(); 
switch (layer) 
{ 
case DesktopLayer.Progman: 
hDesktop = Win32Support.FindWindow("Progman", null);// The first 1 Layer of the desktop  
break; 
case DesktopLayer.SHELLDLL: 
hDesktop = Win32Support.FindWindow("Progman", null);// The first 1 Layer of the desktop  
hWnd =new HandleRef(this, hDesktop); 
hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);// The first 2 Layer of the desktop  
break; 
case DesktopLayer.FolderView: 
hDesktop = Win32Support.FindWindow("Progman", null);// The first 1 Layer of the desktop  
hWnd =new HandleRef(this, hDesktop); 
hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);// The first 2 Layer of the desktop  
hWnd =new HandleRef(this, hDesktop); 
hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);// The first 3 Layer of the desktop  
break; 
} 
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 a window size change message  
Win32Support.SetParent(childWindow, parentWindow); 
Win32Support.SetWindowPos(new HandleRef(window, childWindow), HWND_BOTTOM, 300, 300, window.Width, window.Height, SWP_FRAMECHANGED); 
} 
} 
} 

2,


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

3,


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

3.
Enter the bottom level of the desktop window and provide detailed implementation code for reference.
This class always places the form at the bottom of the window.
First, call some WinAPI functions of User32.dll.


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

Then, in WinForm:


public MainForm()
{
InitializeComponent();
try
{
if (Environment.OSVersion.Version.Major <6)
{
base.SendToBack();
IntPtr hWndNewParent = User32.FindWindow("Progman", null);
User32.SetParent(base.Handle, hWndNewParent);
}
else
{
User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
}
}
catch (ApplicationException exx)
{
MessageBox.Show(this, exx.Message, "Pin to Desktop");
}
}
private void MainForm_Activated(object sender, EventArgs e)
{
if (Environment.OSVersion.Version.Major >=6)
{
User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
}
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
if (Environment.OSVersion.Version.Major >=6)
{
User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
}
}


This is how C# can embed WinForm at the bottom of your desktop window.


Related articles: