/// <summary> /// 初始化实例 /// </summary> /// <param name="className">查找的目标窗口(子项所在的主窗口)类名</param> /// <param name="windowTitle">查找的目标窗口(子项所在的主窗口)标题</param> /// <param name="destinationAccName">查找的目标子项的AccName的值</param> public MSAATextReader(string className, string windowTitle, string destinationAccName) { //获取指定窗口句柄 IntPtr hwnd = Win32.FindWindowEx(IntPtr.Zero, IntPtr.Zero, className, windowTitle); Guid guidCOM = new Guid(0x618736E0, 0x3C3D, 0x11CF, 0x81, 0xC, 0x0, 0xAA, 0x0, 0x38, 0x9B, 0x71); Accessibility.IAccessible IACurrent = null; /* * 通过指定的窗口句柄,获取IAccessible接口指针 * 调用AccessibleObjectFromWindow * 获得句柄hwndCurrent指向的鼠标当前窗口所在的顶级窗口,放入IACurrent,供访问者使用 */ Win32.AccessibleObjectFromWindow(hwnd, (int)Win32.OBJID_CLIENT, ref guidCOM, ref IACurrent); if (IACurrent == null) { throw new NullReferenceException(string.Format("找不到指定窗口名为\"{0}\"的窗口", windowTitle)); } //当前句柄窗口包含的直接下级子项个数 int childCount = IACurrent.accChildCount; //所有子窗口集合 object[] windowChildren = new object[childCount]; /* * 获得当前顶级窗口(特别注意不一定是hwndCurrent所指向的窗口)的第一层子项 */ Win32.AccessibleChildren(IACurrent, 0, childCount, windowChildren, out _pcObtained); //获取消息窗口对应的IAccessible类型引用 msgContentWindow = GetMessageContentWindow(windowChildren, destinationAccName); }
static void getAccessibleObjectFromWindow(IntPtr hwnd, ref Accessibility.IAccessible acc) { Guid guid = new Guid("{618736e0-3c3d-11cf-810c-00aa00389b71}"); // IAccessibleobject obj = null; object tmpObj = null; int returnVal = AccessibleObjectFromWindow(hwnd, (uint)0x00000000, ref guid, ref tmpObj); acc = (Accessibility.IAccessible)tmpObj; return; }
private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox) { Guid guidCOM = new Guid(0x618736E0, 0x3C3D, 0x11CF, 0x81, 0xC, 0x0, 0xAA, 0x0, 0x38, 0x9B, 0x71); Accessibility.IAccessible IACurrent = null; Win32.AccessibleObjectFromWindow(imWindowHwnd, (int)Win32.OBJID_CLIENT, ref guidCOM, ref IACurrent); IACurrent = (IAccessible)IACurrent.accParent; inputBox = null; inputBox = GetMsgChild(IACurrent); }
private void StartGetAccessibleObjects(System.IntPtr imWindowHwnd, out object FindBox, out IAccessible FindBoxParent, string SearchTile, out XmlDocument doc) { Guid guidCOM = new Guid(0x618736E0, 0x3C3D, 0x11CF, 0x81, 0xC, 0x0, 0xAA, 0x0, 0x38, 0x9B, 0x71); Accessibility.IAccessible IACurrent = null; doc = new XmlDocument(); FindBox = null; FindBoxParent = IACurrent; Win32.AccessibleObjectFromWindow(imWindowHwnd, (int)Win32.OBJID_CLIENT, ref guidCOM, ref IACurrent); if (IACurrent == null) { return; } IACurrent = (IAccessible)IACurrent.accParent; FindBox = null; object[] CHILDS = GetAccessibleChildren(IACurrent); doc.LoadXml("<top></top>"); Int32 index = 1; foreach (var CHILDITEM in CHILDS) { XmlElement subwin = doc.CreateElement("win"); doc.DocumentElement.AppendChild(subwin); subwin.SetAttribute("index", index.ToString()); if (CHILDITEM.GetType().ToString().ToUpper().Contains("INT")) { subwin.SetAttribute("name", ((IAccessible)IACurrent).get_accName(CHILDITEM)); subwin.SetAttribute("value", ((IAccessible)IACurrent).get_accValue(CHILDITEM), SearchTile); if (((IAccessible)IACurrent).get_accName(CHILDITEM) == SearchTile) { FindBox = (CHILDITEM); FindBoxParent = IACurrent; return; } } else { subwin.SetAttribute("name", ((IAccessible)CHILDITEM).get_accName(Win32.CHILDID_SELF)); subwin.SetAttribute("value", ((IAccessible)CHILDITEM).get_accValue(Win32.CHILDID_SELF), SearchTile); if (((IAccessible)CHILDITEM).get_accName(Win32.CHILDID_SELF) == SearchTile) { FindBox = ((IAccessible)CHILDITEM); FindBoxParent = IACurrent; return; } } index += 1; if (FindBox == null) { RepeatGetAccessibleObjects(((IAccessible)CHILDITEM), 1, subwin, SearchTile, doc, ref FindBox, ref FindBoxParent); } } //int childCount = IACurrent.accChildCount; //object[] windowChildren = new object[childCount]; //int pcObtained; //Win32.AccessibleChildren(IACurrent, 0, childCount, windowChildren, out pcObtained); //foreach (IAccessible child in windowChildren) //{ // if (child.get_accName(Win32.CHILDID_SELF) == _winTitle) // { // inputBox = GetAccessibleChild(child, new int[] { 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 0 }); // break; // } //} }
public static extern int AccessibleChildren( Accessibility.IAccessible paccContainer, int iChildStart, int cChildren, [Out] object[] rgvarChildren, out int pcObtained);
internal static extern int AccessibleChildren(Accessibility.IAccessible paccContainer, int iChildStart, int cChildren, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), In, Out] object[] rgvarChildren, out int pcObtained);
static int findIexplrTab(System.Diagnostics.Process iexplrProc, string findTxt, bool activateTab) { // [ https://stackoverflow.com/questions/3820228/setting-focus-to-already-opened-tab-of-internet-explorer-from-c-sharp-program-us ] IntPtr hWndDirectUI = GetDirectUIHWND(iexplrProc.MainWindowHandle); if (hWndDirectUI == IntPtr.Zero) { return(int.MinValue); } // [ https://msdn.microsoft.com/en-us/library/system.windows.forms.accessibleobject(v=vs.110).aspx ] // [ https://www.codeproject.com/Articles/38906/UI-Automation-Using-Microsoft-Active-Accessibility ] Accessibility.IAccessible objAccessible = null; getAccessibleObjectFromWindow(hWndDirectUI, ref objAccessible); if (objAccessible == null) { Console.WriteLine("ERROR: getAccessibleObjectFromWindow()"); return(int.MinValue); } bool foundTab = false; int tabIndex = 0; foreach (IAccessible accessor in getAccessibleChildren(objAccessible)) { foreach (IAccessible accChild in getAccessibleChildren(accessor)) { IAccessible[] accTabs = getAccessibleChildren(accChild); for (int i = 0; i < accTabs.Length - 1; i++) { tabIndex++; IAccessible accTab = accTabs[i]; if (findTxt.Length == 0) { if ((int)accTab.get_accState(0) == 0x200002) // 2097154 { printIexplrTabInfo(iexplrProc, accTab, tabIndex, i); Console.WriteLine("[activeTab:{0}]", tabIndex); return(tabIndex); } } else if (findTxt.Length > 0 && accTab.accDescription[0].Contains(findTxt)) { foundTab = true; printIexplrTabInfo(iexplrProc, accTab, tabIndex, i); Console.WriteLine("[foundTab:{0}]", tabIndex); if (activateTab && (int)accTab.get_accState(0) != 0x200002) { accTab.accDoDefaultAction(0); // 0==CHILDID_SELF ==> !!activate this tab!! System.Threading.Thread.Sleep(200); if ((int)accTab.get_accState(0) == 0x200002) { Console.WriteLine("[activateTab:success]"); } else { Console.WriteLine("[activateTab:failed]"); } } return(tabIndex); } } // accTabs } // accChild } // accessor return(-tabIndex); }