MFC_佈局

MFC沒有提供佈局功能,每次作界面都要在OnSize裏面把因此控件的大小,位置都設置一遍。代碼量很多,且大多重複,耗時又費力,故,藉助CBCGPDialog類,進行封裝。html

先看一下未封裝以前的OnSize:函數

void CXXXDlg::OnSize(UINT nType, int cx, int cy)
{
    GetDlgItem(IDC_STATIC)->GetWindowRect(rectTemp);
	ScreenToClient(rectTemp);
	nHeight = rectTemp.Height();
	nWidth = rectTemp.Width();
	rectTemp.left = 10;
	rectTemp.right = cx - 10 * 2;
	rectTemp.top = 15;
	rectTemp.bottom = cy - 15 * 2;
	GetDlgItem(IDC_STATIC)->MoveWindow(rectTemp);
}

下載安裝BCGControlbar16.1,親測好用。地址:http://download.csdn.net/detail/wangxiangdong_sl/4821726佈局

// resize.cpp : header file


#ifndef AFX_RESIZE_H__INCLUDED_
#define AFX_RESIZE_H__INCLUDED_

#include <afxtempl.h>
#include <afxdhtml.h>
#include <vector>
#include <BCGCBProInc.h>

#define IDC_SIZEICON 0x7FFF
//#include <wtypes.h>

/////////////////////////////////////////////////////////////////////////////
// CRealRect

typedef float REAL;

 struct __declspec(dllexport) REALRECT
{
	REAL left, top, right, bottom;
};

class __declspec(dllexport) CRealRect: public REALRECT
{
public:
	CRealRect();
	CRealRect(const RECT& rc);
	CRealRect(const REALRECT& rrc);
	CRealRect(REAL l, REAL t, REAL r, REAL b);

	void operator = (const RECT& rc);
	void operator = (const REALRECT& rrc);
	void operator += (const REALRECT& rrc);

	CRealRect operator + (const REALRECT& rrc);
	CRealRect operator & (const REALRECT& rrc);

	bool operator == (const REALRECT& rrc);
	bool operator != (const REALRECT& rrc);

	operator CRect();

	BOOL IsNormalized();
	REAL Width();
	REAL Height();
};

class  __declspec(dllexport)  CRealItem
{
public:
	CRealItem(CWnd* pwnd, const CRealRect& rrcSizing, CRect& rcWindow, BOOL bDeleteWnd,UINT ID);
	~CRealItem();

	CWnd*		m_pwnd;			// window
	CRealRect	m_rrcSizing;	// sizing option
	CRealRect	m_rrcWindow;	// last control size
	BOOL		m_bDeleteWnd;	// delete the window
	UINT        m_ID;


};

/////////////////////////////////////////////////////////////////////////////
// CResize

#define SZ_RESIZE(x)	CRealRect(0,0,x,x) // resize 
#define SZ_REPOS(x)		CRealRect(x,x,x,x) // reposition 
#define SZ_HORRESIZE(x)	CRealRect(0,0,x,0) // horizontal resize
#define SZ_HORREPOS(x)	CRealRect(x,0,x,0) // horizontal reposition
#define SZ_VERRESIZE(x)	CRealRect(0,0,0,x) // vertical resize
#define SZ_VERREPOS(x)	CRealRect(0,x,0,x) // vertical reposition
 
struct SIZING
{
	UINT id;
	REALRECT rrc;
};

class  __declspec(dllexport)  CResize
{
public:
	CResize(CWnd* pwnd, const UINT nFlags = 0);
	~CResize();

