Reputation: 121
I have a Windows app(Windows Form Application). I need run a automation to grab all the text in the current window, including window tiles, and all the text of all the elements inside the window.
I tried using SendMessage and GetWindowText, it only returns the window tile, but I need to get the text inside the window too.
Is there a method which can grab all text in the window?
Or do I need to loop through all elements, and get the text? and how to do it?
thanks in advance.
Upvotes: 0
Views: 975
Reputation: 1681
You will have to traverse all elements on the form and get their text values separately.
This recursive function will return a string list of all the text values of the control you specify and all its child controls.
private List<string> getStrings(Control control)
{
List<string> retval = new List<string>();
if (control.Text != "")
retval.Add(control.Text);
foreach (Control child in control.Controls)
retval.AddRange(getStrings(child));
return retval;
}
So if you call this from your main form, you'd call it like this:
List<string> allStrings = getStrings(this);
Take note though that this function only returns the text values of all the controls on the form, and following their child controls recursively. Some controls (like ListBox) for instance may have a whole list of text values inside which will not be returned by this function. To do that you may have to extend the function somewhat to test for the type of control and return its contents specifically if applicable.
Upvotes: 0
Reputation: 109130
Is there a method which can grab all text in the window?
No, or rather "not without knowing how the text is rendered".
If the Window contains other controls (eg. labels) then you need to enumerate those controls and get their text.
If the text is being drawn directly onto the surface of the window (eg. in WM_PAINT
handler) then it is just a bitmap being displayed (the text has been lost) and there is no easy way to recover it if there is nothing provided by the application (OCR is another option).
Upvotes: 1