WPF 主窗口最大化、最小化、关闭、可拖动

MainWindow.xaml

<Window x:Class="TestTool.View.MainWindow"

......

AllowsTransparency="True"

WindowStartupLocation="CenterScreen"

WindowStyle="None"

AllowDrop="True" MouseLeftButtonDown="Window_MouseLeftButtonDown">

......

<Grid Background="Blue" Grid.ColumnSpan="2">

<TextBlock Text="Test Tool" FontWeight="Bold" FontSize="22" Foreground="#FFFFFF" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="30,0,0,0"/>

<UniformGrid Columns="3" Rows="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="100" Height="36" Margin="0,6,10,0">

<Button x:Name="btn_minimize" Content="---" FontSize="24" Foreground="White" Width="32" Height="36" Background="Transparent" BorderThickness="0" Click="btn_minimize_Click"/>

<Button x:Name="btn_maximize" Content="□" FontSize="24" Foreground="White" Width="32" Height="36" Background="Transparent" BorderThickness="0" Click="btn_maximize_Click"/>

<Button x:Name="btn_close" Content="×" FontSize="26" Foreground="White" Width="32" Height="36" Background="Transparent" BorderThickness="0" Click="btn_close_Click"/>

</UniformGrid>

</Grid>

MainWindow.xaml.cs

private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

{

base.OnMouseLeftButtonDown(e);

//获取鼠标相对窗口的位置

Point position = e.GetPosition(this);

//鼠标放在窗口就移动

if (e.LeftButton == MouseButtonState.Pressed)

{

if (position.X >= 0 && position.X < this.ActualWidth && position.Y >= 0 && position.Y <= this.ActualWidth)

{

this.DragMove();

}

}

}

private void btn_minimize_Click(object sender, RoutedEventArgs e)

{

this.WindowState = WindowState.Minimized;

}

private void btn_maximize_Click(object sender, RoutedEventArgs e)

{

if (WindowState == WindowState.Maximized)

{

this.WindowState = WindowState.Normal;

}

else

{

this.WindowStyle = WindowStyle.None;

this.ResizeMode = ResizeMode.NoResize;

this.WindowState = WindowState.Maximized;

}

}

private void btn_close_Click(object sender, RoutedEventArgs e)

{

ViewModel.ViewModelLocator.CleanupMainViewModel();

this.Close();

}

ViewModelLocator.cs

......

public static void CleanupMainViewModel()

{

Cleanup<MainViewModel>();

}

private static void Cleanup<T>() where T : ViewModelBase

{

T viewModel = ServiceLocator.Current.GetInstance<T>();

if (viewModel != null)

viewModel.Cleanup();

SimpleIoc.Default.Unregister<T>();

}

相关推荐
冷眼Σ(-᷅_-᷄๑)34 分钟前
WPF缩放动画和平移动画叠加后会发生什么?
wpf·动画
△曉風殘月〆3 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
.net开发10 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
九鼎科技-Leo10 小时前
WPF 中 NavigationWindow 与 Page 的继承关系解析
wpf
SongYuLong的博客11 小时前
C# WPF 打印机
wpf
就是有点傻11 小时前
WPF中的转换器
wpf
.net开发18 小时前
WPF使用prism框架发布订阅实现消息提示
c#·.net·wpf
那少年已不再......1 天前
C#WPF使用CommunityToolkit.Mvvm库
开发语言·c#·wpf
SEO-狼术1 天前
Syncfusion Essential Studio WPF 2024 Crack
wpf
Olivia_vivi2 天前
WPF XAML
ui·wpf