	/////////////////////////////////////////////////////////////////////
	//!	函數名:	  SetResize
	//	-----------------------------------------------------------------
	//!	功能描述:
	//	-----------------------------------------------------------------
	//!	參數: const UINT nID
	//!	參數: REAL left    對話框寬度變,控件左側變化百分比 0不變
	//!	參數: REAL top   
	//!	參數: REAL right   對話框寬度變,控件右側側變化百分比 0不變,1跟着百分百變,0.5變1半 0.6變60%....
	//!	參數: REAL bottom
	//
	//!	返回值: void
	//	-----------------------------------------------------------------
	//!	備註:
	/////////////////////////////////////////////////////////////////////
	void SetResize(const UINT nID, REAL left, REAL top, REAL right, REAL bottom);
	void SetResize(const UINT nID, const REALRECT& rrcSizing);
	void SetResize(SIZING arr[]);
	void SetResize(const SIZING arr[], int nCount);

	void SetMinSize(CSize& sz); // set the minimum size explicitly (initial size is the default)
	void SetMaxSize(CSize& sz); // set the maximum (no maximum as default)

	void SavePlacement(LPCTSTR pszSection); // save window placement to registry
	void LoadPlacement(LPCTSTR pszSection); // load saved window placement from registry

	//清除全部控件 不可移動 
	void ClearCtrlsResize();

	void SetMoveWindowRect(CRect rect);
protected: // methods
	void SetResize(CWnd* pwnd, const CRealRect& rrcSizing, CRect rcWindow);

	void Init(); // called from OnInitDialog or OnInitialUpdate
	void Size(); // called from OnSize
	void GetMinMaxInfo(MINMAXINFO* pMMI); // called from OnGetMinMaxInfo

protected: // flags
	enum EFlags
	{
		SZ_NOSIZEICON			= 0x01, // do not add size icon
		SZ_NOHORISONTAL			= 0x02,	// no horizontal resizing
		SZ_NOVERTICAL			= 0x04,	// no vertical resizing
		SZ_NOMINSIZE			= 0x08,	// do not require a minimum size
		SZ_NOCLIPCHILDREN		= 0x10,	// do not set clip children style
		SZ_NOTRANSPARENTGROUP	= 0x20,	// do not set transparent style for group boxes
	};
	BOOL HasFlag(EFlags eFlag);

public: // helper classes

	typedef std::vector<CRealItem*> CItemArray;

private: // helper methods
	BOOL Defer(HDWP, CRealItem*, int dx, int dy);

private: // helper data
	CWnd*		m_pwnd;			// the associative relation to the window to be resized
	UINT		m_nFlags;		// flags passed from constructor
	CItemArray	m_arrItems;		// array of controls
	CRect		m_rcWindow;		// last dialog size
	CSize		m_szMin; 		// smallest size allowed
	CSize		m_szMax;		// largest size allowed
	CStatic		m_scSizeIcon;	// size icon window
	CBitmap		m_bmSizeIcon;	// size icon bitmap
};

void AFXAPI DestructElements1(CRealItem** pp, int n);

class  __declspec(dllexport)  CResizeDialog : public CDialog, public CResize
{
public:
	CResizeDialog(const UINT nID, CWnd* pParent = 0, const UINT nFlags = 0);

protected: // message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI);
	DECLARE_MESSAGE_MAP()
};
class  __declspec(dllexport)  CResizeBCGDialog : public CBCGPDialog, public CResize
{
public:
	CResizeBCGDialog(const UINT nID, CWnd* pParent = 0, const UINT nFlags = 0);

protected: // message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI);
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
// CResizePropertyPage

class __declspec(dllexport)CResizePropertyPage : public CPropertyPage, public CResize
{
	DECLARE_DYNCREATE(CResizePropertyPage)
public:
	CResizePropertyPage(const UINT nTemplate = 0, const UINT nCaption = 0, const UINT nFlags = 0);

protected: // message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI);
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
// CResizePropertySheet

class __declspec(dllexport)CResizePropertySheet : public CPropertySheet, public CResize
{
public:
	CResizePropertySheet(const UINT nCaption, CWnd* pParent = 0, const UINT nSelectPage = 0, const UINT nFlags = 0);
	CResizePropertySheet(LPCTSTR pszCaption, CWnd* pParent = 0, const UINT nSelectPage = 0, const UINT nFlags = 0);

	friend CResizePropertyPage;

	DWORD m_dwAddStyle, m_dwRemStyle;

protected: // message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI);
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
// CResizeFormView

class __declspec(dllexport)CResizeFormView : public CFormView, public CResize
{
	DECLARE_DYNCREATE(CResizeFormView)
public:
	CResizeFormView(const UINT nID = 0, const UINT nFlags = 0);

protected: // message map functions
	virtual void OnInitialUpdate();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI);
	DECLARE_MESSAGE_MAP()
};



/////////////////////////////////////////////////////////////////////////////
// CResizeDHtmlDialog
class  __declspec(dllexport)  CResizeDHtmlDialog : public CDHtmlDialog, public CResize
{
public:
	CResizeDHtmlDialog(const UINT nID, UINT nHtmlResID=0,CWnd* pParent = 0);

protected: // message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO *lpMMI);
	DECLARE_MESSAGE_MAP()
};



#endif
// resize.cpp : implementation file

#include "stdafx.h"
#include "resize.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma warning(disable: 4355) // 'this' : used in base member initializer list

#ifndef OBM_SIZE
#define OBM_SIZE 32766
#endif

#define ENTRY_WINDOWPLACEMENT _T("WindowPlacement")

/////////////////////////////////////////////////////////////////////////////
// CResize

CResize::CResize(CWnd* pwnd, const UINT nFlags) : 
	m_pwnd(pwnd),
	m_nFlags(nFlags),
	m_szMin(0,0),
	m_szMax(0,0),
	m_rcWindow(0,0,0,0)
{

}

void CResize::SetResize(const UINT nID, const REALRECT& rrcSizing)
{
	CRealItem* pItem = 0;
	// search for an item with the given id
	for (INT_PTR i=m_arrItems.size()-1; i>=0; --i)
	{
		if (m_arrItems[i]->m_pwnd->GetDlgCtrlID() == (int) nID) pItem = m_arrItems[i];
	}
	// if we didn't find it then create one
	if (pItem == 0)
	{
		HWND hwnd;// = ::GetDlgItem(m_pwnd->m_hWnd, nID);
	//	CWnd* pwnd = m_pwnd->FromHandlePermanent(hwnd);
		m_pwnd->GetDlgItem(nID,&hwnd);

	//	HWND hwnd = GetDlgItem( m_pwnd->GetSafeHwnd() ,nID);
		CWnd* pwnd = m_pwnd->GetDlgItem(nID);//m_pwnd->FromHandlePermanent(hwnd);
		BOOL bPermanentWnd = (pwnd != 0);
		if (!bPermanentWnd)
		{
			// there is no permanent window, create one
			pwnd = new CWnd; // CItem handles the delete
			pwnd->Attach(hwnd);
		}

		CRect rcWindow;
		pwnd->GetWindowRect(rcWindow);
		m_pwnd->ScreenToClient(rcWindow);

		m_arrItems.push_back(pItem = new CRealItem(pwnd, rrcSizing, rcWindow, !bPermanentWnd,nID));

		if (!HasFlag(SZ_NOTRANSPARENTGROUP))
		{
			// if the control is a group box, then make it transparent
			TCHAR pszClassName[8];
			::GetClassName(hwnd, pszClassName, 8);
			DWORD dwStyle = pwnd->GetStyle();
			if (_tcsicmp(pszClassName, _T("Button")) == 0 && (dwStyle & BS_GROUPBOX) && !(dwStyle & WS_TABSTOP))
			{
				pwnd->ModifyStyleEx(0, WS_EX_TRANSPARENT, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
			}
		}
	}
	else
	{
		// the item already exists, just add the new sizing option
		pItem->m_rrcSizing += rrcSizing;
	}
	// we should only allow sizing within the rect {0,0,1,1}
	//ASSERT((CRealRect(0,0,1,1) & pItem->m_rrcSizing) == pItem->m_rrcSizing);
}


void CResize::ClearCtrlsResize()
{
	m_arrItems.clear();
}

void CResize::SetMoveWindowRect(CRect rect)
{
	m_rcWindow=rect;
}

void CResize::Init()
{
	// get the dialog size
	m_pwnd->GetWindowRect(m_rcWindow);
	if (!HasFlag(SZ_NOMINSIZE)) 
	{
		// set minimum size to current window size
		m_szMin.cx = m_rcWindow.Width(); 
		m_szMin.cy = m_rcWindow.Height(); 
	}

	// add the size icon
	if (!HasFlag(SZ_NOSIZEICON))
	{
		m_bmSizeIcon.LoadOEMBitmap(OBM_SIZE);
		m_scSizeIcon.Create(0, WS_CHILD | WS_VISIBLE | SS_BITMAP, CRect(0,0,16,16), m_pwnd, IDC_SIZEICON);
		m_scSizeIcon.SetBitmap(m_bmSizeIcon);

		CRect rcIcon, rcClient;
		m_pwnd->GetClientRect(rcClient);
		m_scSizeIcon.GetClientRect(rcIcon);
		m_scSizeIcon.SetWindowPos(&CWnd::wndTop, rcClient.right-rcIcon.Width(), rcClient.bottom-rcIcon.Height(), 0, 0, SWP_NOSIZE);

		SetResize(IDC_SIZEICON, SZ_REPOS(1));
	}
	if ( m_scSizeIcon.GetSafeHwnd() != NULL )
		m_scSizeIcon.ShowWindow(SW_HIDE);
}

void CResize::Size()
{
	if (m_arrItems.size() > 0)
	{
		CRect rcWindow;
		m_pwnd->GetWindowRect(rcWindow);

		int dx = rcWindow.Width() - m_rcWindow.Width();
		int dy = rcWindow.Height() - m_rcWindow.Height();

		for (INT_PTR i=0;  i<(int)m_arrItems.size(); i++)
		{

			CRealRect rrcItem = m_arrItems[i]->m_rrcWindow;
			rrcItem.left += dx * m_arrItems[i]->m_rrcSizing.left;
			rrcItem.top += dy * m_arrItems[i]->m_rrcSizing.top;
			rrcItem.right += dx * m_arrItems[i]->m_rrcSizing.right;
			rrcItem.bottom += dy * m_arrItems[i]->m_rrcSizing.bottom;

			if (rrcItem != m_arrItems[i]->m_rrcWindow)
			{
				m_arrItems[i]->m_rrcWindow = rrcItem;

				CWnd* pWnd = NULL;
				if( m_pwnd != NULL && m_arrItems[i]->m_ID > 0 )
					pWnd = m_pwnd->GetDlgItem( m_arrItems[i]->m_ID);
				else
					pWnd = m_arrItems[i]->m_pwnd;

				if ( pWnd != NULL )
				{
					int x = (int) rrcItem.left, y = (int) rrcItem.top;
					int cx = (int) rrcItem.Width(), cy = (int) rrcItem.Height();
					pWnd->MoveWindow(x, y, cx, cy,TRUE);
				}
			}
		}
		m_rcWindow = rcWindow;
	}
}

BOOL CResize::Defer(HDWP hdwp, CRealItem* pItem, int dx, int dy)
{
	CRealRect rrcItem = pItem->m_rrcWindow;
	rrcItem.left += dx * pItem->m_rrcSizing.left;
	rrcItem.top += dy * pItem->m_rrcSizing.top;
	rrcItem.right += dx * pItem->m_rrcSizing.right;
	rrcItem.bottom += dy * pItem->m_rrcSizing.bottom;
	
	if (rrcItem != pItem->m_rrcWindow)
	{
		pItem->m_rrcWindow = rrcItem;
	
		HWND hwnd = pItem->m_pwnd->m_hWnd;
		if (::IsWindow(hwnd))
		{
			int x = (int) rrcItem.left, y = (int) rrcItem.top;
			int cx = (int) rrcItem.Width(), cy = (int) rrcItem.Height();
			if (DeferWindowPos(hdwp, hwnd, 0, x, y, cx, cy, SWP_NOZORDER) ==  0)
			{
				TRACE("DeferWindowPos failed for ID %i\n", GetDlgCtrlID(hwnd));
				return FALSE;
			}
		}
	}
	return TRUE;
}

void CResize::GetMinMaxInfo(MINMAXINFO* pMMI)
{
	if (!m_rcWindow.IsRectNull())
	{
		if (HasFlag(SZ_NOHORISONTAL))
		{
			pMMI->ptMaxTrackSize.x = pMMI->ptMaxSize.x = m_rcWindow.Width();
		}
		if (HasFlag(SZ_NOVERTICAL))
		{
			pMMI->ptMaxTrackSize.y = pMMI->ptMaxSize.y = m_rcWindow.Height();
		}
	}
	if (m_szMin.cx != 0) pMMI->ptMinTrackSize.x = m_szMin.cx;
	if (m_szMin.cy != 0) pMMI->ptMinTrackSize.y = m_szMin.cy;
	if (m_szMax.cx != 0) pMMI->ptMaxTrackSize.x = m_szMax.cx;
	if (m_szMax.cy != 0) pMMI->ptMaxTrackSize.y = m_szMax.cy;
}

void CResize::LoadPlacement(LPCTSTR pszSection)
{
	UINT nBytes = 0;
	BYTE* pBytes = 0;
	AfxGetApp()->GetProfileBinary(pszSection, ENTRY_WINDOWPLACEMENT, &pBytes, &nBytes);
	if (nBytes == sizeof(WINDOWPLACEMENT))
	{
		m_pwnd->SetWindowPlacement((WINDOWPLACEMENT*) pBytes);
	}
	if (pBytes && nBytes) delete[] pBytes;

}

void CResize::SavePlacement(LPCTSTR pszSection)
{
	WINDOWPLACEMENT wp;
	m_pwnd->GetWindowPlacement(&wp);
	AfxGetApp()->WriteProfileBinary(pszSection, ENTRY_WINDOWPLACEMENT, (BYTE*) &wp, sizeof(wp));
}

void AFXAPI DestructElements1(CRealItem** pp, int n) 
{ 
	while (n--) delete *pp++; 
}

/////////////////////////////////////////////////////////////////////////////
// CResizeDialog

CResizeDialog::CResizeDialog(const UINT nID, CWnd* pParent, const UINT nFlags) : 
CDialog(nID, pParent),
CResize(this, nFlags)
{

}

BEGIN_MESSAGE_MAP(CResizeDialog, CDialog)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

BOOL CResizeDialog::OnInitDialog() 
{
	ASSERT(GetStyle() & WS_THICKFRAME); // Needs resizable frame

	CDialog::OnInitDialog();
	CResize::Init();

	return TRUE;
}

void CResizeDialog::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
	CResize::GetMinMaxInfo(pMMI);
}

void CResizeDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);
	CResize::Size();
}


