title: Event listener if active window moved or resized C#
body: <p>I created an overlay for an application. I just need this 2 to be working:</p>
  <ol>
  <li>The overlay is to be minimised if other windows are opened and maximise if it is the active window. I believe i got this working. In the code below if Notepad is opened, the overlay will minimize.</li>
  <li>A notification if the application window is moved or resized. I cant get it to worked.</li>
  </ol>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  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.Runtime.InteropServices;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Diagnostics;
  
  namespace TriageUserHelp
  {
      /// &lt;summary&gt;
      /// Interaction logic for HomePage.xaml
      /// &lt;/summary&gt;
      partial class HomePage : Page
      {
          private const uint WINEVENT_OUTOFCONTEXT = 0x0000;
          private const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
  
          private const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A;
          private const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B;
  
          WinEventDelegate dele = null;
  
          delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
  
          [DllImport(&quot;user32.dll&quot;)]
          static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
  
          [DllImport(&quot;user32.dll&quot;)]
          static extern IntPtr GetForegroundWindow();
  
          [DllImport(&quot;user32.dll&quot;)]
          static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
  
          [DllImport(&quot;user32.dll&quot;)]
          public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
  
          public HomePage()
          {
              InitializeComponent();
              StartButton.Click += StartButton_Click;
  
              dele = new WinEventDelegate(WinEventProc);
              IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
  
              int processId = Process.GetProcessesByName(&quot;Notepad&quot;)[0].Id;
              IntPtr m_hhook2 = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, WinEventProc, (uint)processId, 0, WINEVENT_OUTOFCONTEXT);
          }
  
          private void StartButton_Click(object sender, RoutedEventArgs e)
          {
              var ProcessPage1 = new ProcessPage1();
              NavigationService?.Navigate(ProcessPage1);
          }
  
          private string GetActiveWindowTitle()
          {
              const int nChars = 256;
              IntPtr handle = IntPtr.Zero;
              StringBuilder Buff = new StringBuilder(nChars);
              handle = GetForegroundWindow();
  
              if (GetWindowText(handle, Buff, nChars) &gt; 0)
              {
                  String windowTitle = Buff.ToString();
  
                  return windowTitle;
              }
              return null;
          }
  
          public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
          {
              Rect move = new Rect();
  
              if (String.Equals(GetActiveWindowTitle(), &quot;Untitled - Notepad&quot;)) // example if notepad is opened
              {
                  if (eventType == EVENT_SYSTEM_MOVESIZESTART)
                  {
                      GetWindowRect(hwnd, ref move);
  
                      Console.WriteLine(&quot;Window Moved Start&quot;);
                  }
                  else if (eventType == EVENT_SYSTEM_MOVESIZEEND)
                  {
                      GetWindowRect(hwnd, ref move);
  
                      Console.WriteLine(&quot;Window Moved End&quot;);
                  }
  
                  System.Windows.Application.Current.MainWindow.WindowState = WindowState.Maximized;
              }
              else
              {
                  System.Windows.Application.Current.MainWindow.WindowState = WindowState.Minimized;
              }
          }
      }
      public struct Rect
      {
          public int Left { get; set; }
          public int Top { get; set; }
          public int Right { get; set; }
          public int Bottom { get; set; }
      }
  
  }
  
  </code></pre>