C# 窗口鼠標穿透以及取消窗口鼠標穿透

 

        private const int WS_EX_TRANSPARENT = 0x20;

        private const int GWL_EXSTYLE = -20;

        /// <summary>
        /// window 擴展樣式 分層顯示
        /// </summary>
        private  const int WS_EX_LAYERED = 0x00080000;//取消鼠標穿透的


        [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);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

        public static void SetTranMouseWind(Window window)
        {
            try
            {
                IntPtr hwnd = new WindowInteropHelper(window).Handle;
                uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
                SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
            }
            catch { }

        }
        public static void ReSetNormalWind(Window window)
        {
            try
            {
                IntPtr hwnd = new WindowInteropHelper(window).Handle;
                uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE| WS_EX_LAYERED);
                SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
            }
            catch { }

        }

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章