/////////////////////////////////////////////////////////////////////////////
// CResizeDialog
CResizeBCGDialog::CResizeBCGDialog(const UINT nID, CWnd* pParent, const UINT nFlags) : 
CBCGPDialog(nID, pParent),
CResize(this, nFlags)
{

}

BEGIN_MESSAGE_MAP(CResizeBCGDialog, CBCGPDialog)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

BOOL CResizeBCGDialog::OnInitDialog() 
{
	//ASSERT(GetStyle() & WS_THICKFRAME); // Needs resizable frame
	
	CBCGPDialog::OnInitDialog();
	EnableVisualManagerStyle(TRUE);
	CResize::Init();

	return TRUE;
}

void CResizeBCGDialog::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
	CResize::GetMinMaxInfo(pMMI);
}

void CResizeBCGDialog::OnSize(UINT nType, int cx, int cy)
{
	CBCGPDialog::OnSize(nType, cx, cy);
	CResize::Size();
}

/////////////////////////////////////////////////////////////////////////////
// CResizePropertyPage

IMPLEMENT_DYNCREATE(CResizePropertyPage, CPropertyPage)

CResizePropertyPage::CResizePropertyPage(const UINT nTemplate, const UINT nCaption, const UINT nFlags) : 
	CPropertyPage(nTemplate, nCaption),
	CResize(this, nFlags | SZ_NOSIZEICON)
{

}

BEGIN_MESSAGE_MAP(CResizePropertyPage, CPropertyPage)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

BOOL CResizePropertyPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	CResize::Init();

	return TRUE;
}

void CResizePropertyPage::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
	CResize::GetMinMaxInfo(pMMI);
}

void CResizePropertyPage::OnSize(UINT nType, int cx, int cy)
{
	CPropertyPage::OnSize(nType, cx, cy);
	CResize::Size();
}

/////////////////////////////////////////////////////////////////////////////
// CResizePropertySheet

CResizePropertySheet::CResizePropertySheet(const UINT nCaption, CWnd* pParent, const UINT nSelectPage, const UINT nFlags) :
	CPropertySheet(nCaption, pParent, nSelectPage),
	CResize(this, nFlags)
{
	m_dwRemStyle = m_dwAddStyle = 0;
}

CResizePropertySheet::CResizePropertySheet(LPCTSTR pszCaption, CWnd* pParent, const UINT nSelectPage, const UINT nFlags) :
	CPropertySheet(pszCaption, pParent, nSelectPage),
	CResize(this, nFlags)
{
	m_dwRemStyle = m_dwAddStyle = 0;
}

BEGIN_MESSAGE_MAP(CResizePropertySheet, CPropertySheet)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

BOOL CResizePropertySheet::OnInitDialog() 
{
	// add user set style and resizable frame
	ModifyStyle(m_dwRemStyle, m_dwAddStyle | WS_THICKFRAME);

	CPropertySheet::OnInitDialog();

	if (!HasFlag(SZ_NOSIZEICON))
	{
		// the size icon is too close to the buttons, so inflate the sheet
		CRect rc;
		GetWindowRect(rc);
		rc.InflateRect(1,1);
		MoveWindow(rc);
	}

	// add sizing entries to the system menu
	if (CMenu* pSysMenu = GetSystemMenu(FALSE))
	{
		CString szMaximize, szMinimize, szSize, szRestore;
		CWnd* pwndTop = this;
		// try to get the strings from the topmost window
		for (; pwndTop->GetParent(); pwndTop = pwndTop->GetParent());
		if (CMenu* pTopSysMenu = pwndTop->GetSystemMenu(FALSE))
		{
			pTopSysMenu->GetMenuString(SC_MAXIMIZE, szMaximize, MF_BYCOMMAND);
			pTopSysMenu->GetMenuString(SC_MINIMIZE, szMinimize, MF_BYCOMMAND);
			pTopSysMenu->GetMenuString(SC_SIZE, szSize, MF_BYCOMMAND);
			pTopSysMenu->GetMenuString(SC_RESTORE, szRestore, MF_BYCOMMAND);
		}
		// if we din't get the strings then set them to the English defaults
		if (szMaximize.IsEmpty()) szMaximize = _T("Ma&ximize");
		if (szMinimize.IsEmpty()) szMinimize = _T("Mi&nimize");
		if (szSize.IsEmpty()) szSize = _T("&Size");
		if (szRestore.IsEmpty()) szRestore = _T("&Restore");

		pSysMenu->InsertMenu(1, MF_BYPOSITION | MF_SEPARATOR, 0, (LPCTSTR) 0);
		pSysMenu->InsertMenu(1, MF_BYPOSITION | MF_STRING, SC_MAXIMIZE, szMaximize);
		pSysMenu->InsertMenu(1, MF_BYPOSITION | MF_STRING, SC_MINIMIZE, szMinimize);
		pSysMenu->InsertMenu(1, MF_BYPOSITION | MF_STRING, SC_SIZE, szSize);
		pSysMenu->InsertMenu(0, MF_BYPOSITION | MF_STRING, SC_RESTORE, szRestore);
	}

	CResize::Init();

	// set sheet sizings
	SetResize(IDOK, SZ_REPOS(1));
	SetResize(IDCANCEL, SZ_REPOS(1));
	SetResize(IDHELP, SZ_REPOS(1));
	SetResize(ID_APPLY_NOW, SZ_REPOS(1));
	SetResize(AFX_IDC_TAB_CONTROL, SZ_RESIZE(1));

	// set page sizings
	CRect rcPage;
	GetActivePage()->GetWindowRect(rcPage);
	ScreenToClient(rcPage);
	for (int i=0; i<GetPageCount(); i++)
	{
		SetResize(GetPage(i), SZ_RESIZE(1), rcPage);
	}
	return TRUE;
}

void CResizePropertySheet::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
	CResize::GetMinMaxInfo(pMMI);
}

void CResizePropertySheet::OnSize(UINT nType, int cx, int cy)
{
	CPropertySheet::OnSize(nType, cx, cy);
	CResize::Size();
}

/////////////////////////////////////////////////////////////////////////////
// CResizeFormView

IMPLEMENT_DYNCREATE(CResizeFormView, CFormView)

CResizeFormView::CResizeFormView(const UINT nID, const UINT nFlags) : 
	CFormView(nID),
	CResize(this, nFlags | SZ_NOSIZEICON)
{

}

BEGIN_MESSAGE_MAP(CResizeFormView, CFormView)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

void CResizeFormView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();

	ResizeParentToFit(FALSE);

	SetScrollSizes(MM_TEXT, CSize(0,0)); // disable the scroll bars

	CResize::Init();
}

void CResizeFormView::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
	// Notice that WM_GETMINMAXINFO must be handled in the parent frame
	CResize::GetMinMaxInfo(pMMI);
}

void CResizeFormView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);
	CResize::Size();
}

/////////////////////////////////////////////////////////////////////////////
// CResizeDHtmlDialog

CResizeDHtmlDialog::CResizeDHtmlDialog(const UINT nID,UINT nHtmlResID/*=0*/,CWnd* pParent/* = 0*/) : 
CDHtmlDialog(nID, nHtmlResID,pParent),
CResize(this)
{

}

