活动窗口移动或调整C#大小时的事件侦听器



我为一个应用程序创建了一个覆盖。我只需要这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; }
}
}

试试这个:

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

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

最新更新