/// <summary> /// 设置点击命中,不会穿透到后面的窗口 /// </summary> public void SetTransparentNotHitThrough() { if (_dwmEnabled) { Win32.User32.SetWindowLongPtr(_hwnd, Win32.GetWindowLongFields.GWL_EXSTYLE, (IntPtr)(int)((long)Win32.User32.GetWindowLongPtr(_hwnd, Win32.GetWindowLongFields.GWL_EXSTYLE) & ~(long)Win32.ExtendedWindowStyles.WS_EX_TRANSPARENT)); } else { Background = BrushCreator.GetOrCreate("#0100000"); } }
/// <summary> /// 创建高性能透明桌面窗口 /// </summary> public PerformanceDesktopTransparentWindow() { WindowStyle = WindowStyle.None; ResizeMode = ResizeMode.NoResize; WindowChrome.SetWindowChrome(this, new WindowChrome { GlassFrameThickness = WindowChrome.GlassFrameCompleteThickness, CaptionHeight = 0 }); var visualTree = new FrameworkElementFactory(typeof(Border)); visualTree.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Window.BackgroundProperty)); var childVisualTree = new FrameworkElementFactory(typeof(ContentPresenter)); childVisualTree.SetValue(UIElement.ClipToBoundsProperty, true); visualTree.AppendChild(childVisualTree); Template = new ControlTemplate { TargetType = typeof(Window), VisualTree = visualTree, }; _dwmEnabled = Win32.Dwmapi.DwmIsCompositionEnabled(); if (_dwmEnabled) { _hwnd = new WindowInteropHelper(this).EnsureHandle(); Loaded += PerformanceDesktopTransparentWindow_Loaded; Background = Brushes.Transparent; } else { AllowsTransparency = true; Background = BrushCreator.GetOrCreate("#0100000"); _hwnd = new WindowInteropHelper(this).EnsureHandle(); } }