You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何获取第三方UWP应用的表格内容?能否使用WinAPI实现?

Hey, great question! You absolutely can extract that table content from your UWP app—whether you need a quick manual workaround or a robust automated solution for your own app. Let’s break down both approaches:

临时手动方案

If you just need to grab the data once or occasionally, these built-in Windows tools will do the trick without writing any code:

  • Use the Inspect Tool: This comes with the Windows SDK (install it via Windows Developer Tools). Launch Inspect, target your UWP app’s window, then navigate the UI tree to find your table control (look for elements labeled DataGrid or Table). Right-click the table element and select Copy all text to dump all cell content to your clipboard, or drill into individual rows/cells to copy specific data.
  • Try Ctrl+C: Many UWP tables let you select rows or the entire table with your mouse, then press Ctrl+C to copy the content directly to Excel or a text file. Even if the app doesn’t have an "Export" button, basic text copy often works thanks to accessibility standards.
理想自动化方案(自研应用)

For a permanent solution in your own app, the UI Automation API (a part of WinAPI) is the way to go—it’s designed specifically to access UI elements across all Windows app types, including sandboxed UWP apps. Here’s how to implement it:

Key Concepts

UWP apps expose their UI structure via accessibility trees, and UI Automation lets you traverse this tree to locate controls, read their properties, and extract content. Unlike raw WinAPI calls like WM_GETTEXT, UI Automation works reliably with UWP’s sandboxed environment.

Example Code (C#)

This snippet shows how to locate a UWP table, iterate through its rows and cells, and extract text:

using System;
using System.Windows.Automation;
using System.Runtime.InteropServices;

class UWPTableExtractor
{
    static void Main(string[] args)
    {
        // Replace with your UWP app's window title
        string targetWindowTitle = "Your UWP App's Window Title";
        IntPtr windowHandle = FindWindow(null, targetWindowTitle);

        if (windowHandle == IntPtr.Zero)
        {
            Console.WriteLine("Couldn't find the target window.");
            return;
        }

        // Get the root automation element for the window
        AutomationElement windowElement = AutomationElement.FromHandle(windowHandle);
        if (windowElement == null) return;

        // Locate the table control (adjust the condition if needed)
        Condition tableCondition = new PropertyCondition(
            AutomationElement.ControlTypeProperty, 
            ControlType.Table);
        AutomationElement tableElement = windowElement.FindFirst(
            TreeScope.Descendants, 
            tableCondition);

        if (tableElement == null)
        {
            Console.WriteLine("No table found in the window.");
            return;
        }

        // Iterate through rows
        Condition rowCondition = new PropertyCondition(
            AutomationElement.ControlTypeProperty, 
            ControlType.DataItem);
        AutomationElementCollection rows = tableElement.FindAll(
            TreeScope.Children, 
            rowCondition);

        foreach (AutomationElement row in rows)
        {
            // Iterate through cells in the row
            Condition cellCondition = new PropertyCondition(
                AutomationElement.ControlTypeProperty, 
                ControlType.Text);
            AutomationElementCollection cells = row.FindAll(
                TreeScope.Children, 
                cellCondition);

            foreach (AutomationElement cell in cells)
            {
                // Extract cell text using TextPattern
                if (cell.TryGetCurrentPattern(TextPattern.Pattern, out object patternObj))
                {
                    TextPattern textPattern = (TextPattern)patternObj;
                    Console.Write($"{textPattern.DocumentRange.GetText(-1)}\t");
                }
            }
            Console.WriteLine();
        }
    }

    // Import WinAPI to find the window handle
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}

Notes for Implementation

  • UI Tree Navigation: UWP apps may have nested containers (like Frame or Grid), so you might need to adjust the TreeScope or add additional property conditions (like element name) to pinpoint the exact table.
  • Accessibility Permissions: Ensure your app has the necessary permissions to access UI elements—on Windows 10/11, this usually just requires enabling UI Automation access (no special admin rights needed for most apps).
  • Alternative Controls: If the table uses a custom control instead of a standard DataGrid, you might need to look for ControlType.List or check the element’s Name/AutomationId properties to identify it.

内容的提问来源于stack exchange,提问作者jungle_mole

火山引擎 最新活动