how can i center a control by it's parent?

heres my ScreenToClient():

1
2
3
4
void ScreenToClient(HWND WindowDestination, RECT *WindowRectangle)
{
    MapWindowPoints(NULL, WindowDestination, LPPOINT(WindowRectangle), 2);
}


and heres how i center a control:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void Center()
        {
            //getting the controls positions\size
            RECT frm,frmparent;
            
            //getting the parent coordenates
            //depending if is a form or the desktop\screen
            if(GetParent(hwnd)!=NULL)
            {
                GetWindowRect(GetParent(hwnd), &frmparent);
                ScreenToClient(GetParent(hwnd),&frmparent);
            }
            else
            {
                GetWindowRect(GetDesktopWindow(), &frmparent);
                ScreenToClient(GetDesktopWindow(),&frmparent);
            }
            
            //getting the control coordenates
            //i have a 2nd ScreenToClient() api function for accept the RECT
            GetWindowRect(hwnd, &frm);            
            if(GetParent(hwnd)!=NULL)
            {
                ScreenToClient(GetParent(hwnd),&frm);
            }
            else

            {
                ScreenToClient(GetDesktopWindow(),&frm);
            }
            
            //calculate the center
            LONG x=(frmparent.right-frmparent.left)/2 - (frm.right-frm.left)/2;
            LONG y=(frmparent.bottom-frmparent.top)/2 - (frm.bottom-frm.top)/2;
            
            //position the control
            SetWindowPos(hwnd,0,x ,y,0,0, SWP_NOSIZE |  SWP_NOZORDER);
        }

i see the control more on center of the screen, instead center of parent form :(
what i'm doing wrong?
Moderator: i can't create threads with code. 1st i need create the thread then i must edit it for add the code. please see the problem
Last edited on
Children coordinates are relative to its parent (not screen), so if you create children window at 20,20 it will appear at screen parentX+20, parentY+20.

If you parent is not desktop, dont map client position to screen position with ScreenToClient.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void CenterInsideParent(HWND hWnd, ControlPositions ControlPosition=CenterCenter)
{
    //getting the parent
    HWND parent=GetDesktopWindow();
    if (!(GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD) && parent==GetDesktopWindow())
    {
        if(GetProp(hWnd, "parent")!=NULL)
            parent=(HWND)GetProp(hWnd, "parent");
    }
    else
        parent=GetParent(hWnd);

    //getting the controls positions\size
    //depending if is a child or not
    RECT frm,frmparent;

    if ((GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD))
    {
        GetClientRect(parent, &frmparent);
        GetWindowRect(hWnd, &frm);
        ScreenToClient(parent,&frm);
    }
    else
    {
        GetWindowRect(parent, &frmparent);
        GetWindowRect(hWnd, &frm);
    }

    //calculate the position choosed
    LONG x;
    LONG y;
    if (parent!=GetDesktopWindow())
    {
        if(ControlPosition==0)
        {
            x=(frmparent.left+((frmparent.right-frmparent.left)/2)) - ((frm.right-frm.left)/2);
            y=(frmparent.top+((frmparent.bottom-frmparent.top)/2)) - ((frm.bottom-frm.top)/2);
        }
        else if (ControlPosition==1)
        {
            x=frmparent.left;
            y=(frmparent.top+((frmparent.bottom-frmparent.top)/2)) - ((frm.bottom-frm.top)/2);
        }

    }
    else //center of screen
    {
        if(ControlPosition==0)
        {
            x=(((frmparent.right-frmparent.left)/2)) - ((frm.right-frm.left)/2);
            y=(((frmparent.bottom-frmparent.top)/2)) - ((frm.bottom-frm.top)/2);
        }
        else if (ControlPosition==1)
        {
            x=0;
            y=(((frmparent.bottom-frmparent.top)/2)) - ((frm.bottom-frm.top)/2)+3;
        }
    }

    //position the control
    SetWindowPos(hWnd,0,x ,y,0,0, SWP_NOSIZE |  SWP_NOZORDER);
}

the child control it's on wright position. but the form isn't.
if (parent!=GetDesktopWindow())
and
1
2
3
4
5
 else if (ControlPosition==1)
        {
            x=frmparent.left;
            y=(frmparent.top+((frmparent.bottom-frmparent.top)/2)) - ((frm.bottom-frm.top)/2);
        }

the frmparent.left is less 3 or 4 pixels from original value. why?
Topic archived. No new replies allowed.