欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-11 23:33:58
一、 在最前面: using system.runtime.interopservices; 在定义部分引入下面两个函数: [dllimport( "user...

一、

在最前面:
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嵌入桌面窗口最底层,希望对你有所帮助。

上一篇:

下一篇: