Created
December 13, 2022 22:41
-
-
Save vjdw/fec610113925ff83ba19e5aeea308405 to your computer and use it in GitHub Desktop.
C# / .NET / WPF / code to attach to WindowOpenedEvent and maximize new windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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