How can I extract text from each item in listbox control in windows application using Win API?

108 views Asked by At

I can`t see the text of ListBox items when I inspect them using any inspector, like inspect or Accessibility Insights For Windows.

These are the properties that appear when you use the Accessibility Insights For Windows inspector to inspect the ListBox control:

VisualEffects   0
AcceleratorKey  Property does not exist
AccessKey   Property does not exist
AriaProperties  Property does not exist
AriaRole    Property does not exist
AutomationId    1021
BoundingRectangle   [l=411,t=227,r=699,b=425]
ClassName   ListBox
ControlType List(50008)
Culture 0
FillType    0
FrameworkId Win32
HasKeyboardFocus    True
HeadingLevel    HeadingLevel_None (80050)
HelpText    Property does not exist
IsContentElement    True
IsControlElement    True
IsDataValidForForm  False
IsDialog    False
IsEnabled   True
IsKeyboardFocusable True
IsOffscreen False
IsPassword  False
IsPeripheral    False
IsRequiredForForm   False
LegacyIAccessiblePattern.ChildId    0
LegacyIAccessiblePattern.Role   ROLE_SYSTEM_LIST(33)
LegacyIAccessiblePattern.Selection  [‏‏عنصر قائمة ""]
LegacyIAccessiblePattern.State  1048580
LiveSetting 0
LocalizedControlType    ‏‏قائمة
Name    Property does not exist
NativeWindowHandle  6292800
OptimizeForVisualContent    False
Orientation None(0)
ProcessId   21908
ProviderDescription [pid:20900,providerId:0x600540 Main:Nested [pid:21908,providerId:0x600540 Annotation(parent link):Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main:Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]; Nonclient:Microsoft: Non-Client Proxy (unmanaged:UIAutomationCore.dll); Hwnd(parent link):Microsoft: HWND Proxy (unmanaged:UIAutomationCore.dll)]
RuntimeId   [2A,600540]
SelectionPattern.CanSelectMultiple  False
SelectionPattern.IsSelectionRequired    True
SelectionPattern.Selection  [‏‏عنصر قائمة ""]

These are the properties that also appear when you use the Accessibility Insights For Windows inspector to examine each item in the ListBox control:

ProviderDescription [pid:21908,providerId:0x0 Annotation:Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main(parent link):Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]
AcceleratorKey  Property does not exist
AccessKey   Property does not exist
AriaProperties  Property does not exist
AriaRole    Property does not exist
BoundingRectangle   [l=329,t=109,r=613,b=129]
ControlType ListItem(50007)
Culture 0
FillType    0
HasKeyboardFocus    False
HeadingLevel    HeadingLevel_None (80050)
HelpText    Property does not exist
IsContentElement    True
IsControlElement    True
IsDataValidForForm  False
IsDialog    False
IsEnabled   True
IsKeyboardFocusable True
IsOffscreen False
IsPassword  False
IsPeripheral    False
IsRequiredForForm   False
LegacyIAccessiblePattern.ChildId    1
LegacyIAccessiblePattern.DefaultAction  نقر مزدوج
LegacyIAccessiblePattern.Role   ROLE_SYSTEM_LISTITEM(34)
LegacyIAccessiblePattern.Selection  [‏‏عنصر قائمة ""]
LegacyIAccessiblePattern.State  3145730
LiveSetting 0
LocalizedControlType    ‏‏عنصر قائمة
Name    Property does not exist
NativeWindowHandle  0
OptimizeForVisualContent    False
Orientation None(0)
ProcessId   21908
RuntimeId   [2A,600540,4,80000001,600540,FFFFFFFC,1]
SelectionItemPattern.IsSelected True
SelectionItemPattern.SelectionContainer ‏‏قائمة ""
VisualEffects   0

As you can see from the inspection, the text of the items does not appear in any property, whether by inspecting the ListBox control completely, or by inspecting each item in it.

This is an image of the program I am trying to inspect the ListBox control from:

image

I tried to use the following code, which finds the window, then the control, and then sends a control message called LB_GETTEXT, but unfortunately it returns empty text:

import win32gui
import win32con

# Find the handle of the main window
app_name = u"The Dictionary      القـامــــوس النسخة الجديدة 2006/2007"
window_handle = win32gui.FindWindow(None, app_name)

if window_handle == 0:
    print("Cannot find handle from window")
    exit()

# Find the handle of the listbox within the main window
control_class = "ListBox"
listbox_handle = win32gui.FindWindowEx(window_handle, None, control_class, None)

if listbox_handle == 0:
    print("Cannot find handle from listbox in window")
    exit()

# Get the count of items in the listbox

item_count = win32gui.SendMessage(listbox_handle, win32con.LB_GETCOUNT, 0, 0)

print("Count of items in listbox is:", item_count)

# Iterate over each item in the listbox and print its text
for index in range(item_count):
    # Get the length of the item's text
    item_text_length = win32gui.SendMessage(listbox_handle, win32con.LB_GETTEXTLEN, index, 0)

    # Create a buffer to hold the item's text
    item_text_buffer = win32gui.PyMakeBuffer(item_text_length + 1)

    # Get the item's text and store it in the buffer
    win32gui.SendMessage(listbox_handle, win32con.LB_GETTEXT, index, item_text_buffer)

    # Convert the buffer to a string
    item_text = item_text_buffer[:item_text_length]

    # Print the item's text
    print("Item text", index, ":", item_text)

Note : When I run the code, it is getting me this result :

Count of items in listbox is: 0

Please, how can I extract text from the ListBox? Why does the text not appear when searching? Why does the code return an empty result without any text? How can I solve this problem and extract the text from the ListBox control?

A very important note is that this program does not have any protection. I tried to extract the text of the items from several regular ListBox programs, but unfortunately the text cannot be extracted.

A final note is that I do not want to use OCR to extract texts from the control.

0

There are 0 answers