heres my function for convert to client using RECT:

void ScreenToClient(HWND WindowDestination, RECT *WindowRectangle)
{
    POINT a={WindowRectangle->right, WindowRectangle->bottom};
    WindowRectangle->left=0;
    WindowRectangle->top=0;
    ::ScreenToClient(WindowDestination,&a);
    WindowRectangle->right=a.x;
    WindowRectangle->bottom=a.y;
}

maybe have some errors too :(

void Center()
        {
            RECT frm,frmparent;
            GetWindowRect(GetParent(hwnd), &frmparent);
            ScreenToClient(GetParent(hwnd),&frmparent);

            GetWindowRect(hwnd, &frm);
            ScreenToClient(hwnd,&frm);


            LONG x=frmparent.right/2 - (frm.right-frm.left)/2;
            LONG y=100;


            SetWindowPos(hwnd,0,x,y,0,0, SWP_NOSIZE |  SWP_NOZORDER);
        }

i'm trying calculate the x and y positions for the control. but i'm getting a big values even with 7 digits :(
can anyone correct me please?

Recommended Answers

All 2 Replies

i was geting wrong parent. now see these code:

enum ControlPositions
{
    CenterCenter=0,
    LeftCenter=1,
    LeftTop=2,
    LeftBottom=3,
    CenterTop=4,
    CenterBottom=5,
    RightTop=6,
    RightCenter=7,
    RightBottom=8,
    LeftRightTop=9,
    LeftRightBottom=10,
    LeftTopBottom=11
};

void ControlPosition(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) //i added these property when the form is created and when the parent it's changed
            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);
}

these function works greate for WS_CHILD controls. but when it's not WS_CHILD and ControlPosition is 1, i see the control, on x, more pixels(maybe 4) on left. how can i correct these?

In Windows they have nice thing for that...
It gets scaled very nice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.