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

}

相关推荐
甜甜不吃芥末19 小时前
WPF依赖属性详解
wpf
Hat_man_21 小时前
WPF制作图片闪烁的自定义控件
wpf
晚安苏州2 天前
WPF Binding 绑定
wpf·wpf binding·wpf 绑定
wangnaisheng2 天前
【WPF】RenderTargetBitmap的使用
wpf
dotent·3 天前
WPF 完美解决改变指示灯的颜色
wpf
orangapple4 天前
WPF 用Vlc.DotNet.Wpf实现视频播放、停止、暂停功能
wpf·音视频
ysdysyn4 天前
wpf mvvm 数据绑定数据(按钮文字表头都可以),根据长度进行换行,并把换行的文字居中
c#·wpf·mvvm
orangapple4 天前
WPF 使用LibVLCSharp.WPF实现视频播放、停止、暂停功能
wpf
晚安苏州4 天前
WPF ControlTemplate 控件模板
wpf
晚安苏州4 天前
WPF 布局控件
wpf