BEGIN_MESSAGE_MAP(CResizeDHtmlDialog, CDHtmlDialog)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

BOOL CResizeDHtmlDialog::OnInitDialog() 
{
	ASSERT(GetStyle() & WS_THICKFRAME); // Needs resizable frame

	CDHtmlDialog::OnInitDialog();
	CResize::Init();

	return TRUE;
}

void CResizeDHtmlDialog::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
	CResize::GetMinMaxInfo(pMMI);
}

void CResizeDHtmlDialog::OnSize(UINT nType, int cx, int cy)
{
	CDHtmlDialog::OnSize(nType, cx, cy);
	CResize::Size();
}



/////////////////////////////////////////////////////////////////////////////
// CRealRect
 CRealRect::CRealRect()
{
}

 CRealRect::CRealRect(const RECT& rc)
{
	(operator =)(rc);
}

 CRealRect::CRealRect(const REALRECT& rrc)
{
	(operator =)(rrc);
}

 CRealRect::CRealRect(REAL l, REAL t, REAL r, REAL b)
{
	left = l;
	top = t;
	right = r;
	bottom = b;
}

 void CRealRect::operator = (const RECT& rc) 
{ 
	left = (REAL) rc.left; 
	top = (REAL) rc.top; 
	right = (REAL) rc.right; 
	bottom = (REAL) rc.bottom; 
}

 void  CRealRect::operator = (const REALRECT& rrc) 
{ 
	left = rrc.left; 
	top = rrc.top; 
	right = rrc.right; 
	bottom = rrc.bottom; 
}

 void  CRealRect::operator += (const REALRECT& rrc) 
{ 
	left += rrc.left; 
	top += rrc.top; 
	right += rrc.right; 
	bottom += rrc.bottom; 
}

 CRealRect CRealRect::operator + (const REALRECT& rrc) 
{ 
	return CRealRect(left + rrc.left, top + rrc.top, right + rrc.right, bottom += rrc.bottom); 
}

 BOOL CRealRect::IsNormalized()
{
	return left<=right && top<=bottom;
}

 CRealRect CRealRect::operator & (const REALRECT& rrc)
{ 
	ASSERT(IsNormalized() && ((CRealRect&) rrc).IsNormalized());

	CRealRect rrcI(max(left,rrc.left), max(top,rrc.top), 
		min(right,rrc.right), min(bottom,rrc.bottom)); 

	// only intersection if resulting rect is normalized
	return (rrcI.IsNormalized()) ? rrcI : CRealRect(0,0,0,0);
}

 bool CRealRect::operator == (const REALRECT& rrc)
{ 
	return left==rrc.left && top==rrc.top && right==rrc.right && bottom==rrc.bottom; 
}

 bool CRealRect::operator != (const REALRECT& rrc) 
{ 
	return !operator==(rrc); 
}

 CRealRect::operator CRect() 
{ 
	return CRect((int) left, (int) top, (int) right, (int) bottom); 
}

 REAL CRealRect::Width() 
{ 
	return right-left; 
}

 REAL CRealRect::Height() 
{ 
	return bottom-top; 
}

