Assembly code snippets

Back to the snippets overview

Details

TitleCenterWindow
Authorstryker
Submitted by:stryker
Date added:2002-06-07 20:21:21
Date modified:2002-06-07 20:22:44

Comments

The CenterWindow function centers a window based on another window.
mainWndHndl = [in] Handle to the main window.
targetWndHndl = [in] Handle to the window to be centered to the main window.
Return Values:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Note:
I listed all the penalties that is involved on the procedure but don't worry about it.

Snippet

CenterWindow PROC USES ebx esi edi mainWndHndl:DWORD, targetWndHndl:DWORD

    LOCAL   mainWnd     :RECT
    LOCAL   targetWnd   :RECT

    xor     eax, eax
    push    eax
    lea     eax, DWORD PTR [ebp-20h]
    push    eax
    mov     edx, DWORD PTR [ebp+0Ch]
    push    edx
    call    GetWindowRect               ;MROM Penalty
    mov     esi, targetWnd.bottom
    mov     edi, targetWnd.right
    sub     esi, targetWnd.top
    sub     edi, targetWnd.left
    push    esi
    push    edi
    shr     esi, 1
    shr     edi, 1
    lea     eax, DWORD PTR [ebp-10h]
    push    eax
    mov     edx, DWORD PTR [ebp+8]
    push    edx
    call    GetWindowRect               ;MROM Penalty
    mov     eax, mainWnd.bottom         ;Bank Conflict
    mov     ecx, mainWnd.right
    sub     eax, mainWnd.top
    sub     ecx, mainWnd.left
    shr     eax, 1
    shr     ecx, 1
    sub     eax, esi
    sub     ecx, edi
    mov     ebx, mainWnd.top
    mov     edx, mainWnd.left
    add     ebx, eax
    add     edx, ecx
    push    ebx
    push    edx
    mov     eax, targetWndHndl
    push    eax
    call    MoveWindow                  ;MROM Penalty

    ret

    ;pop    edi                         ;MROM Penalty
    ;pop    esi                         ;MROM Penalty
    ;pop    ebx                         ;MROM Penalty
    ;+ leave                            ;MROM Penalty
    ;  - pop    ebp                     ;...
    ;  - mov    esp, ebp                ;...
    ;retn   8                           ;Branch Misprediction, MROM Penalty

CenterWindow ENDP