# Call SendMessage WinAPI to get item\_rect for tree control

**URL:** <https://rubytalk.org/t/call-sendmessage-winapi-to-get-item-rect-for-tree-control/56772>\
**Category:** ruby-talk\
**Created:** [24 December 2009 21:28 UTC](https://rubytalk.org/t/call-sendmessage-winapi-to-get-item-rect-for-tree-control/56772 "2009-12-24T21:28:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxim\_Ap](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Maxim\_Ap](https://rubytalk.org/u/Maxim_Ap)\
**Post date:** [24 December 2009 21:28 UTC](https://rubytalk.org/t/call-sendmessage-winapi-to-get-item-rect-for-tree-control/56772/1 "2009-12-24T21:28:49Z")

</div>

Hi everyone!

I am trying to write lib for windows application GUI testing.The problem  
that I see now it is how to call SendMessage WinAPI for tree control to  
get tree item rect.Code Commctrl.h for this looks like:

#define TVM\_GETITEMRECT (TV\_FIRST + 4)  
#define TreeView\_GetItemRect(hwnd, hitem, prc, code) \  
&nbsp;&nbsp;&nbsp;&nbsp;(\*(HTREEITEM \*)prc = (hitem), (BOOL)SNDMSG((hwnd), TVM\_GETITEMRECT,  
(WPARAM)(code), (LPARAM)(RECT \*)(prc)))

And I don't know what is \*(HTREEITEM \*)prc = (hitem) and how to do this  
on Ruby.

For example my Ruby code for GetRoot looks like:

def send\_message(hwnd,msg,wParam,lParam )  
&nbsp;&nbsp;send\_message = user32 'SendMessage' , ['L' , 'L' , 'L' , 'P'], 'L'  
&nbsp;&nbsp;send\_message.call hwnd, msg, wParam, lParam  
end

def get\_tree\_root\_item(htree)  
&nbsp;&nbsp;send\_message(htree,0x1100+10,TVGN\_ROOT,0 )  
end

and this works for me.

Commctrl.h for this looks:  
#define TreeView\_GetRoot(hwnd) TreeView\_GetNextItem(hwnd, NULL,  
TVGN\_ROOT)

#define TVM\_GETNEXTITEM (TV\_FIRST + 10)  
#define TreeView\_GetNextItem(hwnd, hitem, code) \  
&nbsp;&nbsp;&nbsp;&nbsp;(HTREEITEM)SNDMSG((hwnd), TVM\_GETNEXTITEM, (WPARAM)(code),  
(LPARAM)(HTREEITEM)(hitem))

It would be greate if someone can explain what is \*(HTREEITEM \*)prc =  
(hitem) and how to do this in Ruby.

Thanks,  
Maxim

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![AbbyBooth](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@AbbyBooth](https://rubytalk.org/u/AbbyBooth)\
**Post date:** [1 February 2011 18:40 UTC](https://rubytalk.org/t/call-sendmessage-winapi-to-get-item-rect-for-tree-control/56772/2 "2011-02-01T18:40:31Z")

</div>

Did you ever figure this out? I am trying to do something similar

---

<div class="post-metadata">

**Author:** ![Su\_Zhang1](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Su\_Zhang1](https://rubytalk.org/u/Su_Zhang1)\
**Post date:** [1 February 2011 22:12 UTC](https://rubytalk.org/t/call-sendmessage-winapi-to-get-item-rect-for-tree-control/56772/3 "2011-02-01T22:12:49Z")

</div>

You need to pass a buffer to SendMessage to store the rectangle's  
information. You can pass the API a Ruby string, and Win32API will pass  
the actual address of the string to the API. The API then mutate the  
string content so that it stores the rectangle information. The string  
itself is like a primitive structure containing different fields, and  
later on you can use String#unpack to retrieve every single field.

Suppose you already have the handle to the controls and the message's  
internal integer representation defined:

buf = [handle\_tree\_item, 0, 0, 0].pack('l\*')  
send\_message = Win32API.new('user32', 'SendMessage' , ['L' , 'L' , 'L' ,  
'P' ], 'L')  
sned\_message.call(handle\_tree\_view, TVM\_GETITEMRECT, 0, buf)  
rect = buf.unpack('l\*')

rect[0] # left  
rect[1] # top  
rect[2] # right  
rect[3] # bottom

> And I don't know what is \*(HTREEITEM \*)prc = (hitem) and how to do this  
> on Ruby.

This is basically a dereference operation on the pointer prc, which  
cannot be done in Ruby in the language level. You can use RtlMoveMemory  
supplied by kernel32.dll to achieve the same thing in an ugly manner.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Maxim\_Ap](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Maxim\_Ap](https://rubytalk.org/u/Maxim_Ap)\
**Post date:** [2 February 2011 00:27 UTC](https://rubytalk.org/t/call-sendmessage-winapi-to-get-item-rect-for-tree-control/56772/4 "2011-02-02T00:27:18Z")

</div>

Eric wrote in post #978925:

> Did you ever figure this out? I am trying to do something similar

I found more flexible solution to work with Windows applications. I  
integrate AutoIT.dll with Ruby.It works great and meet all my  
requirements.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
