WPF 无边框窗体设置的方法(不使用AllowsTransparency)
如果AllowsTransparency设置为true1、整体渲染性能降低,将会占用更多CPU资源;
2、将不能窗口中嵌入其他程序窗口,例如播放器等等
不使用AllowsTransparency的无边框窗体,可改变大小:
新建一个类,全部粘贴下面:
程序代码:
using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media.Effects; using System.Windows.Media; using System.Windows.Shell; namespace WindowTemplate { internal class Template { public static SolidColorBrush backColor = Brushes.White; // 背景色 public static double newWidth; // 当前窗体宽度 public static double newHeight; // 当前窗体高度 public static double minWidth = 800; // 窗体最小宽度 public static double minHeight = 450; // 窗体最小高度 public static bool isMove = true; // 能否移动 public static bool isSize = true; // 能否改变大小 /// <summary> /// 窗体样式 /// </summary> /// <param name="windowObj">窗体</param> /// <param name="gridObj">主要grid</param> /// <param name="opacityProperty">透明度</param> public static void Pattern(Window windowObj, Grid gridObj, double opacityProperty = 0.95) { windowObj.WindowStyle = WindowStyle.None; windowObj.ResizeMode = ResizeMode.NoResize; newWidth = windowObj.Width; newHeight = windowObj.Height; WindowChrome windowChrome = new() { GlassFrameThickness = new Thickness(-1), CaptionHeight = 0 }; WindowChrome.SetWindowChrome(windowObj, windowChrome); ControlTemplate controlTemplate = new(typeof(Window)); FrameworkElementFactory rootElementFactory = new(typeof(Border)); rootElementFactory.SetValue(Border.PaddingProperty, new Thickness(13)); rootElementFactory.SetValue(Border.BackgroundProperty, Brushes.Transparent); rootElementFactory.SetValue(UIElement.OpacityProperty, opacityProperty); FrameworkElementFactory innerBorderFactory = new(typeof(Border)); innerBorderFactory.SetValue(Border.BackgroundProperty, backColor); innerBorderFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(0)); DropShadowEffect dropShadowEffect = new() { ShadowDepth = 1, BlurRadius = 13, Direction = 270, Opacity = 0.5, Color = Color.FromArgb(255, 33, 22, 19) }; innerBorderFactory.SetValue(UIElement.EffectProperty, dropShadowEffect); FrameworkElementFactory contentPresenterFactory = new(typeof(ContentPresenter)); contentPresenterFactory.SetValue(UIElement.ClipToBoundsProperty, true); innerBorderFactory.AppendChild(contentPresenterFactory); rootElementFactory.AppendChild(innerBorderFactory); controlTemplate.VisualTree = rootElementFactory; windowObj.Template = controlTemplate; if (isSize) { Thumb size_Right = new() { Width = 3, HorizontalAlignment = HorizontalAlignment.Right, Cursor = Cursors.SizeWE, Opacity = 0, }; size_Right.DragDelta += (sender, e) => { var thumb = (Thumb)sender; var window = ((Grid)thumb.Parent).Parent as Window; newWidth = window!.Width + e.HorizontalChange; window.Width = newWidth < minWidth ? minWidth : newWidth; }; gridObj.Children.Add(size_Right); Thumb size_Bottom = new() { Height = 3, VerticalAlignment = VerticalAlignment.Bottom, Cursor = Cursors.SizeNS, Opacity = 0, }; size_Bottom.DragDelta += (sender, e) => { var thumb = (Thumb)sender; var window = ((Grid)thumb.Parent).Parent as Window; newHeight = window!.Height + e.VerticalChange; window.Height = newHeight < minHeight ? minHeight : newHeight; }; gridObj.Children.Add(size_Bottom); Thumb size_Nwse = new() { Width = 3, Height = 3, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Bottom, Cursor = Cursors.SizeNWSE, Opacity = 0, }; size_Nwse.DragDelta += (sender, e) => { var thumb = (Thumb)sender; var window = ((Grid)thumb.Parent).Parent as Window; newWidth = window!.Width + e.HorizontalChange; newHeight = window!.Height + e.VerticalChange; window.Width = newWidth < minWidth ? minWidth : newWidth; window.Height = newHeight < minHeight ? minHeight : newHeight; }; gridObj.Children.Add(size_Nwse); } } /// <summary> /// 移动窗体 /// </summary> /// <param name="windowObj">窗体</param> /// <param name="element">有效组件</param> public static void DragMove(Window windowObj, FrameworkElement element) { element.PreviewMouseDown += (s, e) => { if (isMove && e.LeftButton == MouseButtonState.Pressed) { windowObj.DragMove(); } }; } } }
使用中:
程序代码:
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Interop; using System.Windows.Media; namespace P4 { public partial class MainWindow : Window { protected override void OnSourceInitialized(EventArgs e) // 禁用硬件加速,防止改变窗口大小时候窗口闪烁 { if (PresentationSource.FromVisual(this) is HwndSource hwndSource) = RenderMode.SoftwareOnly; base.OnSourceInitialized(e); } private ChildWindow? childWindow; public MainWindow() { InitializeComponent(); Canvas box = new() // 拖动的块 { Background = Brushes.Green, Margin = new Thickness(0,0,0,0), }; Main_Grid.Children.Add(box); WindowTemplate.Template.Pattern(this, Main_Grid, 1); // 窗口grid的名字为Main_Grid,前台<Grid x:Name="Main_Grid"> WindowTemplate.Template.DragMove(this, box);// 按住块,拖动窗口 Loaded += MainWindow_Loaded; // 嵌入一个窗口,这里省事,新建一个wpf的窗口 } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { childWindow = new ChildWindow(); // 要嵌入的WPF窗口(例如ChildWindow) childWindow.Show(); var childWindowHandle = new WindowInteropHelper(childWindow).EnsureHandle(); SetParent(childWindowHandle, new WindowInteropHelper(this).Handle); } [DllImport("user32.dll")] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); } public class ChildWindow : Window { public ChildWindow() { // 设置窗口的样式和内容 Title = "Child Window"; Width = 400; Height = 300; Content = new TextBlock { Text = "This is a child window." }; } } }
匆忙临时写的,例子比较简单,如有错误自行修正。的,其他版本,去掉里面的“?”等等,自行修改。