News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

MoveWindow coordinates

Started by Jimg, February 15, 2005, 03:34:44 PM

Previous topic - Next topic

Jimg

Is there some api call that returns the coordinates of a window in the same form that MoveWindow wants?  GetWindowRect returns left,top,right,bottom rather than left,top,width,height.  Yes, I know it's easy to compute, but silly to do so if there's some other api call that gives the right answers.

Relvinian

Jimg,

MoveWindow API doesn't have any direct API which will give you the location or coordinates. The reason is because MoveWindow is a little different. GetWindowRect is real close but not exactly.

The format of MoveWindow is (x, y, width, height) and GetWindowRect is (x1, y1, x2, y2  aka X, Y, X+width, Y+Height).  So if you just subtract x2-x1 and y2-y1, you would have the same as MoveWindow for the RECT structure.

Hope this helps.

Relvinian

Jimg

Thank you.
Quotesubtract x2-x1 and y2-y1,
wouldn't this actually need to be x2-x1+1 and y2-y1+1 ?

and I was just confirming that there wasn't some api I just hadn't run accross yet.

Relvinian

Quote from: Jimg on February 15, 2005, 05:14:34 PM
Quotesubtract x2-x1 and y2-y1,
wouldn't this actually need to be x2-x1+1 and y2-y1+1 ?

Not unless you want your window to be 1x1 bigger.  :wink

Relvinian

Jimg

Ok, I thought that right-left would be 1 too small.
eg.   left=1, right=4.
window is 4 wide.
right-left=3.

Relvinian

#5
Quote from: Jimg on February 15, 2005, 08:36:12 PM
Ok, I thought that right-left would be 1 too small.
eg.   left=1, right=4.
window is 4 wide.
right-left=3.


With a left of 1, and a right of 4 that gives 4 like your equation at the bottom (not 4 as your said). You may only have a "client" area of 2 but do you have a total width of 4 (because everything is zero based and windows have "borders").


To give an example of this, let's say we want a window 640x480 -- NOTE: this includes borders, etc (not just client area).
Left = 0
Top = 0
Right = 639
Bottom = 479

Since the computer works in zero based values, we need to account for this (that is why we don't do a +1 normally but when we want absolute window width; including borders; we adjust to one base).

Our window starts on 0,0 and span 640 pixels across ending on 639. The same applies for the height.
Now, because a standard window has some kind of border to show the drawing, you actually only have 638 (or less depending on border with) of "client area".

Relvinian

Jimg

Ok, I'll just have to accept this as just another vagary of windows.  My brain clearly says to me-
If the leftmost pixel is 1 and the rightmost pixel is 4, then there are four pixels accoss.  I could accept subtracting two, one for each border but subtracting one doesn't make any sense.  What the real problem is, is that MoveWindow actually wants one less than the final width.  I would call this a bug in MoveWindow, but then we'd end up in the soapbox.  So I'll just say, Thank you for your patience, I learned something new about an api call that I will have to remember in the future.


Relvinian

Jimg,

Yes, I agree with you that "logic" says to add one make it right but that isn't how it seems to work.   :wink

Have a good one.

Relvinian

Jimg

Hi again.

Now I have a similar question.   What's the best way to move a child window or control down one pixel?  I have tried many things and they all turn out huge.  GetWindowRect return the location relative to the screen, not the parent.  Doing a GetWindowRect on the parent is a start, but the delta between the top of the parent and the top of it's client window varies with menus, etc.

Relvinian

If you want to just move it down a single pixel or any other variation without changing size, z-order, etc, try using SetWindowPos API call.  Probably the easiest way.

Relvinian

Jimg

I tried to ask a simpler question than the actual problem and didn't do it well.
For anyone that wants to know one answer to the question I just asked-
.data
txtPlace WINDOWPLACEMENT <>
.code
invoke GetWindowPlacement,htxt,addr txtPlace
add txtPlace.rcNormalPosition.top,1
add txtPlace.rcNormalPosition.bottom,1
invoke SetWindowPlacement,htxt,addr txtPlace

The problem with SetWindowPos is you have to know the coordinates of the child, which is exactly what I was trying to figure out.

So the actual question should have been and is:
How do you get the coordinates of a child window that is compatible with MoveWindow?

What I'm actually trying to do is keep an edit box in the same position relative to the bottom of the parent when the parent is resized.

Ok, I see one answer, but I don't like the big structure needed for GetWindowPlacement, I was looking for something cleaner and simpler than-

invoke GetWindowPlacement,htxt,addr txtPlace
mov eax,txtPlace.rcNormalPosition.bottom
sub eax,txtPlace.rcNormalPosition.top
mov txtPlace.rcNormalPosition.bottom,eax
mov eax,txtPlace.rcNormalPosition.right
sub eax,txtPlace.rcNormalPosition.left
mov txtPlace.rcNormalPosition.right,eax
add txtPlace.rcNormalPosition.top,1
add txtPlace.rcNormalPosition.bottom,1
invoke MoveWindow,htxt,txtPlace.rcNormalPosition.left,txtPlace.rcNormalPosition.top,txtPlace.rcNormalPosition.right,txtPlace.rcNormalPosition.bottom,TRUE

Tedd

By getting the child's co-ords (which are relative to the parent), and getting the parent's co-ord (which are relative to the screen - ie. (0,0)), and adding the two together?
No snowflake in an avalanche feels responsible.

Jimg

Sorry Tedd, I was modifying my message while you were trying to answer :'( it.

Tedd

One suggestion is the "ClientToScreen" function. Which isn't strictly for this, but I'm assuming it still works in the same way - just make sure you provide the handle of the parent :wink
Note that this only moves the responsibility of the code you've already written into that function; it still does the same (and probably more.)

Alternatively, use GetWindowRect to get the window's position - which will avoid the 'huge' structure :U

[Edit: In fact, you may just be able to give GetWindowRect the child's handle ::)]
No snowflake in an avalanche feels responsible.

Relvinian

To get a child window's position, the following APIs are needed:

GetWindowRect()          for the child control
ScreenToClient() * 2      convert's child window's to parent's window coords   --- or --- MapWindowPoints()
then use either SetWindowPos or MoveWindow.

Example code to take a edit box and move it down one pixe:

    ; get the screen coordinates of the edit control
    invoke GetWindowRect, hEditWnd, addr rectControl

    ; convert to child coordinates (the API calls take POINT pairs
    ; So we need to tell it there are two sets of POINTS)..
    invoke MapWindowPoints, NULL, hParentWnd, addr rectControl, 2

    ; add one to the y position
    add rectControl.top, 1

    ; update the control on screen
    invoke SetWindowPos, hEditWnd, HWND_TOP, \
           rectControl.left, rectControl.top, 0, 0, \
           SWP_NOZORDER or SWP_NOSIZE


Hope this helps.

Relvinian