aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Keyboard/UIAutomationHelper.cs
blob: 5a4703d954814716643ba75ea31e593ea8f6f883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.SharedUI.Keyboard
{
    /// <summary>
    /// This class helps in UI Automation
    /// </summary>
    public static class UIAutomationHelper
    {
        public static IntPtr GetActiveWindow()
        {
            return Win32.GetForegroundWindow();
        }

        /// <summary>
        /// Find a window specified by the window title
        /// </summary>
        /// <param name="windowName"></param>
        /// <returns></returns>
        public static IntPtr FindWindow(string windowName)
        {
            return Win32.FindWindow(null, windowName);
        }

        /// <summary>
        /// Find a window specified by the class name as well as the window title
        /// </summary>
        /// <param name="className"></param>
        /// <param name="windowName"></param>
        /// <returns></returns>
        public static IntPtr FindWindow(string className, string windowName)
        {
            return Win32.FindWindow(className, windowName);
        }

        /// <summary>
        /// Finds a window specified by the window title and set focues on that window
        /// </summary>
        /// <param name="windowName"></param>
        /// <returns></returns>
        public static IntPtr FindWindowAndFocus(string windowName)
        {
            return UIAutomationHelper.FindWindowAndFocus(null, windowName);
        }

        /// <summary>
        /// Finds a window specified by the class name and window title and set focuses on that window
        /// </summary>
        /// <param name="className"></param>
        /// <param name="windowName"></param>
        /// <returns></returns>
        public static IntPtr FindWindowAndFocus(string className, string windowName)
        {
            IntPtr hWindow = Win32.FindWindow(className, windowName);
            Win32.SetForegroundWindow(hWindow);
            return hWindow;
        }

        /// <summary>
        /// Finds a child window
        /// </summary>
        /// <param name="windowName"></param>
        /// <param name="childWindowName"></param>
        /// <returns></returns>
        public static IntPtr FindChildWindow(String windowName, String childWindowName)
        {
            return UIAutomationHelper.FindChildWindow(null, windowName, null, childWindowName);
        }

        /// <summary>
        /// Finds a child window
        /// </summary>
        /// <param name="className"></param>
        /// <param name="windowName"></param>
        /// <param name="childClassName"></param>
        /// <param name="childWindowName"></param>
        /// <returns></returns>
        public static IntPtr FindChildWindow(String className, String windowName,
                                    String childClassName, String childWindowName)
        {
            IntPtr hWindow = Win32.FindWindow(className, windowName);
            IntPtr hWindowEx = Win32.FindWindowEx(hWindow,
                               IntPtr.Zero, childClassName, childWindowName);
            return hWindowEx;
        }

        /// <summary>
        /// Simulates pressing a key
        /// </summary>
        /// <param name="hWindow"></param>
        /// <param name="key"></param>
        public static void PressKey(IntPtr hWindow, System.Windows.Input.Key key)
        {
            Win32.PostMessage(hWindow, Win32.WM_KEYDOWN,
                              System.Windows.Input.KeyInterop.VirtualKeyFromKey(key),
                              0);
        }

        /// <summary>
        /// Simulates pressing several keys
        /// </summary>
        /// <param name="hWindow"></param>
        /// <param name="keys"></param>
        public static void PressKeys(IntPtr hWindow,
                           IEnumerable<System.Windows.Input.Key> keys)
        {
            foreach (var key in keys)
            {
                UIAutomationHelper.PressKey(hWindow, key);
            }
        }

    }
}