|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Specifying the parent when adding treeview nodes?I'm using the EnumChildWindows API with an EnumChildWndProc callback
to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node ?? When adding a new node is there any way to get an handle or something else to be able add the childs to the correct parent ? Thanks! You can only get the Parent property on the TreeNode class. If you want
to add a TreeNode as a child of another TreeNode, you have to have a reference to the parent TreeNode, and call the Add method on the Nodes collection exposed by the parent. -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "SQACSharp" <lsdiscip***@hotmail.com> wrote in message news:9c8f3587-d998-4d86-9798-4d8c1df6fa65@d4g2000prg.googlegroups.com... > I'm using the EnumChildWindows API with an EnumChildWndProc callback > to populate the treeview. The output will be something similar to spy+ > + > > How can I specify the parent when adding a new node ?? > > When adding a new node is there any way to get an handle or something > else to be able add the childs to the correct parent ? > > Thanks! Sorry it's hard to explain for me, so maybe you will understand the
problem of enumerating child windows and adding the node to the correct parent with an example : //... private bool EnumChildWindowsCallBack(IntPtr hWnd, Int32 lParam) { System.Text.StringBuilder buffer = new System.Text.StringBuilder(256); GetClassName(hWnd, buffer, buffer.Capacity); ChildWindows.Add(hWnd.ToString() + " " + buffer.ToString()); return true; } private bool EnumWindowCallback(IntPtr hWnd,UInt32 lParam) { System.Text.StringBuilder buffer = new System.Text.StringBuilder(256); GetClassName(hWnd, buffer, buffer.Capacity); ChildWindows = new ArrayList(); EnumChildWindows(hWnd, new EnumChildWindowsProc(EnumChildWindowsCallBack), 0); ListOfWindows.Add(hWnd.ToString() + " " + buffer.ToString()); ListOfChildWindows.Add(ChildWindows); return true; } //... Process[] processlist = Process.GetProcesses(); int Counter = 0; treeView1.BeginUpdate(); foreach (Process theprocess in processlist) { ProcessThreadCollection myThreads; myThreads = theprocess.Threads; int processId; IntPtr ThreadID1 = GetWindowThreadProcessId(theprocess.MainWindowHandle,IntPtr.Zero); ListOfWindows = new ArrayList(); ListOfChildWindows = new ArrayList(); EnumThreadWndProc cb = new EnumThreadWndProc(EnumWindowCallback); EnumThreadWindows((uint)ThreadID1, cb, 0); //At this point ListOfWindows contains all the windows associated to the process //At this point ListOfChildWindows contains all the childs windows associated to the window (kind of 2 dimension array) // Add a node with the main process name TreeNode MyNode = new TreeNode(); MyNode.Text = theprocess.ProcessName + " - " + theprocess.MainWindowTitle + " [" + theprocess.Id.ToString() + "]"; Counter++; int WindowCounter = 0; foreach (string MyWindow in ListOfWindows) { // Add a node for each windows associated to the processId MyNode.Nodes.Add(new TreeNode(MyWindow)); foreach (string child in (ArrayList)ListOfChildWindows[WindowCounter]) { //Add all the childs associated to the window //*** PROBLEM BEGIN *** //The following node must be added to the correct parent..it's added to the same level as other windows for now MyNode.Nodes.Add("->"+child.ToString()); //*** PROBLEM END *** } WindowCounter++; } } The problem is when looping to add the child windows, the parent can be my Window or another child...since EnumChildWindowsCallback list child that can be parent of another child. In other words i'm trying to match the child window to the correct parent window node with something like (this is not valid ;o)) : //Begin Fictive code ParentHandle=GetParent(ChildHandle); //GetParent win32 api ParentNodeObject=GetNodeObjectWithHandle(ParentHandle); ParentNodeObject.Nodes.Add(Child.ToString()); //End Fictive code ;o) How can i get a reference to to parent window? On 2007-11-27 15:01:07 -0800, SQACSharp <lsdiscip***@hotmail.com> said:
> Sorry it's hard to explain for me, so maybe you will understand the Basically, you need to keep track of the node corresponding to the > problem of enumerating child windows and adding the node to the > correct parent with an example : parent. There are lots of ways you might do this, but in the code you posted, it might look something like this (original code quoted, new code unquoted): > [...] TreeNode nodeParent = new TreeNode(MyWindow);> foreach (string MyWindow in ListOfWindows) > { > // Add a node for each windows associated to the processId MyNode.Nodes.Add(nodeParent));> nodeParent.Nodes.Add("->"+child.ToString());> foreach (string child in > (ArrayList)ListOfChildWindows[WindowCounter]) > { > //Add all the childs associated to the window > > //*** PROBLEM BEGIN *** > //The following node must be added to the correct > parent..it's added to the same level as other windows for now > > I am curious whether you care that you've only obtained a list one > //*** PROBLEM END *** > } > WindowCounter++; > } > } level deep in the window hierarchy. Any window can have a child window, and so you really ought to have some kind of recursive operation that actually does enumerate every window of the process, rather than just the non-child windows and their children. But assuming that the one-level-deep is sufficient for your needs, the above change should work for you. Pete 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. The only thing i know is the parent handle returned by Win32 GetParent. I'm trying to find a way to select the correct node with the correct handle to add the child node. Example of a window : MainWindows1, Handle=12345 |----Label1, Handle=54321 child groupbox1 and not the main window)|----GroupBox1 , Handle=11111 |---- CheckBox1, Handle=666 (the parent of checkbox1 is the So when calling EnumChildWindows it return label1, groupbox1 and checkbox1..... When adding the node for checkbox1 how to tell that the node must be created under the node GroupBox1 (with the handle 11111) The solution can be to use the key to store the handle of the window... but i'm still trying to find the way to found the node with a specific key and being able to add a child node under it. Thanks again... On 2007-11-27 17:23:47 -0800, SQACSharp <lsdiscip***@hotmail.com> said:
> Ok... I will try to explain the problem again : Ah. For some reason I was thinking it behaved the same as GetWindow(). > > 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. 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 Thanks peter and Nicholas for trying to help.. I really appreciate
your help and thank you for your time!! :) I found the solution after loosing all the day on this : When adding the node I use the .Add function with two parameters (the keys and the text). The window handle is stored in the key parameter. Then, when adding my child node I simply use the .Find function to search the parent node with the handle. The TreeNode returned by the .Find function can be use to add nodes! On 2007-11-27 13:30:35 -0800, SQACSharp <lsdiscip***@hotmail.com> said:
> I'm using the EnumChildWindows API with an EnumChildWndProc callback If I understand the question correctly, the fact that you are > to populate the treeview. The output will be something similar to spy+ > + > > How can I specify the parent when adding a new node ?? > > When adding a new node is there any way to get an handle or something > else to be able add the childs to the correct parent ? enumerating windows is irrelevant to your question. And again, assuming I understand the question correctly, the answer is that you simply add the node to the Nodes collection of the node you want to be the parent. For a root-level node, this will be the Nodes collection of the TreeView itself, and for any other node, this will be the Nodes collection of a TreeNode. If that doesn't answer your question, you should rephrase your question, explaining the relevance of the window enumeration if there is any, and more specifically describing what it is you actually want to do. Pete
Other interesting topics
|
|||||||||||||||||||||||