本文介绍了IE7,IE8 Tab交互 - 辅助功能类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 这是IE7 + IE8标签的C#类(使用辅助功能) - 没有ShellWindows 使用XP和IE7 + IE8进行测试。 用法:添加对辅助功能的引用 IEAccessible tabs = new IEAccessible(); var active = tabs.GetActiveTabIndex(yourIEMainHWND); 可用方法: GetActiveTabIndex GetActiveTabCaption 激活标签按索引或标题 GetTabCount GetTabCaptions 使用系统; 使用 System.Collections.Generic; 使用辅助功能; 使用 System.Runtime.InteropServices; 使用 System.Diagnostics; 命名空间 IEAccessibleTabs { public class IEAccessible { private enum OBJID : uint { OBJID_WINDOW = 0x00000000, } private const int IE_ACTIVE_TAB = 2097154; private const int CHILDID_SELF = 0; 私人 IAccessible 可访问; private IEAccessible []儿童 { get { int num = 0; object [] res = GetAccessibleChildren(可访问, out num); if (res == null ) return new IEAccessible [0]; 列表< IEAccessib乐> list = new 列表< IEAccessible >(res.Length); foreach (对象 obj in res) { IAccessible acc = obj as IAccessible ; if (acc! = null ) list.Add( new IEAccessible (acc)); } return list.ToArray(); } } private string 名称 { get { string ret = accessible.get_accName(CHILDID_SELF); 返回 ret; } } private int ChildCount { 获取 { int ret = accessible .accChildCount; return ret; } } public IEAccessible() { } public IEAccessible( IntPtr ieHandle, string tabCaptionToActivate) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref access); if (accessible == null ) throw new 例外(); var ieDirectUIHWND = new IEAccessible (ieHandle); foreach ( IEAccessible accessor in ieDirectUIHWND.Children) { foreach ( var 子 accessor.Children) { foreach ( var tab in child.Children) { if (tab.Name == tabCaptionToActivate) { tab.Activate(); return ; } } } } } public IEAccessible( IntPtr ieHandle, int tabIndexToActivate) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref access); if (accessible == null ) throw new 异常(); var index = 0; var ieDirectUIHWND = new IEAccessible (ieHandle); foreach ( IEAccessible accessor in ieDirectUIHWND.Children) { foreach ( var 子 accessor.Children) { foreach ( var tab in child.Children) { if (tabIndexToActivate> = child.ChildCount -1) return ; if (index == tabIndexToActivate) { tab.Activate(); return ; } index ++; } } } } private IEAccessible( IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref access); if (accessible == null ) throw new 异常(); } public string GetActiveTabUrl( IntPtr ieHandle ) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref 可访问); if (accessible == null ) throw new 例外( ); var ieDirectUIHWND = 新 IEAccessible (ieHandle); foreach ( IEAccessible accessor in ieDirectUIHWND.Children) { foreach ( var child in accessor.Children) { foreach ( var 标签 child.Children) { object tabIndex = tab.accessible.get_accState(CHILDID_SELF); if (( int )tabIndex == IE_ACTIVE_TAB) { var description = tab.accessible.get_accDescription (CHILDID_SELF); if (! string .IsNullOrEmpty(description)) { if (description.Contains(环境 .NewLine)) { var url = description.Substring(description.IndexOf( Environment .NewLine))。Trim(); return url; } } } } } } return String .Empty; } public int GetActiveTabIndex( IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref 可访问); if (accessible == null ) throw new 异常(); var index = 0; var ieDirectUIHWND = new IEAccessible (ieHandle); foreach ( IEAccessible accessor in ieDirectUIHWND.Children) { foreach ( var child accessor.Children) { foreach ( var 标签 in child 。儿童ren) { object tabIndex = tab.accessible.get_accState(0); if (( int )tabIndex == IE_ACTIVE_TAB) return index; index ++; } } } 返回 -1; } public string GetActiveTabCaption( IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref access); if (accessible == null ) throw new 异常(); var ieDirectUIHWND = new IEAccessible (ieHandle); foreach ( IEAccessible 访问者 ieDirectUIHWND.Children) { foreach ( var child in accessor.Children) { foreach ( var 标签 in child.Children) { object tabIndex = tab.accessible.get_accState(0); if (( int )tabIndex == IE_ACTIVE_TAB) return tab.Name; } } } return String .Empty; } public List < string > GetTabCaptions( IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID .OBJID_WINDOW, ref 可访问) ; if (accessible == null ) throw new 异常(); var ieDirectUIHWND = new IEAccessible (ieHandle); VAR captionList = new 列表< string >(); foreach ( IEAccessible accessor in ieDirectUIHWND.Children) { foreach ( var child in accessor.Children) foreach ( var 标签 child.Children) captionList.Add(tab.Name); } 如果(captionList.Count> 0) captionList.RemoveAt(captionList.Count - 1); return captionList; } public int GetTabCount( IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID 。 OBJID_WINDOW, ref access); if (accessible == null ) 掷 new 例外(); var ieDirectUIHWND = new IEAccessible (ieHandle); foreach ( IEAccessible accessor in ieDirectUIHWND.Children) { foreach ( var 子 accessor.Children) { foreach ( var 标签 in child.Children) return child.ChildCount - 1; } } return 0; } private IntPtr GetDirectUIHWND( IntPtr ieFrame) { var 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 ); return directUI; } 私人 IEAccessible( IAccessible acc) { if (acc == null ) throw new 例外(); accessible = acc; } 私人 void 激活() { accessible.accDoDefaultAction(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; } $b $b #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 } } 解决方案 Hi cablehead,I hope your still watching this thread. I have been able to implement this code in my app and I can see that it will work with IE8 aswell. I haven’t tested it with Vista but as it dosen’t use shellexecute so it should work. I need a method to close a tab with a given tabcaption. I have been trying to sendmessage with WM_CLOSE, WM_DESTROY, WM_QUIT, QM_NOTIFYPARENT etc with no joy. I tried to close TabWindowClass (which is now "Frame Tab" in IE8) which closes the DOC but still leaves the DirectUIHWND visable. closing the DriectUIHWND kills all tabs but leaves the DOC’s visable.Do you know a way of closing a given tab. Better yet is there a way to have a method return a WebBrowser2 interface or a IHTMLDocument2 or even a HDocVw.InternetExplorer given a IEMainHWND via the Accessibility class.Thanks,TAHAICP.S. I am a little lost in how the Accessibility class is working - perhaps you could repost the code with some comments. Here's a C# Class for IE7 + IE8 Tabs (using Accessibility) - no ShellWindowsTested with XP and IE7 + IE8.Usage: Add a reference to AccessibilityIEAccessible tabs = new IEAccessible();var active = tabs.GetActiveTabIndex(yourIEMainHWND);Methods Available:GetActiveTabIndexGetActiveTabCaptionActivate Tab by Index or CaptionGetTabCountGetTabCaptionsusing 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 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 IEAccessible() { } public IEAccessible(IntPtr ieHandle, string tabCaptionToActivate) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var ieDirectUIHWND = new IEAccessible(ieHandle); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) { foreach (var tab in child.Children) { if (tab.Name == tabCaptionToActivate) { tab.Activate(); return; } } } } } public IEAccessible(IntPtr ieHandle, int tabIndexToActivate) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var index = 0; var ieDirectUIHWND = new IEAccessible(ieHandle); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) { foreach (var tab in child.Children) { if (tabIndexToActivate >= child.ChildCount -1) return; if (index == tabIndexToActivate) { tab.Activate(); return; } index++; } } } } private IEAccessible(IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); } public string GetActiveTabUrl(IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var ieDirectUIHWND = new IEAccessible(ieHandle); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) { foreach (var tab in child.Children) { object tabIndex = tab.accessible.get_accState(CHILDID_SELF); if ((int)tabIndex == IE_ACTIVE_TAB) { var description = tab.accessible.get_accDescription(CHILDID_SELF); if (!string.IsNullOrEmpty(description)) { if (description.Contains(Environment.NewLine)) { var url = description.Substring(description.IndexOf(Environment.NewLine)).Trim(); return url; } } } } } } return String.Empty; } public int GetActiveTabIndex(IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var index = 0; var ieDirectUIHWND = new IEAccessible(ieHandle); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) { foreach (var tab in child.Children) { object tabIndex = tab.accessible.get_accState(0); if ((int)tabIndex == IE_ACTIVE_TAB) return index; index++; } } } return -1; } public string GetActiveTabCaption(IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var ieDirectUIHWND = new IEAccessible(ieHandle); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) { foreach (var tab in child.Children) { object tabIndex = tab.accessible.get_accState(0); if ((int)tabIndex == IE_ACTIVE_TAB) return tab.Name; } } } return String.Empty; } public List<string> GetTabCaptions(IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var ieDirectUIHWND = new IEAccessible(ieHandle); var captionList = new List<string>(); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) foreach (var tab in child.Children) captionList.Add(tab.Name); } if (captionList.Count > 0) captionList.RemoveAt(captionList.Count - 1); return captionList; } public int GetTabCount(IntPtr ieHandle) { AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible); if (accessible == null) throw new Exception(); var ieDirectUIHWND = new IEAccessible(ieHandle); foreach (IEAccessible accessor in ieDirectUIHWND.Children) { foreach (var child in accessor.Children) { foreach (var tab in child.Children) return child.ChildCount - 1; } } return 0; } private IntPtr GetDirectUIHWND(IntPtr ieFrame) { var 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); return directUI; } private IEAccessible(IAccessible acc) { if (acc == null) throw new Exception(); accessible = acc; } private void Activate() { accessible.accDoDefaultAction(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 } } 解决方案 Hi cablehead,I hope your still watching this thread. I have been able to implement this code in my app and I can see that it will work with IE8 aswell. I haven't tested it with Vista but as it dosen't use shellexecute so it should work. I need a method to close a tab with a given tabcaption. I have been trying to sendmessage with WM_CLOSE, WM_DESTROY, WM_QUIT, QM_NOTIFYPARENT etc with no joy. I tried to close TabWindowClass (which is now "Frame Tab" in IE8) which closes the DOC but still leaves the DirectUIHWND visable. closing the DriectUIHWND kills all tabs but leaves the DOC's visable.Do you know a way of closing a given tab.Better yet is there a way to have a method return a WebBrowser2 interface or a IHTMLDocument2 or even a HDocVw.InternetExplorer given a IEMainHWND via the Accessibility class.Thanks,TAHAICP.S. I am a little lost in how the Accessibility class is working - perhaps you could repost the code with some comments. 这篇关于IE7,IE8 Tab交互 - 辅助功能类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 17:57