user2833581
user2833581

Reputation: 61

Event listener if active window moved or resized C#

I created an overlay for an application. I just need this 2 to be working:

  1. 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.
  2. A notification if the application window is moved or resized. I cant get it to worked.
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
{
    /// <summary>
    /// Interaction logic for HomePage.xaml
    /// </summary>
    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("user32.dll")]
        static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

        [DllImport("user32.dll")]
        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("Notepad")[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) > 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(), "Untitled - Notepad")) // example if notepad is opened
            {
                if (eventType == EVENT_SYSTEM_MOVESIZESTART)
                {
                    GetWindowRect(hwnd, ref move);

                    Console.WriteLine("Window Moved Start");
                }
                else if (eventType == EVENT_SYSTEM_MOVESIZEEND)
                {
                    GetWindowRect(hwnd, ref move);

                    Console.WriteLine("Window Moved End");
                }

                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; }
    }

}

Upvotes: 2

Views: 388

Answers (1)

Sharkbyteprojects
Sharkbyteprojects

Reputation: 11

Try this:

private void Window_SizeChanged(object sender, SizeChangedEventArgs e){/*YOUR CODE*/}

But if you using VisualStudio, it's easier to create a XAML Project, and register it.

Upvotes: 1

Related Questions