.net – WPF中的Window StateChanging事件

前端之家收集整理的这篇文章主要介绍了.net – WPF中的Window StateChanging事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在它最小化之前处理WPF应用程序,而不是在它已经存在的时候.
我在Window对象StateChanged上找到了,但是当Window对象已经处于Minimize状态时它会触发,那就太晚了.

因此,我需要处理类似“StateChanging”事件的事情,而Window对象仍处于先前状态.

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

在使用Spy最小化之前找到窗口上调用的窗口消息.第一个被调用的是WM_WINDOWPOSCHANGING.
我不知道窗口在-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;           
    }       

}
}
原文链接:https://www.f2er.com/windows/371899.html

猜你在找的Windows相关文章