The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: msmith on July 25, 2006, 07:06:20 PM

Title: Finding Client Position
Post by: msmith on July 25, 2006, 07:06:20 PM
Since GetWindowRect returns the window's origin referenced to the screen and GetClientRect returns 0,0, I have been doing a GetWindowRect for both the parent and child(window of interest) and then subtracting the position coordinates to get the origin of the child window within the parent.

Surely, there must be a function to do this directly, but I can't find it.
Title: Re: Finding Client Position
Post by: hutch-- on July 26, 2006, 02:41:39 AM
You can have a look to see if ScreenToClient() and ClientToScreen() APIs will help you.
Title: Re: Finding Client Position
Post by: msmith on July 26, 2006, 04:23:40 AM
Thanks Hutch,

ScreenToClient comes pretty close to doing what I want. I looked at all of my compiler sources and I used it 1 time to convert mouse position for screen to client.

If you do a GetWindowRect, set the POINT structure with rectleft and recttop, and then do a ScreenToClient, that should do the job.

It's too bad that GetClientRect doesn't do exactly that.

It turns out that GetClientRect is really a GetWindowSize function as it returns 0,0 for x,y and returns width for rectright and height for rectbottom.

Regards,

Mike
Title: Re: Finding Client Position
Post by: msmith on July 26, 2006, 04:55:57 AM
Quote
If you do a GetWindowRect, set the POINT structure with rectleft and recttop, and then do a ScreenToClient, that should do the job.

Actually, it's better than that because the POINT structure is the same as the first two dwords of the RECT structure, so you can do a GetWindowRect followed by a ScreenToClient using the same RECT structure for both. This means there is no need to set a POINT structure at all.
Title: Re: Finding Client Position
Post by: Shantanu Gadgil on July 26, 2006, 12:37:59 PM
This is a function I have written and use frequently as I work with dialogs quite a bit! :)

Hope you find it useful too!

GetControlRect proc hWinParent:HWND, hWinControl:HWND, lprcRectangle:DWORD
LOCAL ptLocalControl:POINT
LOCAL rcLocalControl:RECT

push eax
push ebx

invoke GetWindowRect,hWinControl,addr rcLocalControl
mov eax,rcLocalControl.right
sub eax,rcLocalControl.left
mov ebx,lprcRectangle
mov (RECT ptr [ebx]).right, eax ;WIDTH

mov eax,rcLocalControl.bottom
sub eax,rcLocalControl.top
mov ebx,lprcRectangle
mov (RECT ptr [ebx]).bottom, eax ;HEIGHT

push rcLocalControl.left
pop ptLocalControl.x
push rcLocalControl.top
pop ptLocalControl.y

invoke ScreenToClient,hWinParent,addr ptLocalControl

mov ebx,lprcRectangle
mov eax,ptLocalControl.x
mov (RECT ptr [ebx]).left, eax

mov eax,ptLocalControl.y
mov ebx,lprcRectangle
mov (RECT ptr [ebx]).top, eax

pop ebx
pop eax
ret
GetControlRect endp


Cheers,
Shantanu
Title: Re: Finding Client Position
Post by: ToutEnMasm on July 26, 2006, 05:12:42 PM

Hello,
a word on the struct RECT,she is very badly defined.
RECT STRUCT
  left    dd      ?
  top     dd      ?
  right   dd      ?
  bottom  dd      ?
RECT ENDS
If you can extract  a lenght of this without the help of winhelp,you are very strong

I have re-defined it like that.
   FRECT   STRUCT
      min POINT <>
      max POINT <>
   FRECT ENDS
and now you can write,

           rect FRECT <>
           width = rect.max.x -rect.min.x
           height = rect.max.y -rect.min.y
no need of winhelp to understand a basic operation