WPF设置窗口模式(Windowstyle=“None”)

Posted 积跬步---行千里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF设置窗口模式(Windowstyle=“None”)相关的知识,希望对你有一定的参考价值。

当WindowStyle=“None”时,设置AllowsTransparency="True",则不会出现黑色Border,然后可以另外设置外边的Border和BorderThickness;

当WindowStyle=“None”时,生成的窗体无法移动,此时需要定义MouseMove="Window_MouseMove"事件,实现鼠标左键按下可拖动窗口;

xaml代码如下:

<Window x:Class="CustomWindowDemo.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"
        xmlns:local="clr-namespace:CustomWindowDemo"
        mc:Ignorable="d" MouseMove="Window_MouseMove"
        WindowStyle="None" AllowsTransparency="True" BorderBrush="Orange" BorderThickness="3,0,3,3"
        Title="MainWindow" Height="300" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="80"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Background="Orange">
            <TextBlock Margin="20,5,5,5" Foreground="Red" Text="TestWindow" FontSize="14" VerticalAlignment="Center"/>
        </StackPanel>
        <TextBlock Margin="20,30" FontSize="16" Text="This is a Test Window of customized." Grid.Row="1" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
        <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Name="Cancel" Click="Cancel_Click" Margin="0,5,20,5" Height="40" Width="60">Cancel</Button>
            <Button Name="Confirm" Click="Confirm_Click" Margin="20,5,0,5" Height="40" Width="60">Confirm</Button>
        </StackPanel>
    </Grid>
</Window>

后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
                //Window.DragMove();
            }
        }
    }
}

 

以上是关于WPF设置窗口模式(Windowstyle=“None”)的主要内容,如果未能解决你的问题,请参考以下文章

WPF 关于窗口的一些显示效果

如何在 WindowStyle="None" 的 WPF 窗口中强制执行 MinWidth 和 MinHeight?

wpf 如何启动一个窗口 是最大化的

WPF WindowStyle为None

WPF 窗口透明度(包括非客户区)

WPF自定义窗口最大化显示任务栏