c# winform窗口一直置顶显示在桌面最上方或最底层的方法

 更新时间:2013年01月25日 14:24:38   投稿:whsnow  
winform窗口一直置顶显示在桌面最上方,这样的功能真的很实用的,很多的软件窗口都有这样的功能,本文也来实现一个,感兴趣的你千万不要错过了,希望本文对你有所帮助

一、

在最前面:
using System.Runtime.InteropServices;
在定义部分引入下面两个函数:
[DllImport( "user32 ")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport( "user32 ")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
在窗体On_Load事件中添加(Santos的代码):
IntPtr hDeskTop=FindWindow( "Progman ", "Program Manager ");
SetParent(this.Handle,hDeskTop);
另一个方法可以修改桌面壁纸实现
经测试,win2000--win2003 、xp下嵌入桌面,不支持vista和win7以上系统

二、

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);//第一层桌面 
break; 
case DesktopLayer.SHELLDLL: 
hDesktop = Win32Support.FindWindow("Progman", null);//第一层桌面 
hWnd =new HandleRef(this, hDesktop); 
hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);//第2层桌面 
break; 
case DesktopLayer.FolderView: 
hDesktop = Win32Support.FindWindow("Progman", null);//第一层桌面 
hWnd =new HandleRef(this, hDesktop); 
hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);//第2层桌面 
hWnd =new HandleRef(this, hDesktop); 
hDesktop = Win32Support.GetWindow(hWnd, GW_CHILD);//第3层桌面 
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;//发送窗口大小改变消息 
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 
} 
} 

三、
入桌面窗口最底层,并提供详细的实现代码供参考。
此类将窗体永远置于窗口最底层。
首先, 调用一些User32.dll的WinAPI函数。

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); 
} 

然后, 在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);
}
}


以上介绍的就是C#如何让WinForm嵌入桌面窗口最底层,希望对你有所帮助。

相关文章

  • C#实现打印与打印预览功能的思路及代码

    C#实现打印与打印预览功能的思路及代码

    这篇文章主要介绍了C#实现打印与打印预览功能的思路及代码,有需要的朋友可以参考一下
    2013-12-12
  • 深入分析C# 线程同步

    深入分析C# 线程同步

    这篇文章主要介绍了C# 线程同步的的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • c# openxml 删除xlsx、xls的外链示例代码

    c# openxml 删除xlsx、xls的外链示例代码

    要删除一个 Excel 文件(.xlsx)中的外部链接(external links),你可以使用 OpenXML SDK,本文演示如何使用 OpenXML SDK 删除外部链接,感兴趣的朋友一起看看吧
    2024-01-01
  • C#实现将一个矩阵分解为对称矩阵与反称矩阵之和的方法

    C#实现将一个矩阵分解为对称矩阵与反称矩阵之和的方法

    这篇文章主要介绍了C#实现将一个矩阵分解为对称矩阵与反称矩阵之和的方法,较为详细的分析了矩阵分解运算的原理与C#实现技巧,需要的朋友可以参考下
    2015-08-08
  • C# Fiddler插件实现网站离线浏览功能

    C# Fiddler插件实现网站离线浏览功能

    本文主要介绍了C# Fiddler插件实现网站离线浏览功能的原理与方法。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • C# goto语句的具体使用

    C# goto语句的具体使用

    # goto 语句用于直接在一个程序中转到程序中的标签指定的位置,标签实际上由标识符加上冒号构成。本文主要介绍了C# goto语句的具体使用,感兴趣的可以了解一下
    2021-06-06
  • C# [ImportDll()] 知识小结

    C# [ImportDll()] 知识小结

    今天小编就为大家分享一篇关于C# [ImportDll()] 知识小结,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • 详解三种C#实现数组反转方式

    详解三种C#实现数组反转方式

    本篇文章主要介绍了详解三种C#实现数组反转方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 如何用C#验证IP是否为局域网地址

    如何用C#验证IP是否为局域网地址

    这篇文章主要介绍了如何用C#验证IP是否为局域网地址,文中讲解非常细致,代码帮助大家更好的参考和学习,感兴趣的朋友可以了解下
    2020-06-06
  • C#如何添加PPT背景

    C#如何添加PPT背景

    这篇文章主要为大家详细介绍了C#如何添加PPT背景,添加纯色背景、渐变色背景、图片背景等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07

最新评论