新建一个 AppWrapper工具类,用来注册事件,获取窗口句柄。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.InteropServices; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace CombinationKeyRecord 9 { 10 public class APIWrapper 11 { 12 [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)] 13 public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData); 14 [DllImport("User32.dll", CharSet = CharSet.Auto)] 15 public static extern int RegisterWindowMessage(string msg); 16 //取得Shell窗口句柄函数 17 [DllImport("user32.dll")] 18 public static extern IntPtr GetShellWindow(); 19 //取得桌面窗口句柄函数 20 [DllImport("user32.dll")] 21 public static extern IntPtr GetDesktopWindow(); 22 //取得前台窗口句柄函数 23 [DllImport("user32.dll")] 24 public static extern IntPtr GetForegroundWindow(); 25 } 26 [StructLayout(LayoutKind.Sequential)] 27 public struct RECT 28 { 29 public int left; 30 public int top; 31 public int right; 32 public int bottom; 33 } 34 [StructLayout(LayoutKind.Sequential)] 35 public struct APPBARDATA 36 { 37 public int cbSize; 38 public IntPtr hWnd; 39 public int uCallbackMessage; 40 public int uEdge; 41 public RECT rc; 42 public IntPtr lParam; 43 } 44 public enum ABMsg : int 45 { 46 ABM_NEW = 0, 47 ABM_REMOVE, 48 ABM_QUERYPOS, 49 ABM_SETPOS, 50 ABM_GETSTATE, 51 ABM_GETTASKBARPOS, 52 ABM_ACTIVATE, 53 ABM_GETAUTOHIDEBAR, 54 ABM_SETAUTOHIDEBAR, 55 ABM_WINDOWPOSCHANGED, 56 ABM_SETSTATE 57 } 58 public enum ABNotify : int 59 { 60 ABN_STATECHANGE = 0, 61 ABN_POSCHANGED, 62 ABN_FULLSCREENAPP, 63 ABN_WINDOWARRANGE 64 } 65 public enum ABEdge : int 66 { 67 ABE_LEFT = 0, 68 ABE_TOP, 69 ABE_RIGHT, 70 ABE_BOTTOM 71 } 72 }
在Form类中添加这些方法和属性
1 Boolean RunningFullScreenApp = false; 2 private IntPtr desktopHandle; 3 private IntPtr shellHandle; 4 int uCallBackMsg; 5 6 private void RegisterAppBar(bool registered) 7 { 8 APPBARDATA abd = new APPBARDATA(); 9 abd.cbSize = Marshal.SizeOf(abd); 10 abd.hWnd = this.Handle; 11 12 desktopHandle = APIWrapper.GetDesktopWindow(); 13 shellHandle = APIWrapper.GetShellWindow(); 14 if (!registered) 15 { 16 //register 17 uCallBackMsg = APIWrapper.RegisterWindowMessage("APPBARMSG_CSDN_HELPER"); 18 abd.uCallbackMessage = uCallBackMsg; 19 uint ret = APIWrapper.SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd); 20 } 21 else 22 { 23 APIWrapper.SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd); 24 } 25 } 26 27 28 //重载窗口消息处理函数 29 protected override void WndProc(ref System.Windows.Forms.Message m) 30 { 31 if (m.Msg == uCallBackMsg) 32 { 33 switch (m.WParam.ToInt32()) 34 { 35 case (int)ABNotify.ABN_FULLSCREENAPP: 36 { 37 IntPtr hWnd = APIWrapper.GetForegroundWindow(); 38 //判断当前全屏的应用是否是桌面 39 if(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle)) 40 { 41 RunningFullScreenApp = false; 42 break; 43 } 44 //判断是否全屏 45 if ((int)m.LParam == 1) 46 this.RunningFullScreenApp = true; 47 else 48 this.RunningFullScreenApp = false; 49 break; 50 } 51 default: 52 break; 53 } 54 } 55 base.WndProc(ref m); 56 }
使用方法:
RegisterAppBar(false);//注册该事件;
RegisterAppBar(true);//清除该事件;