| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 768 人关注过本帖
标题:WPF 无边框窗体设置的方法(不使用AllowsTransparency)
只看楼主 加入收藏
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
结帖率:100%
收藏
 问题点数:0 回复次数:2 
WPF 无边框窗体设置的方法(不使用AllowsTransparency)
如果AllowsTransparency设置为true
1、整体渲染性能降低,将会占用更多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." };
        }
    }
}



匆忙临时写的,例子比较简单,如有错误自行修正。的,其他版本,去掉里面的“?”等等,自行修改。
搜索更多相关主题的帖子: using new Window System public 
2023-09-06 16:29
bigleg5201
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-4-14
收藏
得分:0 
那主窗口再怎么添加其他
2023-10-31 14:58
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:0 
回复 2楼 bigleg5201
需要添加什么说一下,我给写个demo
2023-10-31 16:46
快速回复:WPF 无边框窗体设置的方法(不使用AllowsTransparency)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060024 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved