subwndcontainer.cpp

来自「基于WINDOWS mobile 的用于创建一个窗体和自定义试图的工程」· C++ 代码 · 共 254 行

CPP
254
字号
// SubWndContainer.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "SubWndContainer.h"

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


//---------------------------------------------------------------------------
//
//	CSubWndContainer
//
//---------------------------------------------------------------------------


CSubWndContainer::CSubWndContainer()
:	m_pSubWnd	(NULL),
	m_hFont		((HFONT)::GetStockObject(SYSTEM_FONT))
{
	m_cont.reserve(64);

	RegisterWindowClass();
}


CSubWndContainer::~CSubWndContainer()
{
	CSubWndCont::iterator	pos,
							end	= m_cont.end();

	for(pos = m_cont.begin(); pos != end; ++pos)
		delete (*pos);
}


BEGIN_MESSAGE_MAP(CSubWndContainer, CWnd)
	//{{AFX_MSG_MAP(CSubWndContainer)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP

	ON_MESSAGE(WM_SETFONT, OnSetFont)
END_MESSAGE_MAP()


//---------------------------------------------------------------------------
//
//	CSubWndContainer operations
//
//---------------------------------------------------------------------------


// CSubWndContainer::RegisterWindowClass
//
//		Registers the table window class
//
BOOL CSubWndContainer::RegisterWindowClass()
{
    WNDCLASS	wc;
    HINSTANCE	hInst = AfxGetInstanceHandle();

	//
	// Check if the class is already registered
	//
	if(!::GetClassInfo(hInst, WC_SUBWNDCONT, &wc))
	{
		//
		// Register the new class name
		//
		wc.style			= CS_DBLCLKS;
		wc.hIcon			= NULL;
		wc.hInstance		= hInst;
		wc.cbClsExtra		= 0;
		wc.cbWndExtra		= 0;
		wc.hbrBackground	= (HBRUSH) (COLOR_WINDOW + 1);
		wc.lpfnWndProc		= ::DefWindowProc;
		wc.lpszMenuName		= NULL;
		wc.lpszClassName	= WC_SUBWNDCONT;

		if(!::RegisterClass(&wc))
		{
			AfxThrowResourceException();
			return FALSE;
		}
	}

	return TRUE;
}


// CSubWndContainer::Create
//
//		Creates the sub window container
//
BOOL CSubWndContainer::Create(DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID)
{
	HWND		hWnd;
	HINSTANCE	hInst	= AfxGetInstanceHandle();
	BOOL		bOk		= FALSE;

	hWnd = ::CreateWindowEx(0, 
							WC_SUBWNDCONT,
							_T(""),
							dwStyle,
							rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
							*pParentWnd,
							(HMENU)nID,
							hInst,
							NULL);
	if(hWnd)
		bOk = SubclassWindow(hWnd);

	return bOk;
}


// CSubWndContainer::Add
//
//		Adds a new sub window
//
void CSubWndContainer::Add(CSubWnd* pSubWnd)
{
	if(pSubWnd)
	{
		pSubWnd->SetContainer(this);
		m_cont.push_back(pSubWnd);
	}
}


// CSubWndContainer::SubWndFromPoint
//
//		Find a sub window given a point
//
CSubWnd* CSubWndContainer::SubWndFromPoint(CPoint point)
{
	CSubWnd*	pSubWnd	= NULL;

	//
	// Try to optimize
	//
	if(m_pSubWnd && m_pSubWnd->GetRect().PtInRect(point))
	{
		pSubWnd = m_pSubWnd;
	}
	else
	{
		CSubWndCont::iterator	pos,
								end		= m_cont.end();
		bool					bFound	= false;

		for(pos = m_cont.begin(); !bFound && pos != end; ++pos)
		{
			if((*pos)->GetRect().PtInRect(point))
			{
				bFound	= true;
				pSubWnd	= *pos;
			}
		}
	}
	return pSubWnd;
}


//---------------------------------------------------------------------------
//
//	CSubWndContainer message handlers
//
//---------------------------------------------------------------------------


// CSubWndContainer::OnPaint
//
//		Paints the container and its children
//
void CSubWndContainer::OnPaint() 
{
	CPaintDC				dc(this);
	CSubWndCont::iterator	pos,
							end	= m_cont.end();
	HFONT					hFontOld;

	hFontOld = (HFONT)::SelectObject(dc, m_hFont);

	//
	// Paint all sub windows
	//
	for(pos = m_cont.begin(); pos != end; ++pos)
		if(dc.RectVisible(&(*pos)->GetRect()))
			(*pos)->Paint(dc);

	::SelectObject(dc, hFontOld);
}


// CSubWndContainer::OnLButtonDown
//
//		The window is being clicked
//
void CSubWndContainer::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_pSubWnd = SubWndFromPoint(point);
	if(m_pSubWnd)
		m_pSubWnd->Click(CLICK_DOWN, point);
}


// CSubWndContainer::OnLButtonUp
//
//		The user is releasing the stylus
//
void CSubWndContainer::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_pSubWnd)
		m_pSubWnd->Click(CLICK_UP, point);
}


// CSubWndContainer::OnLButtonDblClk
//
//		The user is double-clicking the stylus
//
void CSubWndContainer::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	m_pSubWnd = SubWndFromPoint(point);
	if(m_pSubWnd)
		m_pSubWnd->Click(CLICK_DOWN | CLICK_DOUBLE, point);
}


// CSubWndContainer::OnSetFont
//
//		Sets the font for the control
//
LRESULT CSubWndContainer::OnSetFont(WPARAM wParam, LPARAM lParam)
{
	m_hFont = (HFONT)wParam;

	if(lParam)
	{
		InvalidateRect(NULL, FALSE);
		UpdateWindow();
	}

	return 0;
}

⌨️ 快捷键说明

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