.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:

  1. <Window x:Class="WindowStateTest2.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Window1" Height="300" Width="300">
  5. <Grid>
  6. <Grid.RowDefinitions>
  7. <RowDefinition Height="Auto"></RowDefinition>
  8.  
  9. <RowDefinition Height="*"></RowDefinition>
  10. </Grid.RowDefinitions>
  11. <Button Click="btnClear_Click" Grid.Row="0" x:Name="btnClear">Clear</Button>
  12.  
  13. <TextBox Name="txt" VerticalScrollBarVisibility="Visible" Grid.Row="2"></TextBox>
  14. </Grid>
  15. </Window>

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Windows.Interop;
  15. using System.Runtime.InteropServices;
  16.  
  17. namespace WindowStateTest2
  18. {
  19. /// <summary>
  20. /// Interaction logic for Window1.xaml
  21. /// </summary>
  22. public partial class Window1 : Window
  23. {
  24. public Window1()
  25. {
  26. InitializeComponent();
  27.  
  28. this.StateChanged += new EventHandler(Window1_StateChanged);
  29. this.SourceInitialized += new EventHandler(Window1_SourceInitialized);
  30.  
  31. }
  32.  
  33. #region Event handlers
  34.  
  35. void btnClear_Click(object sender,RoutedEventArgs e)
  36. {
  37. this.txt.Text = string.Empty;
  38. }
  39. void Window1_SourceInitialized(object sender,EventArgs e)
  40. {
  41. AttachWndProc();
  42. }
  43.  
  44. void Window1_StateChanged(object sender,EventArgs e)
  45. {
  46. if (this.WindowState == WindowState.Minimized)
  47. Console.WriteLine("SC: " + this.WindowState);
  48. }
  49.  
  50. #endregion
  51.  
  52. #region Const
  53.  
  54. private int SYSCOMMAND = 0x0112;
  55. private int SC_MINIMIZE = 0xf020;
  56. private int WINDOWPOSCHANGING = 0x0046;
  57.  
  58. #endregion
  59.  
  60. private void AttachWndProc()
  61. {
  62. HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
  63. source.AddHook(new HwndSourceHook(WndProc));
  64. }
  65.  
  66. [StructLayout(LayoutKind.Sequential)]
  67. internal struct WINDOWPOSPARAMS
  68. {
  69. public IntPtr hwnd;
  70. public IntPtr hwndInsertAfter;
  71. public int x;
  72. public int y;
  73. public int cx;
  74. public int cy;
  75. public int flags;
  76. }
  77.  
  78.  
  79. private IntPtr WndProc(IntPtr hwnd,int msg,IntPtr wParam,IntPtr lParam,ref bool handled)
  80. {
  81. if (msg == WINDOWPOSCHANGING)
  82. {
  83. WINDOWPOSPARAMS param = (WINDOWPOSPARAMS)Marshal.PtrToStructure(lParam,typeof(WINDOWPOSPARAMS));
  84. if (param.x == -32000 && param.y == -32000)
  85. {
  86. Output("");
  87.  
  88. // EVENT WOULD BE RAISED HERE
  89.  
  90. Output("State before minimize:");
  91. Output(string.Format("CurrentState: {0}",this.WindowState));
  92. Output(string.Format("Location {0} {1}: ",this.Top,this.Left));
  93. Output("");
  94. }
  95. }
  96.  
  97. // process minimize button
  98. if (msg == SYSCOMMAND && SC_MINIMIZE == wParam.ToInt32())
  99. {
  100. Output("Minimize clicked");
  101. }
  102.  
  103. handled = false;
  104. return IntPtr.Zero;
  105. }
  106.  
  107. public void Output(object output)
  108. {
  109. this.txt.Text += output.ToString();
  110. this.txt.Text += Environment.NewLine;
  111. }
  112.  
  113. }
  114. }

猜你在找的Windows相关文章