zoukankan      html  css  js  c++  java
  • WPF中常用的Window事件

      官方链接:https://msdn.microsoft.com/en-us/library/system.windows.window.statechanged(v=vs.110).aspx

    1. Activated获得焦点事件 和 Deactivated失去焦点的事件:

         Activated: 获得焦点 (首次打开软件时;由别的软件切换回当前软件时;点击当前软件在任务栏的按钮时)

         Deactivated:失去焦点,与Activated正好相反,(Deactivated = de + activated)

         使用方法有两种:

          第一种是在XAML中声明,然后在后台书写执行代码:

    <Window x:Class="WindowsFocusDemo20170117.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"  Activated="Window_Activated">

         在后台xxx.cs文件中书写Window_Activated方法:

    private void Window_Activated(object sender, EventArgs e)
     {
             System.Windows.MessageBox.Show("It is activating...");
    
    }

          第二种是都在后台书写代码:

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Activated += Window_Activated;
            }
    
    
            private void Window_Activated(object sender, EventArgs e)
            {
                System.Windows.MessageBox.Show("It is activating...");
      
            }
            
        }

           失去焦点Deactivated是用法与上述一致。

    2. 关闭中Closing事件 和 已完全关闭Closed事件

          由名字可以看出,Closing事件会比Closed事件早执行,

          触发Closing事件的条件:调用Close()方法;点击软件的关闭按钮;或者使用快捷键Alt+F4关闭

          注意:

                如果窗口存在子窗口,那么调用Close()是不会触发子窗口的Closing事件的,即子窗口不会关闭;

                如果想在关闭时也关闭所有子窗口,请调用Application的Shutdown()方法,用法如下:

    Application.Current.Shutdown();//整个应用都关闭了,当然所有窗口都会关闭

                Closing事件可以理解为,Window窗体正准备关闭,此时还是可以取消关闭操作的,但Closed事件则是无力回天了;

                当用户退出log off或者关闭电脑,Closing方法也不会触发,但Application.SessionEnding事件会触发

    3.ContentRendered事件

    4.StateChanged事件

         窗体状态改变事件,窗体状态WindowState是枚举类型

    public enum WindowState
        {
            Normal = 0,
            Minimized = 1,
            Maximized = 2,
        }

           所以我们可以利用WindowState来判断窗体的状态来实现我们的需求

    5.LocationChanged事件

    6.DpiChanged事件

    7.SourceInitialized事件

  • 相关阅读:
    个人总结
    团队作业五
    个人项目五:个人回顾
    第二次冲刺
    第一次冲刺
    猜数字1
    随机数
    个人作业
    课后作业1
    作业
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6292394.html
Copyright © 2011-2022 走看看