实现效果:
知识运用:
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,指定数据的前一个值
实现代码:
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; //使窗体始终在其他窗体之上 }