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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM