• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > c# winform窗口一直置顶显示在桌面最上方或最底层的方法

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#winform,c#winform窗体美化,c#winform教程,c#winform视频教程,c#winform开发框架等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、

在最前面:
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); 
} 
} 
} 
</div>

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

3、

namespace ShowInDesk 
{ 
publicenum DesktopLayer 
{ 
Progman =0, 
SHELLDLL =1, 
FolderView =2 
} 
} 
</div>

三、
入桌面窗口最底层,并提供详细的实现代码供参考。
此类将窗体永远置于窗口最底层。
首先, 调用一些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); 
} 
</div>

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


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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 分享两种实现Winform程序的多语言支持的多种解决方案
  • Winform中进行MD5加密的实例
  • Winform窗体圆角设计代码
  • C#在winform中实现数据增删改查等功能
  • C# winform 模拟键盘输入自动接入访问网络的实例
  • WinForm天猫双11自动抢红包源码分享
  • Winform实现鼠标可穿透的窗体镂空效果
  • WinForm单例窗体用法实例
  • WinForm遍历窗体所有子控件的方法
  • C#使用winform简单导出Excel的方法

相关文章

  • 2017-05-28C#获取本机IP地址(ipv4)
  • 2017-05-28C#读写操作app.config中的数据应用介绍
  • 2017-05-28C#中is与as的区别分析
  • 2017-05-28C#使用foreach循环遍历数组完整实例
  • 2017-05-28深入理解C# DateTime日期格式化
  • 2017-05-28C#判断密码强度的方法
  • 2017-05-28C#嵌套类的访问方法
  • 2017-05-28C# JsonHelper 操作辅助类,拿来直接用
  • 2017-05-28C# 从枚举值获取对应的文本描述详解
  • 2017-05-28C#数值转换-显式数值转换表(参考)

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#窗体显示留言时间的方法
    • C# 匿名方法基础回顾
    • c#获得目标服务器中所有数据库名、表名、列名的实现代码
    • 测试stringbuilder运行效率示例
    • C#实现Windows Form调用R进行绘图与显示的方法
    • c# split分隔字符串使用方法
    • C#中把任意类型的泛型集合转换成SQLXML数据格式的实例
    • c#得到本月有几周和这几周的起止时间示例代码
    • C#生成不重复随机数列表实例
    • C#多线程处理多个队列数据的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有