• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1677
  • Last Modified:


If you have Charles Petzold's "Programming Windows 95" book,  look at figure 12.4 on page 603. I am trying to write code that will get the highlighted text "PSM_APPLY" into a string. My approach is to use sendmessage using TVM_GETITEM. So far all attempts have failed. I am attempting this with VB and may not have my TV_ITEM structure quite right. Any thoughts??
1 Solution
jdwarrenAuthor Commented:
Edited text of question
What does your TV_ITEM structure look like?
jdwarrenAuthor Commented:
Currently I presume all records of the structure are 32 bit as shown below:
  mask AS LONG
  hItem AS LONG
  state AS LONG
  stateMask AS LONG
  pszText AS LONG
  cchTextMax AS LONG
  iImage AS LONG
  SelectedImage AS LONG
  cChildren AS LONG
  lParam AS LONG

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Are you passing the structure of your TV_ITEM or the address of the structure?  You need to pass the address so it can be populated.  Does SendMessage always return FALSE here?

What are you setting hItem and mask to?

go here to see a full explanation of the structure:

from microsoft.

I need you to tell me if SendMessage is returning TRUE or FALSE and make sure you zero initialize the structure before setting any of the parms. and then lastly I need you to make sure you are putting a valid number for the cchTextMax

jdwarrenAuthor Commented:
To test code I first run Ctlmacro.exe. This is a sample program that came with the book I referenced above. It displays the same treeview as shown in the book. My code trys to get the folder and item text lables that are displayed in that window. My code is run seperately as an executable.

My Procedure:
I first determine hwnd1 with GetForegroundWindow(), then GetNextDlgGroupItem(hwnd1,NULL,FALSE) to get hwnd2, I then loop using GetNextDlgGroupItem(hwnd1,hwnd2,0) until I find the handle that has a title of "Tree1" and a ClassName of "SysTreeView32".
SendMessage(hwnd2,TVM_GETCOUNT,0,0), and TVM_GETINDENT, and TVM_GETVISIBLECOUNT, provide me with values that suggest that I have the right handle (hwnd2).
Next I get hItem for the root with SendMessage(hwnd2,TVM_GETNEXTITEM,TVGN_ROOT, 0), This returns a non-zero value. Also I can get other non-zero hItem values using TVGN_CARET, TVGN_FIRSTVISIBLE, etc.

On each attempt I set .hItem in the TV_ITEM structure to one of these values and then try to retrieve the text with a SendMessage to hwnd2, TVM_GETITEM and a pointer to pitem which is my TV_ITEM structure.

So far no luck. The text I am after is the name next to a folder or the name of an item that is shown after it has been expanded. I think that is what this supposed to be returned as text.
Sendmessage with TVM_GETITEM returns 0 when on Windows 95 and returns 1 when on NT 4.0 and with both cases the text is not updated. GetLastError() always returns zero just after a SendMessage with TVM_GETITEM

pitem.mask= TVIF_TEXT = 1
Also I set the .cchTextMax and .pszText the exact same way I set .cch and .dwTypeData in the MENUITEMINFO structure(i.e. pointing to 128 character string). Note that GetMenuItemInfo works fine in other applications.

At the start of my code I set:
  pitem.mask = 0
  pitem.hItem = 0
  pitem.state = 0
  pitem.stateMask = 0
  pitem.pszText = 0
  pitem.cchTextMax = 0
  pitem.iImage = 0
  pitem.SelectedImage = 0
  pitem.cChildren = 0
  pitem.lParam = 0          
Is this OK?
jdwarrenAuthor Commented:
Adjusted points to 150
jdwarrenAuthor Commented:
Adjusted points to 200
So if I understand you correctly, you are setting .cchTextMax  to 128?

If you run Spy++ and spy over the controls, if the caption of these folders is set to the text you expect to find, then you should be able to retrieve the information.  If it is not, check to see what the other captions of the controls look like in the window.

In windows, controls themselves are little windows, but in the case of a control, its caption is the text you see.

Let me know what you find.  I don't have the book so I cannot look directly at the code.  YOu could email it to me if you want though.... winexpert@hotmail.com

jdwarrenAuthor Commented:
Yes I am setting .cchTextMax  to 128. Also tried setting it to 127. Also just sent you the sample program from the book via email.

I just found an error where I had defined TVM_GETITEM = (TV_FIRST + 62). It should have been (TV_FIRST + 12). With that corrected, SendMessage now returns 1 on Windows 95.
Still no text is returned when the mask set to TVIF_TEXT  .
 I have also tried the TVIF_CHILDREN mask and that does not update my TV_ITEM structure regardless of which hItem I try. This has nothing to do with text so I am suspicious that something else causing a problem.
spy didn't help me in this case....
Tommy HuiEngineerCommented:
The common control messages cannot be used across process boundaries. You will need to use an injected DLL and communicate with the DLL as to what the common controls contain.

I think this is an oversight with the common controls, but from a speed perspective, it was probably okay.
jdwarrenAuthor Commented:
Before I accept this answer please note that:

SendMessage has so far returned what appears to be valid results with the TVM_GETNEXTITEM, TVM_GETCOUNT, TVM_GETGETINDENT, and TVM_GETVISIBLECOUNT  messages.
You stated that "common control messages cannot be used across process boundaries" but it appears to me that this is not completely true. Please Comment on the above  exceptions.
Thui is a pretty good judge at this.  He used to help me all the time back in '93....
jdwarrenAuthor Commented:
Since it has been difficult for me to find descriptive information on the common controls, like the tidbit you gave me about it being unable to cross process boundaries, make this expert-exchange very valuable. Thanks for taking a look at this problem!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now