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

📄 shapesview.cpp

📁 A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
💻 CPP
字号:
#include "stdafx.h"
#include "Shapes.h"

#include "ShapesDoc.h"
#include "ShapesView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace localNS
{
	// DesignViewItems //////////////////////////////////////
	
	class ShapeItem : public SbjCore::Mvc::DesignView::Item
	{
	public:
		ShapeItem() :
			r(0,0,0,0),
			nBorderWidth(1),
			clrBorder(RGB(0,0,0)),
			clrFill((COLORREF)-1)
		{
		}
		virtual ~ShapeItem()
		{
		}
	protected:
		CRect r;
		int nBorderWidth;		
		COLORREF clrBorder;
		COLORREF clrFill;
	private:
		DECLARE_DYNCREATE(ShapeItem)
		
		virtual void OnDraw(CDC* pDC)
		{
			pDC;
			HANDLE hItem = GetModelItemHandle();
			SbjCore::Mvc::Model::Controller* pModelCtrlr = SbjCore::Mvc::Model::GetCurController();
			r = SbjCore::Mvc::Model::Rect::GetItemValue(pModelCtrlr, hItem);
			nBorderWidth = pModelCtrlr->GetItemAttrValue(hItem, _T("borderWidth"));
			clrBorder = pModelCtrlr->GetItemAttrValue(hItem, _T("borderRGB"));
			clrFill = pModelCtrlr->GetItemAttrValue(hItem, _T("fillRGB"));

		}
		virtual bool OnIsTrackable() const
		{
			return true;
		}
		
	};
	IMPLEMENT_DYNCREATE(ShapeItem, SbjCore::Mvc::DesignView::Item)

	
	
	class RectangleItem : public ShapeItem
	{
		DECLARE_DYNCREATE(RectangleItem)
		virtual void OnDraw(CDC* pDC)
		{
			ShapeItem::OnDraw(pDC);
			
			CPen pen(PS_SOLID, nBorderWidth, clrBorder);
			SbjCore::Utils::GDI::Object<CPen> objPen(pDC, &pen);
			LOGBRUSH lb = {BS_HOLLOW, 0, 0};
			CBrush brHollow;
			brHollow.CreateBrushIndirect(&lb);
			SbjCore::Utils::GDI::Object<CBrush> objBrush(pDC, &brHollow);

			if (clrFill != (COLORREF)-1)
			{
				CBrush brush;
				brush.CreateSolidBrush(clrFill);
				pDC->FillRect(r, &brush);
			}
			pDC->Rectangle(r);
		}
	};
	IMPLEMENT_DYNCREATE(RectangleItem, ShapeItem)


	class EllipseItem : public ShapeItem
	{
		DECLARE_DYNCREATE(EllipseItem)
		virtual void OnDraw(CDC* pDC)
		{
			ShapeItem::OnDraw(pDC);
			CPen pen(PS_SOLID, nBorderWidth, clrBorder);
			SbjCore::Utils::GDI::Object<CPen> objPen(pDC, &pen);
			CBrush br;
			br.CreateSolidBrush(clrFill);
			SbjCore::Utils::GDI::Object<CBrush> objBrush(pDC, &br);

			pDC->Ellipse(r);
		}
	};
	IMPLEMENT_DYNCREATE(EllipseItem, ShapeItem)
}

struct ShapesViewImpl : public SbjCore::Mvc::DesignView::Controller
{
	ShapesViewImpl()
	{
	 // must have a root for the design but it isn't drawn
		MapItemRTCToModelTypeName(_T("Drawing"), RUNTIME_CLASS(SbjCore::Mvc::DesignView::Item));
		MapItemRTCToModelTypeName(_T("Rectangle"), RUNTIME_CLASS(localNS::RectangleItem));
		MapItemRTCToModelTypeName(_T("Ellipse"), RUNTIME_CLASS(localNS::EllipseItem));
	}
	
	virtual ~ShapesViewImpl()
	{
	}
	
	virtual SbjCore::Utils::Menu::ItemRange OnPrepareCtxMenu( CMenu& ctxMenu )
	{
		SbjCore::Utils::Menu::ItemRange menuItems;

		menuItems.nFirst = (int)ctxMenu.GetMenuItemCount()-1;
		menuItems.nLast = menuItems.nFirst; 

		if (SbjCore::Utils::Menu::InsertSeparator(ctxMenu, menuItems.nLast))
		{
			menuItems.nLast++;
		}

		(void)ctxMenu.InsertMenu(0, MF_BYPOSITION, ID_CMDS_NEWELLIPSE, _T("New &Ellipse"));
		(void)ctxMenu.InsertMenu(0, MF_BYPOSITION, ID_CMDS_NEWRECTANGLE, _T("New &Rectangle"));
		

		menuItems = SbjCore::Mvc::DesignView::Controller::OnPrepareCtxMenu(ctxMenu);

		return menuItems;
	}
	
	
};

// ShapesView ///////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(ShapesView, t_Base)

BEGIN_MESSAGE_MAP(ShapesView, t_Base)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &t_Base::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &t_Base::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &ShapesView::OnFilePrintPreview)
END_MESSAGE_MAP()

ShapesView::ShapesView() :
	m_pImpl(new ShapesViewImpl)
{
	SetController(m_pImpl);
}

ShapesView::~ShapesView()
{
	try
	{
		delete m_pImpl;
	}
	catch(...)
	{
		ASSERT(FALSE);
	}
}

BOOL ShapesView::PreCreateWindow(CREATESTRUCT& cs)
{
	return t_Base::PreCreateWindow(cs);
}

void ShapesView::OnDraw(CDC* pDC)
{
	ShapesDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
	{
		return;
	}
	m_pImpl->Draw(pDC);
}
												

void ShapesView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL ShapesView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void ShapesView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	pDC;
}

void ShapesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

void ShapesView::OnRButtonUp(UINT nFlags, CPoint point)
{
	nFlags;
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void ShapesView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	pWnd;
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// ShapesView diagnostics

#ifdef _DEBUG
void ShapesView::AssertValid() const
{
	t_Base::AssertValid();
}

void ShapesView::Dump(CDumpContext& dc) const
{
	t_Base::Dump(dc);
}

ShapesDoc* ShapesView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ShapesDoc)));
	return (ShapesDoc*)m_pDocument;
}

#endif //_DEBUG


⌨️ 快捷键说明

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