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
Try using GetClientRect to get the "workable" part of the parent.
Indeed,
invoke GetClientRect, eax, esi;
...
invoke GetClientRect, hChild, esi;
does the job. Sinsi was faster ;-)
Not sure if you should use it on the child though, you want to centre the whole control not just the client area.
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
Thank you very much to all :t