Last active
January 28, 2023 16:09
-
-
Save smourier/fffb122dfdf887df4534015828dbc069 to your computer and use it in GitHub Desktop.
Open Taskbar's Calendar using UI Automation
This file contains hidden or 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.Runtime.InteropServices; | |
using UIAutomationClient; // add a COM reference to Windows\System32\UIAutomationCore.dll, and set "Embed Interop Types" to false on this reference's properties | |
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
private static readonly CUIAutomation8 _ui = new CUIAutomation8(); | |
static void Main() | |
{ | |
var handler = new AutomationEventHandler(); | |
var root = _ui.GetRootElement(); | |
_ui.AddAutomationEventHandler(UIA_EventIds.UIA_Window_WindowOpenedEventId, root, TreeScope.TreeScope_Children, null, handler); | |
var taskbar = root.FindFirst(TreeScope.TreeScope_Children, _ui.CreatePropertyCondition(UIA_PropertyIds.UIA_ClassNamePropertyId, "Shell_TrayWnd")); | |
// or var taskbar = root.FindFirst(TreeScope.TreeScope_Children, ui.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Taskbar")); | |
var clockButton = taskbar.FindFirst(TreeScope.TreeScope_Subtree, _ui.CreatePropertyCondition(UIA_PropertyIds.UIA_ClassNamePropertyId, "TrayClockWClass")); | |
var invoke = (IUIAutomationInvokePattern)clockButton.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId); | |
invoke.Invoke(); | |
Console.ReadLine(); | |
} | |
private class AutomationEventHandler : IUIAutomationEventHandler | |
{ | |
public void HandleAutomationEvent(IUIAutomationElement sender, int eventId) | |
{ | |
// TODO: add some checks on the sender | |
var handle = sender.CurrentNativeWindowHandle; | |
var rc = sender.CurrentBoundingRectangle; | |
MoveWindow(handle, 100, 100, rc.right - rc.left, rc.bottom - rc.top, false); | |
} | |
[DllImport("user32")] | |
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment