c#winform窗口一直置顶显示在桌面最上方或最底层的方法
2023-06-13 09:14:44 时间
一、
在最前面:
usingSystem.Runtime.InteropServices;
在定义部分引入下面两个函数:
[DllImport("user32")]
privatestaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName);
[DllImport("user32")]
privatestaticexternIntPtrSetParent(IntPtrhWndChild,IntPtrhWndNewParent);
在窗体On_Load事件中添加(Santos的代码):
IntPtrhDeskTop=FindWindow("Progman","ProgramManager");
SetParent(this.Handle,hDeskTop);
另一个方法可以修改桌面壁纸实现
经测试,win2000--win2003、xp下嵌入桌面,不支持vista和win7以上系统
二、
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Runtime.InteropServices; namespaceShowInDesk { publicpartialclassForm1:Form { IntPtrhDesktop; publicconstintGW_CHILD=5; publicForm1() { InitializeComponent(); this.hDesktop=GetDesktopHandle(DesktopLayer.Progman); EmbedDesktop(this,this.Handle,this.hDesktop); isMouseDown=false; } publicIntPtrGetDesktopHandle(DesktopLayerlayer){//hWnd=newHandleRef(); HandleRefhWnd; IntPtrhDesktop=newIntPtr(); switch(layer) { caseDesktopLayer.Progman: hDesktop=Win32Support.FindWindow("Progman",null);//第一层桌面 break; caseDesktopLayer.SHELLDLL: hDesktop=Win32Support.FindWindow("Progman",null);//第一层桌面 hWnd=newHandleRef(this,hDesktop); hDesktop=Win32Support.GetWindow(hWnd,GW_CHILD);//第2层桌面 break; caseDesktopLayer.FolderView: hDesktop=Win32Support.FindWindow("Progman",null);//第一层桌面 hWnd=newHandleRef(this,hDesktop); hDesktop=Win32Support.GetWindow(hWnd,GW_CHILD);//第2层桌面 hWnd=newHandleRef(this,hDesktop); hDesktop=Win32Support.GetWindow(hWnd,GW_CHILD);//第3层桌面 break; } returnhDesktop; } publicvoidEmbedDesktop(ObjectembeddedWindow,IntPtrchildWindow,IntPtrparentWindow) { Formwindow=(Form)embeddedWindow; HandleRefHWND_BOTTOM=newHandleRef(embeddedWindow,newIntPtr(1)); constintSWP_FRAMECHANGED=0x0020;//发送窗口大小改变消息 Win32Support.SetParent(childWindow,parentWindow); Win32Support.SetWindowPos(newHandleRef(window,childWindow),HWND_BOTTOM,300,300,window.Width,window.Height,SWP_FRAMECHANGED); } } }
2、
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Runtime.InteropServices; namespaceShowInDesk { classWin32Support { [DllImport("user32.dll",CharSet=CharSet.Auto)] publicstaticexternIntPtrFindWindow(stringclassName,stringwindowName); [DllImport("user32.dll",CharSet=CharSet.Auto,ExactSpelling=true)] publicstaticexternIntPtrGetWindow(HandleRefhWnd,intnCmd); [DllImport("user32.dll")] publicstaticexternIntPtrSetParent(IntPtrchild,IntPtrparent); [DllImport("user32.dll",EntryPoint="GetDCEx",CharSet=CharSet.Auto,ExactSpelling=true)] publicstaticexternIntPtrGetDCEx(IntPtrhWnd,IntPtrhrgnClip,intflags); [DllImport("user32.dll",CharSet=CharSet.Auto,ExactSpelling=true)] publicstaticexternboolSetWindowPos(HandleRefhWnd,HandleRefhWndInsertAfter,intx,inty,intcx,intcy,intflags); [DllImport("user32.dll")] publicstaticexternintReleaseDC(IntPtrwindow,IntPtrhandle); } }
3、
namespaceShowInDesk { publicenumDesktopLayer { Progman=0, SHELLDLL=1, FolderView=2 } }
三、
入桌面窗口最底层,并提供详细的实现代码供参考。
此类将窗体永远置于窗口最底层。
首先,调用一些User32.dll的WinAPI函数。
internalclassUser32 { publicconstintSE_SHUTDOWN_PRIVILEGE=0x13; [DllImport("user32.dll")] publicstaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName); [DllImport("user32.dll")] publicstaticexternIntPtrSetParent(IntPtrhWndChild,IntPtrhWndNewParent); [DllImport("user32.dll")] publicstaticexternboolSetWindowPos(IntPtrhWnd,inthWndInsertAfter,intX,intY,intcx, intcy,uintuFlags); }
然后,在WinForm里面:
publicMainForm() { InitializeComponent(); try { if(Environment.OSVersion.Version.Major<6) { base.SendToBack(); IntPtrhWndNewParent=User32.FindWindow("Progman",null); User32.SetParent(base.Handle,hWndNewParent); } else { User32.SetWindowPos(base.Handle,1,0,0,0,0,User32.SE_SHUTDOWN_PRIVILEGE); } } catch(ApplicationExceptionexx) { MessageBox.Show(this,exx.Message,"PintoDesktop"); } } privatevoidMainForm_Activated(objectsender,EventArgse) { if(Environment.OSVersion.Version.Major>=6) { User32.SetWindowPos(base.Handle,1,0,0,0,0,User32.SE_SHUTDOWN_PRIVILEGE); } } privatevoidMainForm_Paint(objectsender,PaintEventArgse) { if(Environment.OSVersion.Version.Major>=6) { User32.SetWindowPos(base.Handle,1,0,0,0,0,User32.SE_SHUTDOWN_PRIVILEGE); } }
以上介绍的就是C#如何让WinForm嵌入桌面窗口最底层,希望对你有所帮助。
相关文章
- C# Winform界面美化(借助IrisSkin实现)
- C# winform键盘钩子
- C# winform美化窗体
- c# mysql executenonquery_C#与数据库访问技术之ExecuteNonQuery方法
- C#构造函数的作用_以下程序段的作用是inta
- winform与php交互,WinForm与Javascript交互「建议收藏」
- C#WinForm窗口最小化到系统托盘
- C#Winform调用系统接口操作INI配置文件的代码
- c#中合并DataTable重复行的值
- 深入分析C#中WinForm控件之Dock顺序调整的详解
- c#中的interfaceabstract与virtual介绍
- c#同步两个子目录文件示例分享两个文件夹同步
- c#rsa注册实现加密文字
- c#协变和逆变实例分析
- C#中String类常用方法汇总
- C#(WinForm)ComboBox和ListBox添加项及设置默认选择项
- C#中winform控制textbox输入只能为数字的方法