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

}

相关推荐
dotent·2 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图3 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞3 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii3 天前
WPF入门指南:解析默认项目结构
wpf
极客智造3 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad5 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168885 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad5 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔5 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad5 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf