Skip to content

Instantly share code, notes, and snippets.

@vjdw
Created December 13, 2022 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjdw/fec610113925ff83ba19e5aeea308405 to your computer and use it in GitHub Desktop.
Save vjdw/fec610113925ff83ba19e5aeea308405 to your computer and use it in GitHub Desktop.
C# / .NET / WPF / code to attach to WindowOpenedEvent and maximize new windows
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Automation;
namespace AutoMaximize
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children ,
(sender, e) =>
{
var element = sender as AutomationElement;
if (element != null)
{
Trace.WriteLine($"{element.Current.Name} ({element.Current.LocalizedControlType})");
try
{
if (new[] { "window", "pane" }.Contains(element.Current.LocalizedControlType))
{
System.Threading.Thread.Sleep(1000);
var windowPattern = element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
if (windowPattern != null && windowPattern.Current.CanMaximize && !windowPattern.Current.IsModal)
{
windowPattern.SetWindowVisualState(WindowVisualState.Maximized);
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
});
InitializeComponent();
//Automation.RemoveAllEventHandlers();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment