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);
}
|