简体   繁体   English

如何在顶部活动窗口 C# WPF 中获取子内容的可访问性信息

[英]How to get accessibility info of child contents in top active window C# WPF

I am able to get top active window (title) in my code but I wanted to know how to get all the children(content and controls or elements -image, link, button etc) inside the active top window in my code.我能够在我的代码中获得顶部活动窗口(标题),但我想知道如何在我的代码的活动顶部窗口中获取所有子项(内容和控件或元素 - 图像、链接、按钮等)。 I searched many links such as - Retrieve all the controls of any window with their types and value which suggests- To enumerate top level windows use EnumWindows(), to get their child windows use EnumChildWindows().我搜索了许多链接,例如 - 检索任何窗口的所有控件及其类型和值,这表明 - 枚举顶级窗口使用 EnumWindows(),让他们的子窗口使用 EnumChildWindows()。 But I am not able to understand how to implement it in my code and get the children of top active windows which is focused.但是我无法理解如何在我的代码中实现它并获得焦点的顶级活动窗口的子级。

It would be great if someone helped me around with some sample code.如果有人帮助我提供一些示例代码,那就太好了。 Stuck from long time exploring and checking different links.由于长时间探索和检查不同的链接而陷入困境。

Below is the code which gives me top active window title and displaying in the grid.下面是给我顶部活动窗口标题并在网格中显示的代码。

xaml code - xml代码 -

 <Grid x:Name="grid">
</Grid>

.cs Class code - .cs 类代码 -

 public partial class MainWindow : Window
    {

     [DllImport("user32.dll")]
            static extern IntPtr GetForegroundWindow();
            private static IntPtr GetActiveWindow()
            {
                IntPtr handle = IntPtr.Zero;
                return GetForegroundWindow();
            }
            [DllImport("user32.dll", EntryPoint = "GetWindowTextW", CharSet = CharSet.Unicode)]
            private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);
            ListBox lst = new ListBox();
            public MainWindow()
            {
                InitializeComponent();
    
                DispatcherTimer dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
                dispatcherTimer.Start();
                var d = ActiveWindowTitle();
                var t = GetForegroundWindow();
                grid.Children.Add(lst);
            }
            public static IntPtr GetTopWindow(string wClass, string wCaption)
            {
                if (string.IsNullOrEmpty(string.Concat(wClass, wCaption)))
                {
                    return (IntPtr)0;
                }
                return (IntPtr)0;
            }
            private void DispatcherTimer_Tick(object sender, EventArgs e)
            {
                string title = ActiveWindowTitle();
                var current = this.Title;
                if (!current.Equals(title) && !String.IsNullOrEmpty(title))
                    lst.Items.Add("Title: " + title );
            }
    
            private string ActiveWindowTitle()
            {
                const int nChar = 256;
                StringBuilder ss = new StringBuilder(nChar);
                IntPtr handle = IntPtr.Zero;
                handle = GetForegroundWindow();
    
                if (GetWindowText(handle, ss, nChar) > 0)
                    return ss.ToString();
                else return "";
    
            }
}

After log research I am able to achieve it but still facing problems while drawing overlay window.经过日志研究,我能够实现它,但在绘制覆盖窗口时仍然面临问题。

I am able get handle of top active window and I am also able to draw an overlay window on top of each control .我能够获得顶部活动窗口的句柄,并且我还能够在每个控件的顶部绘制一个覆盖窗口。

Below is my code I used to create an overlay window on active window children.下面是我用来在活动窗口子级上创建覆盖窗口的代码。 I am using FlaUI automation for getting all Children of active top window.我正在使用 FlaUI 自动化来获取活动顶部窗口的所有子项。 To get all children of active window I am attaching processId of active window to get children and storing all Hyperlink children in a list and passing same list to show overlay bubble window.要获取活动窗口的所有子项,我将附加活动窗口的 processId 以获取子项并将所有超链接子项存储在列表中并传递相同的列表以显示覆盖气泡窗口。

Code -代码 -

 var processId = GetFileNameFormHandle();
                GetAllChildrenOfTopWindow(processId);
    [DllImport("user32.dll")]
            static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
    
            public UInt32 GetFileNameFormHandle()
            {
                var handle = GetForegroundWindow();
                uint pid = 0;
                GetWindowThreadProcessId(handle, out pid);
                string wmiQuery = string.Format("SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId LIKE '{0}'", pid.ToString());
                var pro = new ManagementObjectSearcher(wmiQuery).Get().Cast<ManagementObject>().FirstOrDefault();
                UInt32 processid = (UInt32)pro["ProcessId"];
                return processid;
            }
     

  

      public static List<FlaUI.Core.AutomationElements.AutomationElement> ChildLinksList = new List<FlaUI.Core.AutomationElements.AutomationElement>();
         public void GetAllChildrenOfTopWindow(UInt32 topActiveWindowName)
                {
                    var app = FlaUI.Core.Application.Attach((int)topActiveWindowName);
                    var automation = new UIA3Automation();
                    var mainwindow = app.GetMainWindow(automation,TimeSpan.MinValue);
             

       ConditionFactory cf = new ConditionFactory(new UIA3PropertyLibrary());
                var Elements = mainwindow.FindAllDescendants();
     foreach (var child in Elements)
                    {
                        if (child.ControlType == FlaUI.Core.Definitions.ControlType.Hyperlink)
                            ChildLinksList.Add(child);
    }
     childBubbleWindow(ChildLinksList);
}

To show Bubble/OverlayWindow -显示 Bubble/OverlayWindow -

 public static void childBubbleWindow(List<FlaUI.Core.AutomationElements.AutomationElement> childrenList)
        {
            int i = 0;

            foreach (var links in childrenList)
            {
                if (links.IsAvailable == true && links.IsOffscreen != true)
                {
                    var linkbounding = links.Properties.BoundingRectangle.Value;
                    var handle = GetForegroundWindow();
var hwndSource = new HwndSource(new HwndSourceParameters("OverlayWindow")
                    {
                        ParentWindow = handle,
                        UsesPerPixelTransparency = true,
                        PositionX = linkbounding.X,
                        PositionY = linkbounding.Y,

                    })
                    {
                        RootVisual = new System.Windows.Controls.TextBox()
                        {
                            Width = 30,
                            Height = 30,
                            BorderThickness = new Thickness(3),
                            BorderBrush = new SolidColorBrush(Colors.Red),
                            TextAlignment = TextAlignment.Right,
                            FontWeight = FontWeights.Bold, 
                            Background = new SolidColorBrush(Colors.DodgerBlue),
                            Text = i.ToString(),
                            IsHitTestVisible = true,
                        }
                    };
                    i++;
                }
            }  
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM