News:

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

How to get WindowX and WindowY size

Started by Jojodi, February 03, 2006, 12:55:04 AM

Previous topic - Next topic

Jojodi

Can someone show me how to gather the X and Y sizes of a window and how to print them (comma sperated) in a messagebox? ("xsize,ysize")

MichaelW

This smells like homework, so I'll just describe one method of doing it. You will need a buffer of a suitable size to hold the string that will be displayed in the message box. You can get the width and height of the window by calling the GetWindowRect function. Assuming there is no error, the function will return with the width of the window in the .right member of the RECT structure and the height in the .bottom member. You can use the MASM32 ustr$ macro to convert the width and height to decimal strings, the MASM32 chr$ macro as a quick way to allocate a comma separator string, and the MASM32 append$ macro to append the strings to the buffer, something like this:

    mov ebx, append$(ADDR buffer, ustr$(rect.right), 0)
    mov ebx, append$(ADDR buffer, chr$(","), ebx)
    mov ebx, append$(ADDR buffer, ustr$(rect.bottom), ebx)

For append$, "The return value is the next character past the end of the appended string data so it can be used as the location parameter for a subsequent call to append$." The code stores the return value in ebx so the ustr$ and chr$ macros will not overwrite it (if this code will be in a procedure then the procedure should be coded to preserve ebx). In case you are not allowed to use macros, you can examine the macro definitions (in macros.asm) and substitute code that does essentially what the macros do.
eschew obfuscation

Jojodi

Thank you very much for the code and the excellent explanation, but I was speaking more of the window dimensions than its location on screen. The String concatenation code was useful though  :bg

ramguru

It depends... whether you want whole window (including border) dimensions or only client area (in this case you should use GetClientRect)
If you want whole window dim. you should still
use (invoke GetWindowRect, ADDR rc) but additionaly
convert pt1.x = rc.left, pt1.y =  rc.top to client (invoke ScreenToClient, ADDR pt1)
convert pt2.x = rc.right, pt2.y = rc.bottom to client (invoke ScreenToClient, ADDR pt2)
then substract windowX = pt2.x - pt1.x, windowY = pt2.y - pt1.y

Tedd

C'mon! Think a little..

width = right_side - left_side ..i.e. (rect.right - rect.left)

Height left as an exercise :P
No snowflake in an avalanche feels responsible.

Mincho Georgiev

Another way to do it, if i understand you correctly:

     LOCAL p:POINT
LOCAL nWidth:DWORD
LOCAL nHeight:DWORD
LOCAL szWidth[128] :BYTE
LOCAL szHeight[128] :BYTE

.if uMsg == WM_SIZE

mov eax,lParam
and eax,0FFFFh
mov nWidth,eax
mov eax,lParam
shr eax,16
mov nHeight,eax

invoke wsprintf,addr szHeight, CTXT("%lu"),nHeight
invoke wsprintf,addr szWidth, CTXT("%lu"),nWidth

;//if you need to convert them to Screen coordinates:
;push nWidth
;pop p.x
;push nHeight
;pop p.y
;invoke ClientToScreen,hwnd,addr p
;//and then again 'wsprintf' to convert them to string
;;/////Print Them anywhere here////////////////

Tedd

Yes, in generally (for your own window) use the way shaka shows.
Your window will receive a WM_SIZE message once the window is created, and then again each time its size is changed. This way you can know when your window has been resized. But note that it is the "client size" -- that is, the size of your window's content (not the outside edges.)
No snowflake in an avalanche feels responsible.

Jojodi

Thanks to everyone for the prompt and useful help! Shaka's process looks the best, but I sincerely appeciate everyone's attempts to help  :cheekygreen: