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

}

相关推荐
军训猫猫头5 小时前
52.this.DataContext = new UserViewModel(); C#例子 WPF例子
开发语言·c#·wpf
Maybe_ch12 小时前
WPF-系统资源
wpf
苏克贝塔15 小时前
WPF3-在xaml中引用其他程序集的名称空间
wpf
军训猫猫头17 小时前
54.DataGrid数据框图 C#例子 WPF例子
ui·c#·wpf
她说彩礼65万1 天前
WPF 使用webView显示浏览器网页
wpf
de之梦-御风1 天前
【WPF】WPF设置自定义皮肤主题
wpf
^@^lemon tea^@^2 天前
WPF 引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常 解决办法
windows·wpf·ocx控件开发错误
苏克贝塔2 天前
WPF1-从最简单的xaml开始.md
wpf
踏上青云路2 天前
WPF MVVM 模式如何监听IsVisibleChanged 事件
wpf
Alasdair_lu2 天前
WPF 字符串传值到后端
开发语言·wpf