Windows Develop Bookmark and Share   
 index > Windows Forms General > Grabbing data from a control on a window
 

Grabbing data from a control on a window

I posted this on the win32.programmer.ui newsgroup but need a quicker answer.

I have some c# which is using the user32.dll to get the window information.
But I need to provide a class name and get back the text of a control inside
the window I have the handle for.

so I need something like....

getControlText(IntrPtr hWnd, String classname, out mytext)

where hWnd will be the hdnale for the window, classname is the classname of the control and mytext will get back the text in that control.


or if I could get the ID of the control based on a classname and then use
the id to grab the text, that of course would work too.

int getControlId(IntrPtr hWnd, String classname)
getControlText(IntrPtr hWnd, int controlId, out mytext)

Thanks in advance.

madhombre  Wednesday, June 28, 2006 3:10 PM
It sounds like you have the window handle to a dialog window but not the handle to the controls on that dialog. You'll need to guess the ID that was assigned to the control. First try calling GetDlgItem() for all possible IDs so you'll know what ID was used. Call GetDlgItemText() to retrieve the text or title associate with the control.

I'm not sure if this is going to work if you access a dialog in another process, you may have to play some nasty games (VirtualAllocEx, ReadProcessMemory et al) to get the text. See this webpage for reference...

nobugz  Wednesday, June 28, 2006 3:55 PM

"First try calling GetDlgItem() for all possible IDs so you'll know what ID was used.First try calling GetDlgItem() for all possible IDs so you'll know what ID was used."

Do you mean grab all controls on the window and just go thru them until I find the one I want (how do I get the controls?)

OR just try all control ids like 0 theu 10000 or some such? Seems like a huge cludge.

Thanks for the help.

madhombre  Wednesday, June 28, 2006 6:37 PM
I assumed you're trying to grab data off a program whose source you do not have available. The only way to find out what IDs the original programmer used is to try them all. You only need to do that once, assigned IDs don't change. Once you figured out the ID you need, you can then write your program using the ID you know will work.

Then again, I might completely misunderstand your request...

nobugz  Wednesday, June 28, 2006 6:50 PM

ok so I have a little program called Auto It and it seems to give me a control Id BUT

when I write out the result of the call I get0

here is the code...

IntPtr hParent = IntPtr.Zero;

IntPtr hNext = IntPtr.Zero;

String sClassNameFilter = "EKPANEL";

do

{

hNext = FindWindowEx(hParent, hNext, sClassNameFilter, IntPtr.Zero);

if (!hNext.Equals(IntPtr.Zero))

{

STRINGBUFFER sLimitedLengthWindowTitle;

GetWindowText(hNext, out sLimitedLengthWindowTitle, 256);

Console.WriteLine(sLimitedLengthWindowTitle.szText);

Console.WriteLine(GetDlgItem(hNext, 53)); // Control Id per auto it

}

while (!hNext.Equals(IntPtr.Zero));

}

madhombre  Wednesday, June 28, 2006 7:50 PM
This article suggests that GetDlgItem() could work from another process. Other than that, I have no idea...

Note: not sure but it is probably a good idea to initialize the StringBuffer like this:
sLimitedLengthWindowTitle(256);
Then use the return value from GetWindowText to trim the buffer into a string.

nobugz  Wednesday, June 28, 2006 8:32 PM

looping thru the children using recursion but it hits the end of the chain and keeps going,

GetNextDlgGroupItem never returns anything I can sense it is the end of the chain!

I will keep at it.

madhombre  Thursday, June 29, 2006 6:37 PM

You can use google to search for other answers

Custom Search

More Threads

• Designing for multiple users - overall strategies?
• Rendering Aero Glass to a WinForms form?
• Maximize and Form Size
• How to disable a property in the property grid?
• Draw a control such as RichTextBox inside a datagridviewcell
• Handling special characters with SQL data
• Create an application that allows for secure file transfer
• Changing the text in an MDIForm's StatusBar from a Module
• How do you paint at the top of the z-order on a form
• How to determine if a form IsDirty