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

📄 childfrm.cpp

📁 这是MFC经典问答书的光盘内容
💻 CPP
字号:
// ChildFrm.cpp : implementation of the CChildFrame class
//

#include "stdafx.h"
#include "LimitPosition.h"

#include "ChildFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChildFrame

IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
	//{{AFX_MSG_MAP(CChildFrame)
	ON_WM_WINDOWPOSCHANGING()
	ON_COMMAND(ID_POSITION_LIMIT, OnPositionLimit)
	ON_COMMAND(ID_POSITION_DONT_LIMIT, OnPositionDontLimit)
	ON_UPDATE_COMMAND_UI(ID_POSITION_LIMIT, OnUpdatePositionLimit)
	ON_UPDATE_COMMAND_UI(ID_POSITION_DONT_LIMIT, OnUpdatePositionDontLimit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// The following constants define our position constraints
// (same values as used in CMdiBackgroundWnd::OnEraseBkgnd() for
// painting the MDI Client area)
const int LEFT_MIN = 20;
const int LEFT_MAX = 220;

const int TOP_MIN = 50;
const int TOP_MAX = 100;

const int RIGHT_MIN = 300;
const int RIGHT_MAX = 500;

const int BOTTOM_MIN = 250;
const int BOTTOM_MAX = 300;

/////////////////////////////////////////////////////////////////////////////
// CChildFrame construction/destruction

CChildFrame::CChildFrame()
{
	m_bLimitPosition = TRUE;	
}

CChildFrame::~CChildFrame()
{
}

BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// Remove 'Maximize' caption button
	cs.style &= ~WS_MAXIMIZEBOX;

	// Enforce initial position
	cs.x = LEFT_MIN;
	cs.y = TOP_MIN;
	cs.cx = RIGHT_MIN - cs.x;
	cs.cy = BOTTOM_MIN - cs.y;

	return CMDIChildWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CChildFrame diagnostics

#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
	CMDIChildWnd::AssertValid();
}

void CChildFrame::Dump(CDumpContext& dc) const
{
	CMDIChildWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChildFrame message handlers

void CChildFrame::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{
	CMDIChildWnd::OnWindowPosChanging(lpwndpos);
	
	if( m_bLimitPosition && !IsIconic() )
	{
		// Verify left side position
		if( lpwndpos->x < LEFT_MIN )
		{
			// Adjust horizontal size before left side
			lpwndpos->cx -= LEFT_MIN - lpwndpos->x;

			// Adjust left side
			lpwndpos->x = LEFT_MIN;
		}
		
		if( lpwndpos->x > LEFT_MAX )
		{
			// Adjust horizontal size before left side
			lpwndpos->cx += lpwndpos->x - LEFT_MAX;

			// Adjust left side
			lpwndpos->x = LEFT_MAX;
		}

		// Verify top side position
		if( lpwndpos->y < TOP_MIN )
		{
			// Adjust vertical size before top side
			lpwndpos->cy -= TOP_MIN - lpwndpos->y;

			// Adjust top side
			lpwndpos->y = TOP_MIN;
		}
		
		if( lpwndpos->y > TOP_MAX )
		{
			// Adjust vertical size before top side
			lpwndpos->cy += lpwndpos->y - TOP_MAX;

			// Adjust top side
			lpwndpos->y = TOP_MAX;
		}

		// Verify right side position
		if( lpwndpos->x + lpwndpos->cx < RIGHT_MIN )
		{
			lpwndpos->cx = RIGHT_MIN - lpwndpos->x;
		}
		
		if( lpwndpos->x + lpwndpos->cx > RIGHT_MAX )
		{
			lpwndpos->cx = RIGHT_MAX - lpwndpos->x;
		}

		// Verify bottom side position
		if( lpwndpos->y + lpwndpos->cy < BOTTOM_MIN )
		{
			lpwndpos->cy = BOTTOM_MIN - lpwndpos->y;
		}
		
		if( lpwndpos->y + lpwndpos->cy > BOTTOM_MAX )
		{
			lpwndpos->cy = BOTTOM_MAX - lpwndpos->y;
		}
	}
}

void CChildFrame::OnPositionLimit() 
{
	m_bLimitPosition = TRUE;	
}

void CChildFrame::OnPositionDontLimit() 
{
	m_bLimitPosition = FALSE;	
}

void CChildFrame::OnUpdatePositionLimit(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio( m_bLimitPosition );
}

void CChildFrame::OnUpdatePositionDontLimit(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio( !m_bLimitPosition );
}

⌨️ 快捷键说明

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