forked from bstromquist/fIEsta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-IETab.ps1
201 lines (174 loc) · 6.43 KB
/
Get-IETab.ps1
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#Requires -Version 2
Function Select-IETab
{
Param (
[object]$ie,
[Parameter(Mandatory = $False)][string]$name
)
# C# code
$code = @'
using System;
using System.Collections.Generic;
using Accessibility;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace IEAccessibleTabs
{
public class IEAccessible
{
private enum OBJID : uint
{
OBJID_WINDOW = 0x00000000,
}
private const int IE_ACTIVE_TAB = 2097154;
private const int CHILDID_SELF = 0;
private const int SELFLAG_TAKEFOCUS = 1;
private IAccessible accessible;
private IEAccessible[] Children
{
get
{
int num = 0;
object[] res = GetAccessibleChildren(accessible, out num);
if (res == null)
return new IEAccessible[0];
List<IEAccessible> list = new List<IEAccessible>(res.Length);
foreach (object obj in res)
{
IAccessible acc = obj as IAccessible;
if (acc != null)
list.Add(new IEAccessible(acc));
}
return list.ToArray();
}
}
private string Name
{
get
{
string ret = accessible.get_accName(CHILDID_SELF);
return ret;
}
}
private int ChildCount
{
get
{
int ret = accessible.accChildCount;
return ret;
}
}
public bool Activate(IntPtr ieHandle, string tabCaptionToActivate)
{
AccessibleObjectFromWindow( GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);
if (accessible == null)
throw new Exception();
IEAccessible ieDirectUIHWND = new IEAccessible( accessible);
foreach (IEAccessible accessor in ieDirectUIHWND.Children)
{
foreach (IEAccessible child in accessor.Children)
{
foreach (IEAccessible tab in child.Children)
{
if (tab.Name == tabCaptionToActivate)
{
tab.Activate();
//tab.Select();
return true;
}
}
}
}
return false;
}
private IntPtr GetDirectUIHWND(IntPtr ieFrame)
{
// For IE 8:
IntPtr directUI = FindWindowEx(ieFrame, IntPtr.Zero, "CommandBarClass", null);
directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
if (directUI == IntPtr.Zero)
{
// For IE 9:
directUI = FindWindowEx(ieFrame, IntPtr.Zero, "WorkerW", "Navigation Bar");
directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
}
return directUI;
}
public IEAccessible()
{
}
private IEAccessible(IAccessible acc)
{
if (acc == null)
throw new Exception();
accessible = acc;
}
private void Activate()
{
accessible.accDoDefaultAction( CHILDID_SELF);
}
private void Select()
{
accessible.accSelect( SELFLAG_TAKEFOCUS, CHILDID_SELF);
}
private static object[] GetAccessibleChildren( IAccessible ao, out int childs)
{
childs = 0;
object[] ret = null;
int count = ao.accChildCount;
if (count > 0)
{
ret = new object[count];
AccessibleChildren(ao, 0, count, ret, out childs);
}
return ret;
}
#region Interop
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,
string lpszWindow);
private static int AccessibleObjectFromWindow(IntPtr hwnd, OBJID idObject, ref IAccessible acc)
{
Guid guid = new Guid("{618736e0-3c3d-11cf-810c-00aa00389b71}"); // IAccessible
object obj = null;
int num = AccessibleObjectFromWindow(hwnd, (uint)idObject, ref guid, ref obj);
acc = (IAccessible)obj;
return num;
}
[DllImport("oleacc.dll")]
private static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint id, ref Guid iid, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);
[DllImport("oleacc.dll")]
private static extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] object[] rgvarChildren, out int pcObtained);
#endregion
}
}
'@
#User Add-Type to import the code
add-type $code -ReferencedAssemblies 'Accessibility'
#Create the object for the Function
$obj = New-Object IEAccessibleTabs.IEAccessible
if( $name)
{
$obj.Activate( $ie.hwnd, $name)
}
}