News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Centering a child control

Started by Ar0n, February 01, 2015, 01:54:44 PM

Previous topic - Next topic

Ar0n

Hello, trying to center a control to its parent, it doesn't center correctly :/ does anyone know the problem?
If I set up the parent window without borders, it works...  :icon_eek: It is necessary to get the size of the window title? :/

.586
.model flat,stdcall
option casemap:none

   include windows.inc
   include user32.inc
   include kernel32.inc
   
   includelib user32.lib
   includelib kernel32.lib


WinMain proto :DWORD,:DWORD,:DWORD,:DWORD


.data
   ClassName db "MainWinClass",0
   AppName  db "Main Window",0
   buttonclass db "button",0

.data?
   hInstance HINSTANCE ?
   CommandLine LPSTR ?

.code


; ---------------------------------------------------------------------------


start:
invoke GetModuleHandle, NULL
mov    hInstance,eax

invoke GetCommandLine
mov    CommandLine,eax

invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax

centerchild proc hChild:HWND
    LOCAL hwndParent:HWND
    LOCAL rect:RECT
    LOCAL rectP:RECT
    LOCAL Right:LONG
    LOCAL Botton:LONG
   
    invoke GetParent, hChild
    .if eax
        lea esi, rectP
        invoke GetWindowRect, eax, esi;
        lea esi, rect
        invoke GetWindowRect, hChild, esi;
       
        mov eax, rect.right
        .if rectP.right > eax
             mov eax, rect.bottom
            .if rectP.bottom > eax
               
                mov eax, rectP.right
                mov ebx, rect.right
                shr eax, 1
                shr ebx, 1               
                sub eax, ebx
                mov Right, eax
               
                mov eax, rectP.bottom
                mov ebx, rect.bottom
                shr eax, 1
                shr ebx, 1               
                sub eax, ebx
                mov Botton, eax               
               
                invoke MoveWindow, hChild, Right, Botton, rect.right, rect.bottom, TRUE
            .endif
        .endif
    .endif
   
    ret

centerchild endp

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND

mov   wc.cbSize,SIZEOF WNDCLASSEX
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,NULL
push  hInstance
pop   wc.hInstance
mov   wc.hbrBackground,COLOR_BTNFACE+1
mov   wc.lpszMenuName,NULL
mov   wc.lpszClassName,OFFSET ClassName

invoke LoadIcon,NULL,IDI_APPLICATION
mov   wc.hIcon,eax
mov   wc.hIconSm,eax

invoke LoadCursor,NULL,IDC_ARROW
mov   wc.hCursor,eax

invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
           0,400,120,NULL,NULL,\
           hInst,NULL
mov   hwnd,eax

invoke CreateWindowEx,0,addr buttonclass,addr AppName,WS_VISIBLE or WS_CHILD,0,0,200,60,eax,0,hInst,0

invoke centerchild,eax


invoke ShowWindow, hwnd,SW_SHOWNORMAL
invoke UpdateWindow, hwnd

.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW

mov     eax,msg.wParam
ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.IF uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_CREATE
;
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF

xor eax,eax
ret
WndProc endp


end start

sinsi

Try using GetClientRect to get the "workable" part of the parent.
😁

jj2007

Indeed,
         invoke GetClientRect, eax, esi;
         ...
         invoke GetClientRect, hChild, esi;
does the job. Sinsi was faster ;-)

sinsi

Not sure if you should use it on the child though, you want to centre the whole control not just the client area.
😁

dedndave

CenterChild PROC hWnd:HWND,hChild:HWND

    LOCAL   _rcClient   :RECT
    LOCAL   _rcChild    :RECT

    INVOKE  GetClientRect,hWnd,addr _rcClient
    INVOKE  GetWindowRect,hChild,addr _rcChild
    mov     edx,_rcClient.bottom
    mov     ecx,_rcClient.right
    add     edx,_rcChild.top
    add     ecx,_rcChild.left
    sub     edx,_rcChild.bottom
    sub     ecx,_rcChild.right
    xor     eax,eax
    sar     edx,1
    sar     ecx,1
    INVOKE  SetWindowPos,hChild,eax,ecx,edx,eax,eax,SWP_NOZORDER or SWP_NOSIZE
    ret

CenterChild ENDP

Ar0n