WPF 使用WindowChrome自定义窗体 保留原生窗体特性

本文大幅度借鉴dino.c大佬的文章html

https://www.cnblogs.com/dino623/p/uielements_of_window.htmlshell

https://www.cnblogs.com/dino623/p/problems_of_WindowChrome.htmlexpress

https://www.cnblogs.com/dino623/p/custom_window_style_using_WindowChrome.htmlwindows

我在这里汇总一下,属于粘了就能用那种。在预设100,125,150,175DPI下最大化也能正常显示。api

不懂的地方能够阅读上面的文章框架

说到原生窗体的特性都有什么 咱来作个对比 你们来感觉下函数

使用WindowChrome测试

使用WindowStyle="None"动画

使用WindowChrome的时候 无需设置就保留了原生阴影、拖拽、交互动画ui

而使用WindowStyle="None"的话,会发现最大化的时候会覆盖任务栏。

想要实现原生的样式就须要本身手写。

这些功能能够实现吗?能实现。效果好吗? 不必定,像我这种WPF玩的不专业的人很难实现这些功能,啥玩意都得上网扒拉,对不对路还不必定,那百度上一人一种写法,这就很闹心。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" SnapsToDevicePixels="True" StateChanged="Window_StateChanged" Loaded="Window_Loaded">
    <WindowChrome.WindowChrome>
        <WindowChrome UseAeroCaptionButtons="False" NonClientFrameEdges="None" CaptionHeight="40" />
    </WindowChrome.WindowChrome>
    <Grid x:Name="grdMain" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="#C62F2F">
            <WrapPanel Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,10,0">
                <Button x:Name="btnMin" Style="{DynamicResource MinButton}" Click="BtnMin_Click"/>
                <Button x:Name="btnNorm" Style="{DynamicResource MaxButton}" Margin="3,0,0,0" Click="BtnNorm_Click"/>
                <Button x:Name="btnClose" Style="{DynamicResource CloseButton}" Margin="3,0,0,0" Click="BtnClose_Click"/>
            </WrapPanel>
        </Grid>
        <Grid Grid.Row="1" Background="#FFFFFF">
            <Border BorderThickness="1" BorderBrush="#C62F2F"/>
        </Grid>
    </Grid>
</Window>

UseAeroCaptionButtons 表示是对 Windows Aero 标题按钮启用的命中测试是否可用,默认值为True。

NonClientFrameEdges 获取或设置一个值,该值表示窗口框架边缘是否归客户端全部,默认值为None。

CaptionHeight 表示窗体菜单栏高度,我这里设置跟Gird里第一行高度一致,表明自定义的菜单栏。

注意下,我在放置按钮的WrapPanel容器中设置了 WindowChrome.IsHitTestVisibleInChrome,该值表示 WPF 命中测试在窗口非工做区中的元素是否可用,默认值为False。

接下来是后台代码

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        int paddings = 0;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            paddings = 4;
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                Thickness thickness = SystemParameters.WindowResizeBorderThickness;
                grdMain.Margin = new Thickness(thickness.Left + paddings, thickness.Top + paddings, thickness.Right + paddings, thickness.Bottom + paddings);
            }
            else
            {
                grdMain.Margin = new Thickness(0);
            }
        }

        private void BtnMin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnNorm_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                btnNorm.Style = (Style)FindResource("NormButton");
                WindowState = WindowState.Maximized;
            }
            else
            {
                btnNorm.Style = (Style)FindResource("MaxButton");
                WindowState = WindowState.Normal;
            }
        }
    }
}

这里的paddings我默认设置为4,这里须要借鉴下大佬的第1、二篇文章,里面很清晰的解释道为啥设置为4。

完整看过第二篇文章的会发现没法直接获取SM_CXPADDEDBORDER的值。

我查了官网文档user32.dll里面有函数能够取值。https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-getsystemmetrics

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int GetSystemMetrics(int index);

只不过按照这个取值来的话125往上的DPI最大化显示不正常,边框会比以前厚,咱就默认4就行。