Assembly code snippets

Back to the snippets overview

Details

TitleCenterScreen
Authorstryker
Submitted by:stryker
Date added:2002-06-07 20:19:26
Date modified:2002-06-07 20:19:26

Comments

The CenterScreen function centers a window based on the primary display monitor`s resolution in pixels.
hWnd = [in] Handle to the 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

CenterScreen PROC USES esi edi hWnd:DWORD

    LOCAL   wRect:RECT

    xor     eax, eax
    push    eax
    lea     eax, DWORD PTR [ebp-16]
    push    eax
    mov     eax, hWnd
    push    eax
    call    GetWindowRect               ;MROM Penalty
    mov     esi, wRect.bottom
    sub     esi, wRect.top
    push    esi
    shr     esi, 1
    mov     edi, wRect.right
    sub     edi, wRect.left
    push    edi
    shr     edi, 1
    mov     eax, SM_CYSCREEN
    push    eax
    call    GetSystemMetrics            ;MROM Penalty
    shr     eax, 1
    sub     eax, esi
    push    eax
    mov     eax, SM_CXSCREEN
    push    eax
    call    GetSystemMetrics            ;MROM Penalty
    shr     eax, 1
    sub     eax, edi
    push    eax
    mov     eax, hWnd
    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   4                           ;Branch Misprediction, MROM Penalty

CenterScreen ENDP