【问题标题】:Event listener if active window moved or resized C#如果活动窗口移动或调整大小 C# 时的事件侦听器
【发布时间】:2021-03-10 19:14:09
【问题描述】:

我为一个应用程序创建了一个叠加层。我只需要这 2 个工作:

  1. 如果打开了其他窗口,则覆盖将被最小化,如果它是活动窗口则最大化。我相信我得到了这个工作。在下面的代码中,如果打开记事本,覆盖将最小化。
  2. 应用程序窗口移动或调整大小时的通知。我无法让它工作。
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; }
    }

}

【问题讨论】:

    标签: c# wpf winapi user32


    【解决方案1】:

    试试这个:

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

    但如果您使用 VisualStudio,创建 XAML 项目并注册它会更容易。

    【讨论】:

    • 但这是针对windows窗体本身的吗?我正在尝试连接到另一个活动的应用程序窗口并检测事件。
    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多