⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controlpos2.cpp

📁 VCsuishu fudai de yixie chengxuyuanma
💻 CPP
字号:
//------------------------------------------------------------------------------
// ControlPos2.cpp
// 
//	CControlPos2 
//	Position controls on a form's resize 
//  
#include "StdAfx.h"
#include "ControlPos2.h"

//------------------------------------------------------------------------------
// CControlPos2::CControlPos2
// 
//	default constructor 
// 
//	Access: public
// 
//	Args:
//		CWnd* pParent				=	pointer to parent window
// 
//	Return:
//		none
// 
CControlPos2::CControlPos2(CWnd* pParent /* = NULL */)
{
	m_pParent = pParent;

	ResetControls();
}

//------------------------------------------------------------------------------
// CControlPos2::~CControlPos2
// 
//	default destructor -- It deletes all controls. 
// 
//	Access: public
// 
//	Args:
//		none
// 
//	Return:
//		none
// 
CControlPos2::~CControlPos2()
{
	ResetControls();
}

//------------------------------------------------------------------------------
// CControlPos2::SetParent
// 
//	This sets the parent window. It should be called from a CWnd's 
//    post-constructor function, like OnInitdialog or InitialUpdate. 
// 
//	Access: public
// 
//	Args:
//		CWnd* pParent	=	parent window
// 
//	Return:
//		none
// 
void CControlPos2::SetParent(CWnd* pParent)
{
	CRect rcParentOriginalSize;

	m_pParent = pParent;

	m_pParent->GetClientRect(rcParentOriginalSize);
	m_rectOriginalParent = rcParentOriginalSize;
}

//------------------------------------------------------------------------------
// CControlPos2::AddControl
// 
//	This adds a control to the internal list of controls in CControlPos.  
// 
//	Access: public
// 
//	Args:
//		CWnd* pControl				=	pointer to the control to be added
//		const DWORD& dwStyle		=  how the window should be moved -- see #define's
//                               in the header file
// 
//	Return:
//		BOOL 	=	TRUE if the control was added successfully, FALSE otherwise
// 
BOOL CControlPos2::AddControl(CWnd* pControl)
{
	BOOL fReturnValue = TRUE;

	if (pControl && m_pParent)
	{
		CRect rcOriginalButton;
		LPCONTROLDATA pstControl = new CONTROLDATA;
		pstControl->hControl = pControl->GetSafeHwnd();
		m_awndControls.Add(((CObject*)pstControl));
		pControl->GetWindowRect(rcOriginalButton);
		m_pParent->ScreenToClient(rcOriginalButton);
		pstControl->hOriginalButtonRect = rcOriginalButton;
	}
	else
	{
		fReturnValue = FALSE;
	}

	return (fReturnValue);
}

//------------------------------------------------------------------------------
// CControlPos2::AddControl
// 
//	This adds a control the internal list of controls in CControlPos. 
// 
//	Access: public
// 
//	Args:
//		const UINT& unId			=	ID of the control to add
//		const DWORD& dwStyle		=	how the window should be moved -- see #define's
//                               in the header file
// 
//	Return:
//		BOOL 	=	TRUE if the control was added successfully, FALSE otherwise
// 
BOOL CControlPos2::AddControl(const UINT& unId)
{
	CWnd* pControl;

	if (m_pParent)
	{
		pControl = m_pParent->GetDlgItem(unId);
		return (AddControl(pControl));
	}
	else
	{
		return (FALSE);
	}
}

//------------------------------------------------------------------------------
// CControlPos2::RemoveControl
// 
//	If a client ever wants to remove a control programmatically, this 
//    function will do it. 
// 
//	Access: public
// 
//	Args:
//		CWnd* pControl	=	pointer of the window who should be removed from 
//								the internal control list [ie: will not be repositioned]
// 
//	Return:
//		BOOL 	=	TRUE if the control was found [and deleted], FALSE otherwise
// 
BOOL CControlPos2::RemoveControl(CWnd* pControl)
{
	BOOL fReturnValue = FALSE;

	for (int i = 0; i < m_awndControls.GetSize(); i++)
	{
		LPCONTROLDATA pstControl = ((LPCONTROLDATA)m_awndControls.GetAt(i));
		
		if (pstControl->hControl == pControl->GetSafeHwnd())
		{
			m_awndControls.RemoveAt(i);
			delete pstControl;
			fReturnValue = TRUE;
			break;
		}
	}

	return (fReturnValue);
}

//------------------------------------------------------------------------------
// CControlPos2::RemoveControl
// 
//	If a client ever wants to remove a control programmatically, this 
//    function will do it. 
// 
//	Access: public
// 
//	Args:
//		const UINT& unId  =  ID of the control that should be removed from the
//                         internal control list [ie: will not be repositioned]
// 
//	Return:
//		BOOL 	=	TRUE if the control was found [and deleted], FALSE otherwise
// 
BOOL CControlPos2::RemoveControl(const UINT& unId)
{
	CWnd* pControl;

	if (m_pParent)
	{
		pControl = m_pParent->GetDlgItem(unId);
		return (RemoveControl(pControl));
	}
	else
	{
		return (FALSE);
	}
}

//------------------------------------------------------------------------------
// CControlPos2::ResetControls
// 
//	This function removes all controls from the CControlPos object 
// 
//	Access: public
// 
//	Args:
//		none
// 
//	Return:
//		none
// 
void CControlPos2::ResetControls(void)
{
	while (m_awndControls.GetSize() > 0)
	{
		int   nHighIdx = m_awndControls.GetUpperBound();
		LPCONTROLDATA pstControl = ((LPCONTROLDATA)m_awndControls.GetAt(nHighIdx));
		if (pstControl)
		{
			m_awndControls.RemoveAt(nHighIdx);
			delete pstControl;
		}
	}
}

//------------------------------------------------------------------------------
// CControlPos2::MoveControls
// 
//	This function takes care of moving all controls that have been added to 
//    the object [see AddControl].  This function should be called from the 
//    WM_SIZE handler-function [typically OnSize]. 
// 
//	Access: public
// 
//	Args:
//		none
// 
//	Return:
//		none
// 
void CControlPos2::MoveControls(void)
{
	if (m_pParent)
	{
		//--------------------------------------------------------------------
		// for each control that has been added to our object, we want to
		// check its style and move it based off of the parent control's
		// movements.
		// the thing to keep in mind is that when you resize a window, you
		// can resize by more than one pixel at a time. this is important
		// when, for example, you start with a width smaller than the
		// original width and you finish with a width larger than the
		// original width. you know that you want to move the control, but
		// by how much? that is why so many if's and calculations are made
		//
		int s = 0;
		s = m_awndControls.GetSize();
		for (int i = 0; i <m_awndControls.GetSize(); i++)
		{
			LPCONTROLDATA pstControl = ((LPCONTROLDATA)m_awndControls.GetAt(i));
			double scalex,scaley;
			CRect rcParentBounds;
			CRect rcOldBounds,rcNewBounds;
			//
			CWnd* pControl = m_pParent->FromHandle(pstControl->hControl);
			//
			m_pParent->GetClientRect(rcParentBounds);
			//
			rcOldBounds = pstControl->hOriginalButtonRect;
			scalex = (float)rcParentBounds.right /
								(float)m_rectOriginalParent.right;
			scaley = (float)rcParentBounds.bottom /
								(float)m_rectOriginalParent.bottom;
			rcNewBounds.left =(int)(rcOldBounds.left * scalex);
			rcNewBounds.right = (int)rcOldBounds.right * scalex;
			rcNewBounds.top = (int)rcOldBounds.top * scaley;
			rcNewBounds.bottom = (int)rcOldBounds.bottom * scaley;
		
			//
			m_rcNewBound = rcNewBounds;
			//
			pControl->MoveWindow(rcNewBounds);
		}
	}
}

CRect CControlPos2::GetNewBound()
{
	return m_rcNewBound ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -