目標:由於需求界面美觀,圓角邊距等等,所以整個界面使用背景圖,去除自帶邊框及按鈕。
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;
}