Как я могу прочитать текст с этой метки, используя вызовы WinAPI? - PullRequest
0 голосов
/ 29 апреля 2020

Есть окно, из которого я хочу прочитать определенный текст (10.024 на скриншоте ниже), используя вызовы WinAPI.

Вот как выглядит это окно в Spy ++:

Чтобы прочитать значение из этой метки, я сначала определяю дескриптор окна:

private TargetWindowInfo FindScrivenerWindow()
{
    IntPtr targetWindowHandle = new IntPtr(0);
    String windowTitle = "";
    Process[] scrivenerProcesses = Process.GetProcessesByName("scrivener");
    if (scrivenerProcesses.Length < 1)
    {
        return null; // Scrivener is not started
    }
    foreach (var handle in EnumerateProcessWindowHandles(scrivenerProcesses.First().Id))
    {
        StringBuilder message = new StringBuilder(1000);
        SendMessage(handle, WM_GETTEXT, message.Capacity, message);
        String curWindowTitle = message.ToString();

        if (curWindowTitle.EndsWith("Project Targets"))
        {
            bool windowVisible = IsWindowVisible(handle);

            if (windowVisible)
            {
                windowTitle = curWindowTitle;
                targetWindowHandle = handle;

                var result = new TargetWindowInfo();

                result.ProjectName = windowTitle.Substring(0, windowTitle.IndexOf(" Project Targets"));
                result.Handle = handle;
                return result;
            }

        }
    }
    return null;
}


var targetWindowInfo = FindScrivenerWindow();

Затем я попытался применить этот ответ , чтобы прочитать все строки из окно и, надеюсь, найти нужный (10.024) среди них (targetWindowInfo.Handle - результат FindScrivenerWindow):

List<WinText> windows = new List<WinText>();

//find the "first" window
IntPtr hWnd = targetWindowInfo.Handle;

while (hWnd != IntPtr.Zero)
{
    //find the control window that has the text
    IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, null, null);

    //initialize the buffer.  using a StringBuilder here
    System.Text.StringBuilder sb = new System.Text.StringBuilder(255);  // or length from call with GETTEXTLENGTH

    //get the text from the child control
    int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb);

    windows.Add(new WinText() { hWnd = hWnd, Text = sb.ToString() });

    //find the next window
    hWnd = FindWindowEx(IntPtr.Zero, hWnd, "scrivener", null);
}

//do something clever
windows.OrderBy(x => x.Text).ToList().ForEach(y => Console.Write("{0} = {1}\n", y.hWnd, y.Text));

Console.Write("\n\nFound {0} window(s).", windows.Count);

windows всегда имеет один элемент, а текст является пустым строка.

Как я могу прочитать значение метки? Возможно ли это вообще при заданных свойствах окна?

Обновление 1: Проверка автоматизации пользовательского интерфейса выдает следующие значения для метки:

How found:  Selected from tree...
Name:   ""
BoundingRectangle:  {l:1437 t:455 r:1484 b:475}
IsEnabled:  true
IsOffscreen:    false
IsKeyboardFocusable:    false
HasKeyboardFocus:   false
AccessKey:  ""
ProcessId:  6488
ProviderDescription:    "[pid:6488,providerId:0x0 Main(parent link):Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]"
IsPassword: false
HelpText:   ""
IsDialog:   false
LegacyIAccessible.ChildId:  0
LegacyIAccessible.DefaultAction:    "SetFocus"
LegacyIAccessible.Description:  ""
LegacyIAccessible.Help: ""
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: ""
LegacyIAccessible.Role: Client (0xA)
LegacyIAccessible.State:    normal (0x0)
LegacyIAccessible.Value:    ""
IsAnnotationPatternAvailable:   false
IsDragPatternAvailable: false
IsDockPatternAvailable: false
IsDropTargetPatternAvailable:   false
IsExpandCollapsePatternAvailable:   false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable:   false
IsItemContainerPatternAvailable:    false
IsLegacyIAccessiblePatternAvailable:    true
IsMultipleViewPatternAvailable: false
IsObjectModelPatternAvailable:  false
IsRangeValuePatternAvailable:   false
IsScrollItemPatternAvailable:   false
IsScrollPatternAvailable:   false
IsSelectionItemPatternAvailable:    false
IsSelectionPatternAvailable:    false
IsSpreadsheetItemPatternAvailable:  false
IsSpreadsheetPatternAvailable:  false
IsStylesPatternAvailable:   false
IsSynchronizedInputPatternAvailable:    false
IsTableItemPatternAvailable:    false
IsTablePatternAvailable:    false
IsTextChildPatternAvailable:    false
IsTextEditPatternAvailable: false
IsTextPatternAvailable: false
IsTextPattern2Available:    false
IsTogglePatternAvailable:   false
IsTransformPatternAvailable:    false
IsTransform2PatternAvailable:   false
IsValuePatternAvailable:    false
IsVirtualizedItemPatternAvailable:  false
IsWindowPatternAvailable:   false
IsCustomNavigationPatternAvailable: false
IsSelectionPattern2Available:   false
FirstChild: [null]
LastChild:  [null]
Next:   "" 
Previous:   "" 
Other Props:    Object has no additional properties
Children:   Container has no children
Ancestors:  "short-story-4 Project Targets" Fenster
    "short-story-4 - Scrivener" Fenster
    "Desktop 1" Bereich
    [ No Parent ]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...