.net - WPF 中的 Window StateChanging 事件

标签 .net wpf user-interface xaml

我需要在 WPF 应用程序最小化之前处理它,而不是在它已经存在时。
我在 Window 对象 StateChanged 上找到了,但是当 Window 对象已经处于最小化状态时它会触发,那么为时已晚。

所以,我需要像“StateChanging”这样的事件来处理,而 Window 对象仍处于以前的状态。

是否有可能创建这样的事件?

最佳答案

在使用 Spy++ 最小化之前找到在窗口上调用的 Windows 消息。第一个被调用的是 WM_WINDOWPOSCHANGING。
我不知道 Windows 在最小化寡妇时正在 -32000、-32000 位置点移动窗口,这些是 WM_WINDOWPOSCHANGING 中的参数。不过,我只在 Vista 上测试过。 http://blogs.msdn.com/oldnewthing/archive/2004/10/28/249044.aspx

此处使用的代码由 Nir ​​发布 here

这是示例代码

xaml :

<Window x:Class="WindowStateTest2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>

        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
        <Button Click="btnClear_Click" Grid.Row="0" x:Name="btnClear">Clear</Button>            

        <TextBox Name="txt" VerticalScrollBarVisibility="Visible" Grid.Row="2"></TextBox>
</Grid>
</Window>

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace WindowStateTest2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        this.StateChanged += new EventHandler(Window1_StateChanged);
        this.SourceInitialized += new EventHandler(Window1_SourceInitialized);

    }

    #region Event handlers

    void btnClear_Click(object sender, RoutedEventArgs e)
    {
        this.txt.Text = string.Empty;
    }
    void Window1_SourceInitialized(object sender, EventArgs e)
    {
        AttachWndProc();
    }

    void Window1_StateChanged(object sender, EventArgs e)
    {
        if (this.WindowState == WindowState.Minimized)
            Console.WriteLine("SC: " + this.WindowState);
    } 

    #endregion

    #region Const

    private int SYSCOMMAND = 0x0112;
    private int SC_MINIMIZE = 0xf020;
    private int WINDOWPOSCHANGING = 0x0046;

    #endregion

    private void AttachWndProc()
    {
        HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
        source.AddHook(new HwndSourceHook(WndProc));
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct WINDOWPOSPARAMS
    {
        public IntPtr hwnd;
        public IntPtr hwndInsertAfter;
        public int x;
        public int y;
        public int cx;
        public int cy;
        public int flags;
    }


    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == WINDOWPOSCHANGING)               
        {
            WINDOWPOSPARAMS param = (WINDOWPOSPARAMS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOSPARAMS));
            if (param.x == -32000 && param.y == -32000)
            {
                Output("");

                                    // EVENT WOULD BE RAISED HERE

                Output("State before minimize:");
                Output(string.Format("CurrentState: {0}", this.WindowState));
                Output(string.Format("Location {0} {1}: ", this.Top, this.Left));
                Output("");
            }
        }

        // process minimize button
        if (msg == SYSCOMMAND && SC_MINIMIZE == wParam.ToInt32())
        {
            Output("Minimize clicked");             
        }

        handled = false;
        return IntPtr.Zero;
    }

    public void Output(object output)
    {
        this.txt.Text += output.ToString();
        this.txt.Text += Environment.NewLine;           
    }       

}
}

关于.net - WPF 中的 Window StateChanging 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/926758/

相关文章:

php - 在 Laravel 5.3 的任何 View 中隐藏任何元素的聪明方法?

JavaFX setCellFactory 未设置我的项目

c# - Web.config 生产环境性能 - 最佳实践

c# - 使用导航服务在页面之间进行动画转换

javascript - 在两个可拖动的 Flatlist-React Native 之间拖放

wpf - 上下文菜单与弹出菜单

c# - 如何以编程方式访问 DataTemplate 的关联数据类型?

c# - DropDownList 中的 ASP.NET Bind() - 字符串值与 Null

c# - 控制 LTR 和 RTL 语言之间的对齐切换

.net - IoC.Resolve 与构造函数注入(inject)