/////////////////////////////////////////////////////////////////////////////
// CResize

 void CResize::SetMinSize(CSize& sz) 
{ 
	m_szMin = sz; 
}

 void CResize::SetMaxSize(CSize& sz) 
{ 
	m_szMax = sz; 
}

 BOOL CResize::HasFlag(EFlags eFlag)
{
	return (m_nFlags & eFlag) != 0;
}

 void CResize::SetResize(const UINT nID, REAL left, REAL top, REAL right, REAL bottom)
{
	SetResize(nID, CRealRect(left, top, right, bottom));
}

 // 傳入數量 
 void CResize::SetResize(const SIZING arr[], int nCount)
{
	for (int i=0; i</*sizeof(arr)*/nCount; i++)
	{
		const SIZING& sz = arr[i];
		SetResize(sz.id, sz.rrc.left, sz.rrc.top, sz.rrc.right, sz.rrc.bottom);
	}
}

void CResize::SetResize(CWnd* pwnd, const CRealRect& rrcSizing, CRect rcWindow)
{
	m_arrItems.push_back(new CRealItem(pwnd, rrcSizing, rcWindow, FALSE,0));
}

 CResize::~CResize()
 {
	 for (INT_PTR i=m_arrItems.size(); i--; )
	 {
		 delete m_arrItems[i];
	 }
	 m_arrItems.clear();
 }

 CRealItem::CRealItem(CWnd* pwnd, const CRealRect& rrcSizing, CRect& rcWindow, BOOL bDeleteWnd,UINT ID) : 
m_pwnd(pwnd),
m_rrcSizing(rrcSizing),
m_rrcWindow(rcWindow),
m_bDeleteWnd(bDeleteWnd),
m_ID(ID)
{

}

 CRealItem::~CRealItem()
{
	if (m_bDeleteWnd)
	{
		m_pwnd->Detach();
		delete m_pwnd; 
	}
}

封裝後用法:ui

SetResize(IDC_STATIC		,0  ,0  ,1  ,1  );
相關文章
相關標籤/搜索