WPF 入门(1)—自定义关闭按钮
目标:由于需求界面美观,圆角边距等等,所以整个界面使用背景图,去除自带边框及按钮。
1.去除系统自带边框、按钮。
2.自定义退出按钮,并对退出进行提示框提示,同时解决了Alt+F4退出的不提示的问题。
操作:
设置Window属性 AllowsTransparency="True" 透明
WindowStyle="None" 去除边框
MouseDown="Window_MouseDown" 由于没有边框无法鼠标拖动,所以定义拖动方法
Closing="Window_Closing"> 设置页面关闭触发方法
<Window x:Class="QfpayPC.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="740" Width="936" AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown" Closing="Window_Closing">
Window_MouseDown:
//界面可以拖动 private void Window_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { DragMove(); } }
Window_Closing:
//退出提示程序 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { MessageBoxResult result = MessageBox.Show("正在交易中,您真的要退出吗?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { Application.Current.Shutdown(); } else { e.Cancel = true; } }
自定义关闭按钮、最小化按钮
private void btnClose_Click(object sender, RoutedEventArgs e){ this.Close(); } private void btnClose_Click(object sender, RoutedEventArgs e){
this.WindowState = WindowState.Minimized;
}