// LayoutTabCtrl.cpp : 实现文件
//
#include "stdafx.h"
#include "AutoLayout.h"
/*
*创建一个子布局
*pane( 布局模式 )布局模式可以是横向(HORIZONTAL)或者是纵向(VERTICAL)
*/
CLayoutMgr::CPane& CLayoutMgr::pane( AutoLayoutPaneMode pm )
{
CPane * pPane = new CPane( pm );
ASSERT( pPane != NULL );
return * pPane;
}
/*
*设定具体某一个控件的布局
*item( 控件ID, 调整模式 ) 调整模式可以是:
* 自由 GREEDY 默认的,可以在任意方向上调整
* 绝对横向 ABSOLUTE_HORZ 只在纵向上调整
* 绝对纵向 ABSOLUTE_VERT 只在横向上调整
* 不调整 NORESIZE 保持控件原始大小,不予调整
*/
CLayoutMgr::CItem& CLayoutMgr::item( CWnd * pWnd, AutoLayoutResizeMode rm /* = GREEDY */ )
{
ASSERT ( pWnd != NULL );
CItem * pItem = new CItem( pWnd, rm );
ASSERT ( pItem != NULL );
return *pItem;
}
/*
*创建一个空白区域
*Growing( 布局模式, 相当于多少个可调空间 )
*参数1、布局模式可以是横向(HORIZONTAL)或者是纵向(VERTICAL)
* 指定该空白区域横向调整或纵向调整
*参数2、当为1时,相当于一个可调控件所占的大小
* 当为2时,相当于二个可调控件所占的大小
.....................................
*/
CLayoutMgr::CGrowing& CLayoutMgr::Growing( AutoLayoutPaneMode pm ,int nSize /* = 1 */ )
{
ASSERT ( nSize > 0 );
CGrowing * pGrowing = new CGrowing( pm, nSize );
ASSERT ( pGrowing != NULL );
return *pGrowing;
}
/*
*创建一个带有静态组控件的空间
*GroupPane( 静态组控件ID, 布局模式 )
*布局模式可以是横向(HORIZONTAL)或者是纵向(VERTICAL)
*/
CLayoutMgr::CGroupPane& CLayoutMgr::GroupPane( CWnd * pWnd, AutoLayoutPaneMode pm )
{
ASSERT ( pWnd != NULL );
CGroupPane * pPane = new CGroupPane( pWnd, pm );
ASSERT ( pPane != NULL );
return *pPane;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLayoutMgr::CPane::CPane( AutoLayoutPaneMode pm ) : m_pm( pm )
{
}
CLayoutMgr::CPane::~CPane()
{
while( m_ChildList.GetCount() )
{
delete m_ChildList.RemoveTail();
}
}
CLayoutMgr::CPane& CLayoutMgr::CPane::operator << ( CPane &pe )
{
m_ChildList.AddTail( &pe) ;
return *this;
}
CLayoutMgr::CPane& CLayoutMgr::CPane::operator << ( CItem &it )
{
m_ChildList.AddTail( &it );
return *this;
}
/*
*将一个空间调整到指定的位置和尺寸
*/
void CLayoutMgr::CPane::ResizeWindow( CRect rect )
{
POSITION pos;
CSize ms;
long gd;
gd = GetGreedyCount( m_pm ); //计算空间内有多少需要调整的控件
if( gd != 0 )
{
//计算需要调整的控件的大小 公式为:需要调整的控件的大小 = (空间总大小 - 空间内所有控件的固定大小 - 所需要的边框大小 ) / 需调整控件的数量
if( m_pm == HORIZONTAL )
gd = ( rect.Width() - GetFixedSize().cx - nBorder * ( m_ChildList.GetCount()-1 )) / gd;
else
gd = ( rect.Height() - GetFixedSize().cy - nBorder * ( m_ChildList.GetCount()-1 )) / gd;
}
for( pos = m_ChildList.GetHeadPosition(); pos; m_ChildList.GetNext( pos ))
{
CElement * pE = m_ChildList.GetAt( pos );
ms = pE->GetFixedSize(); //获取该控件(空间)所需要的最小空间尺寸
//调整该控件(空间)
if( m_pm == HORIZONTAL )
{
rect.right = rect.left + max( pE->GetGreedyCount( HORIZONTAL ) * gd, ms.cx );
pE->ResizeWindow( &rect );
rect.left = rect.right + nBorder;
}
else
{
rect.bottom = rect.top + max( pE->GetGreedyCount( VERTICAL ) * gd, ms.cy );
pE->ResizeWindow( &rect );
rect.top = rect.bottom + nBorder;
}
}
}
/*
*获取空间的最小需要尺寸
*/
CSize CLayoutMgr::CPane::GetFixedSize()
{
CSize mSize;
CSize sz( 0, 0 );
long minX = 0, minY = 0, minMaxX = 0, minMaxY = 0;
POSITION pos;
BOOL flag = FALSE;
for( pos = m_ChildList.GetHeadPosition(); pos; m_ChildList.GetNext( pos ))
{
CElement * pE = m_ChildList.GetAt( pos );
sz = pE->GetFixedSize();
if( pE->GetKind() == KIND_ITEM )
{
if( m_pm == VERTICAL && sz.cx == 0 )
{
flag = TRUE;
}
if( m_pm == HORIZONTAL && sz.cy == 0 )
{
flag = TRUE;
}
}
minX += sz.cx;
minY += sz.cy;
if( minMaxX < sz.cx )
minMaxX = sz.cx;
if( minMaxY < sz.cy )
minMaxY = sz.cy;
}
if( m_pm == HORIZONTAL )
mSize.SetSize( minX, flag ? 0 : minMaxY);
else
mSize.SetSize( flag ? 0 : minMaxX, minY );
return mSize;
}
/*
*获取空间内部需要调整的控件数量
*/
long CLayoutMgr::CPane::GetGreedyCount( AutoLayoutPaneMode pm )
{
long gd = 0;
POSITION pos;
for( pos = m_ChildList.GetHeadPosition(); pos; m_ChildList.GetNext( pos ))
{
CElement * pE = m_ChildList.GetAt( pos );
if( pm == m_pm )
gd += pE->GetGreedyCount( pm );
else
{
if( pE->GetGreedyCount( pm ))
return 1;
}
}
return gd;
}
void CLayoutMgr::CPane::ShowWindow( int nCmd /* = SW_SHOW */ )
{
POSITION pos;
for( pos = m_ChildList.GetHeadPosition(); pos; m_ChildList.GetNext( pos ))
{
CElement * pE = m_ChildList.GetAt( pos );
pE->ShowWindow( nCmd );
}
}
BOOL CLayoutMgr::CPane::IsWindowVisible()
{
if( m_ChildList.GetCount() == 0 )
return FALSE;
else
return ( m_ChildList.GetHead()->IsWindowVisible());
}
CLayoutMgr::ElementKind CLayoutMgr::CPane::GetKind()
{
return KIND_PANE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLayoutMgr::CItem::CItem( CWnd * pWnd, AutoLayoutResizeMode rm ) : m_pWnd( pWnd ), m_rm( rm )
{
}
CLayoutMgr::CItem::~CItem()
{
}
/*
*调整控件到指定的位置和尺寸
*/
void CLayoutMgr::CItem::ResizeWindow( CRect rect )
{
CRect rt;
m_pWnd->GetWindowRect( & rt );
if( m_rm & ABSOLUTE_VERT )
rect.bottom = rect.top + rt.Height();
if( m_rm & ABSOLUTE_HORZ )
rect.right = rect.left + rt.Width();
m_pWnd->MoveWindow( &rect, TRUE );
}
/*
*获取控件所需要的最小尺寸
*/
CSize CLayoutMgr::CItem::GetFixedSize()
{
CRect rt;
CSize mSize;
mSize.SetSize( 0, 0 );
m_pWnd->GetWindowRect( &rt );
if( m_rm & ABSOLUTE_VERT )
mSize.cy = rt.Height();
if( m_rm & ABSOLUTE_HORZ )
mSize.cx = rt.Width();
return mSize;
}
/*
*计算该控件是否在特定方向上是可调的
*/
long CLayoutMgr::CItem::GetGreedyCount( AutoLayoutPaneMode pm )
{
if( m_rm == GREEDY )
return 1;
if( m_rm == NORESIZE )
return 0;
if( pm == HORIZONTAL )
{
if( m_rm == ABSOLUTE_VERT )
return 1;
}
else
{
if( m_rm == ABSOLUTE_HORZ )
return 1;
}
return 0;
}
void CLayoutMgr::CItem::ShowWindow( int nCmd /* = SW_SHOW */ )
{
m_pWnd->ShowWindow( nCmd );
}
BOOL CLayoutMgr::CItem::IsWindowVisible()
{
return m_pWnd->IsWindowVisible();
}
CLayoutMgr::ElementKind CLayoutMgr::CItem::GetKind()
{
return KIND_ITEM;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLayoutMgr::CGrowing::CGrowing( AutoLayoutPaneMode pm, int nSize ) : CItem( NULL, GREEDY ), m_pm ( pm ), m_nSize( nSize )
{
m_nShowCmd = SW_SHOW;
}
CSize CLayoutMgr::CGrowing::GetFixedSize()
{
CSize mSize;
if( m_pm == HORIZONTAL )
{
m_rm = ABSOLUTE_VERT;
mSize.SetSize( 0, 1 );
}
else
{
m_rm = ABSOLUTE_HORZ;
mSize.SetSize( 1, 0 );
}
return mSize;
}
long CLayoutMgr::CGrowing::GetGreedyCount( AutoLayoutPaneMode pm )
{
if( pm == m_pm )
return m_nSize;
else
return 0;
}
void CLayoutMgr::CGrowing::ResizeWindow( CRect rect )
{
//不做任何事情
}
void CLayoutMgr::CGrowing::ShowWindow( int nCmd /* = SW_SHOW */ )
{
m_nShowCmd = nCmd;
}
BOOL CLayoutMgr::CGrowing::IsWindowVisible()
{
return ( m_nShowCmd == SW_SHOW );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLayoutMgr::CGroupPane::CGroupPane( CWnd * pWnd, AutoLayoutPaneMode pm ) : CPane( pm ), m_pWnd( pWnd )
{
}
CLayoutMgr::CGroupPane::~CGroupPane()
{
}
void CLayoutMgr::CGroupPane::ResizeWindow( CRect rt )
{
if( m_pWnd )
m_pWnd->MoveWindow( &rt ); //移动静态组控件到指定位
- 1
- 2
前往页