实现效果:
知识运用:
API函数SetWindowLong和GetWindowLong
在调用API函数的时候要添加 System.Runtime.InteropService命令空间
[DllImport("user32", EntryPoint = "GetWindowLong")] //从指定的结构中取得信息
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
1.返回值:uint,由nIndex决定 零表是出错
[DllImport("user32", EntryPoint = "SetWindowLong")] //在窗口结构中为指定的窗口设置信息
private static extern uint SetWindowLong(IntPtr hwnd,int nIndex,uint dwNewLong);
- nIndex:int,欲取回的信息 其常量如图
- hwnd:IntPtr,欲为其取得信息的窗口的句柄
- dwNewLong:uint,由nIndex指定的窗口信息的新值
- 返回值:uint,指定数据的前一个值
实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private const uint WS_EX_LAYERED = 0x80000; private const int WS_EX_TRANSPARENT = 0x20; private const int GWL_EXSTYLE = -20; private const int LWA_COLORKEY = 1; [DllImport( "user32" , EntryPoint = "SetWindowLong" )] private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong); [DllImport( "user32" , EntryPoint = "GetWindowLong" )] private static extern uint GetWindowLong(IntPtr hwnd, int nIndex); private void CanPenetrate() { uint intExTemp = GetWindowLong( this .Handle, GWL_EXSTYLE); //从当前窗口中取得信息 //在窗口结构中为当前窗口设置信息 uint oldGWLEx = SetWindowLong( this .Handle, GWL_EXSTYLE, WS_EX_LAYERED|WS_EX_TRANSPARENT); } private void Form1_Load( object sender, EventArgs e) { this .ShowInTaskbar = false ; //窗体不出现在Windows任务栏中 CanPenetrate(); this .TopMost = true ; //使窗体始终在其他窗体之上 } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步