On 2007-11-27 17:23:47 -0800, SQACSharp <ls*********@hotmail.comsaid:
Ok... I will try to explain the problem again :
EnumChildWindows return **ALL** child windows of a given handle. A
child window can also be a parent of another child and so on... So
when adding childs, the parent can be the main window or any other
child returned by enumChildWindow.
Ah. For some reason I was thinking it behaved the same as GetWindow().
It's been awhile.
It didn't help that your original code didn't even put the child
windows under the thread node in the TreevView. It was a bit confusing
to try to understand what exactly you were trying to do.
Anyway, as far as the specific question goes...
One possible solution would be in fact to use GetWindow() to enumerate
the windows in the process. That way your enumeration could be done in
a recursive method that also tracks the current parent node so that it
could be handled appropriately.
Alternatively, you could store the TreeNode instances you create in a
Dictionary<IntPtr, TreeNode>, where the window handle corresponding to
the node is used for the key. Then when adding a TreeNode, you can use
GetParent() to get the window handle for the parent of the current
node's window handle, use that to look up the appropriate TreeNode and
add the current node to that node's Nodes collection.
There are actually lots of other ways to do this, assuming I finally
understand the question correctly. But I think the two methods above
are good representations of the two basic ideas you might use. That
is, either:
1) enumerate the child windows in a way such that you always know
the relationship of the current node in the enumeration to its parent.
This is likely to always involve some kind of recursion, whether
explicitly as a recursive method or using a stack data structure to
maintain the state of the enumeration.
2) retrieve the parent information from the current node, given the
window handle of the parent. A dictionary is an efficient way to do
this, but you could do a brute-force search of the TreeViews nodes if
you really wanted to.
If the above doesn't help, you'll have to try to elaborate again I think.
Pete