Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

position panels evenly across window

Status
Not open for further replies.

prettitoni

Programmer
Apr 22, 2004
74
US
Hi there.

I've been struggling with this for a bit now and it might just be a quick algorithm and I'm missing something. I'm using VC++ 2005. I have 5 panels (all the same size) on a windows form and I want them positioned across the width of the form. I want one panel on the leftmost side, one panel on the rightmost side and the remaining 3 panels evenly distributed in between. The number of panels may change so I want to do this in a flexible way. Can someone please help me with the algorithm?

Thanks.
 
Unfortunately I do not have .Net installed at the moment so here's some MFC code you can modify to work.
Code:
UINT ids[] ={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5};
int numItems = 5;
	
RECT crec,itemRect;
CWnd* base = GetDlgItem(ids[0]);
if(base)
{
    base->GetWindowRect(&itemRect);
    ScreenToClient(&itemRect);

    GetClientRect(&crec);
    int clientWidth = (crec.right - crec.left);
    CWnd** lplpWnd = new CWnd*[numItems];
    for (int i = 0; i < numItems; i++)
    {
	lplpWnd[i] = GetDlgItem(ids[i]);
    }

    int totalItemWidth = ((itemRect.right - itemRect.left) * numItems);
    int Gap = ((clientWidth - totalItemWidth)/(numItems - 1));
    if(Gap > 0)
    {
	for(int n = 0; n < numItems; n++)
	{
	    lplpWnd[n]->MoveWindow((n*((itemRect.right - itemRect.left)+Gap)),
            itemRect.top,
	    (itemRect.right - itemRect.left),
	    (itemRect.bottom - itemRect.top));
	}
    }
    delete [] lplpWnd;
}
for example replace
Code:
movewindow
with
Code:
panels[n].Location = New Point(n*((itemRect.right - itemRect.left)+Gap)),panels[n].Top);
and
Code:
GetClientRect
with
Code:
panel1.ClientRectangle
I will post a windows forms version soon as I install VC++ .Net
 
Heres the same thing in windows forms.
Code:
int numItems = 5;

System::Windows::Forms::Panel* pannels[] = new System::Windows::Forms::Panel* [numItems];

pannels[0] = panel1;
pannels[1] = panel2;
pannels[2] = panel3;
pannels[3] = panel4;
pannels[4] = panel5;

int totalItemWidth = (pannels[0]->Width * numItems);
int Gap = ((ClientRectangle.Width - totalItemWidth)/(numItems - 1));
if(Gap > 0)
{
    for(int n = 0; n < numItems; n++)
    {
        pannels[n]->Location = Point((n*(pannels[0]->Width+Gap)),pannels[0]->Top);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top