News:

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

Centering a window on another window

Started by NoCforMe, April 09, 2012, 01:56:13 AM

Previous topic - Next topic

Farabi

Quote from: dedndave on April 09, 2012, 03:35:22 AM
here is what the code looks like
i always like to look at the disassembly to see where i can do better   :P
00000018 Calc    PROC

00000018  B8 00000000 R         mov     eax,offset mws
0000001D  E8 00000005 call    Calc00
00000022  8B CA         mov     ecx,edx
00000024  83 C0 04 add     eax,4

00000027  51         Calc00: push    ecx
00000028  8B 50 10 mov     edx,[eax].MyWinStruct.rcRect.right
0000002B  8B 48 08 mov     ecx,[eax].MyWinStruct.rcRect.left
0000002E  2B 10         sub     edx,[eax].MyWinStruct.dwWidth
00000030  2B D1         sub     edx,ecx
00000032  D1 FA         sar     edx,1
00000034  03 D1         add     edx,ecx
00000036  59 pop     ecx
00000037  C3 ret

00000038 Calc    ENDP

; Calculate X-position of window:
00000038  A1 00000010 R         MOV EAX, mws.rcRect.right
0000003D  2B 05 00000008 R SUB EAX, mws.rcRect.left
00000043  2B 05 00000000 R SUB EAX, mws.dwWidth
00000049  D1 F8         SAR EAX, 1 ;Quick-n-dirty IDIV / 2.
0000004B  03 05 00000008 R ADD EAX, mws.rcRect.left
00000051  8B C8         MOV ECX, EAX ;Stash x-pos.

; Calculate Y-position of window:
00000053  A1 00000014 R         MOV EAX, mws.rcRect.bottom
00000058  2B 05 0000000C R SUB EAX, mws.rcRect.top
0000005E  2B 05 00000004 R SUB EAX, mws.dwHeight
00000064  D1 F8         SAR EAX, 1
00000066  03 05 0000000C R ADD EAX, mws.rcRect.top
0000006C  8B D0         MOV EDX, EAX
0000006E  C3 RET


55 bytes of code vs 32
How'd did you do that? What disassembler did you used?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

dedndave

masm generated that listing with the Fl switch
i sometimes use Sang Cho's simple disassembler if i want another view (he's a prof at CheongJu University)
http://hcilab.cju.ac.kr/
there are numerous more powerful disassemblers out there

NoCforMe

Quote from: hfheatherfox07 on April 12, 2012, 08:04:43 PM
Quote from: dedndave on April 12, 2012, 07:59:36 PM
yah - little snafu with coordinates
GetWindowRect returns screen coordinates
MoveWindow and SetWindowPos use client coordinates for child windows, screen coordinates for top-level windows

so - the routine works ok for centering the main window of a program
we can use ScreenToClient to straighten things out, i think

The authors original post said that it does what I thought it would ..????
I have an example that does that .... I just thought he had a better way..... oh well ......
not that important ...... I am sure the author might respond and explain or give a demo of how he uses this proc

look at the first proc  quote "Centers window (by w. & h.) on another window"

Yes, I'm the OP (original poster), and yes, it does what you thought I said it did (and what I did say it did, and what in fact does).

It centers one window, whose X- and Y-coordinates are passed in, on another window, whose handle is passed in. For example, the window to be centered could be a pop-up window, and the window you want it centered on could be your main window. Remember (and this may be where some confusion came in) that the X- and Y-coordinates for centering the window are screen coordinates, not client coordinates (i.e., relative to (0,0) of the parent window). This can't be used to center a child window on a parent window, though it could easily be modified to do so.

It works exactly as described. The way to use it is thus:


INVOKE CenterWindow, winHandle, (window width), (window height)
; X- and Y-coords. come back in ECX & EDX
INVOKE CreateWindowEx, WS_EX_OVERLAPPEDWINDOW, (class), (title),
(styles), ECX, EDX, (window width), (window height),
NULL, NULL, InstanceHandle, NULL



Of course, this shows it being used to create another window centered on the main one. You could just as easily use it with an existing window by calling SetWindowPos() to move the window.

And it works whether the window to be centered is larger or smaller than the other window.

Try it sometime!