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>();

}

相关推荐
九鼎科技-Leo6 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
麻花201316 小时前
C#之WPF的C1FlexGrid空间的行加载事件和列事件变更处理动态加载的枚举值
开发语言·c#·wpf
lcintj16 小时前
【WPF】Prism学习(九)
学习·wpf·prism
界面开发小八哥16 小时前
界面控件DevExpress WPF中文教程:网格视图数据布局的列和卡片字段
wpf·界面控件·devexpress·ui开发·用户界面
△曉風殘月〆16 小时前
如何在WPF中嵌入其它程序
wpf
Crazy Struggle16 小时前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
shepherd枸杞泡茶1 天前
WPF动画
c#·.net·wpf
lcintj1 天前
【WPF】Prism学习(十)
学习·wpf·prism
wyh要好好学习1 天前
WPF数据加载时添加进度条
ui·wpf
code_shenbing1 天前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计