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

📄 diagrameditor.cpp

📁 So you wanted to add a forms editor to your application? A dialog editor? Something that allows draw
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	bmp.CreateCompatibleBitmap( &outputdc, rect.right, rect.bottom );
	CBitmap* oldbmp = dc.SelectObject( &bmp );

	// Painting
	EraseBackground( &dc, rect );

	dc.SetWindowOrg( sih.nPos, siv.nPos );

	Draw( &dc, totalRect );

	// Blit the memory CDC to screen
	outputdc.BitBlt( 0, 0, rect.right, rect.bottom, &dc, sih.nPos, siv.nPos, SRCCOPY );
	dc.SelectObject( oldbmp );

}

void CDiagramEditor::Draw( CDC* dc, CRect rect ) const
/* ============================================================
	Function :		CDiagramEditor::Draw
	Description :	Calls a series of (virtual) functions to 
					draw to "dc". "rect" is the total rectangle 
					to draw to.
	Access :		Public

	Return :		void
	Parameters :	CDC* dc		-	The "CDC" to draw to.
					CRect rect	-	The complete rectangle 
									(including non-visible areas)
					
	Usage :			Should not normally be called from user code. 
					Can be called to draw the complete window. 
					Can be overriden to change drawing order.

   ============================================================*/
{

	double zoom = GetZoom();

	DrawBackground( dc, rect, zoom );

	if( m_grid )
		DrawGrid( dc, rect, zoom );

	if( m_margin )
		DrawMargins( dc, rect, zoom );

	DrawObjects( dc, zoom );

	if( m_bgResize && m_bgResizeSelected )
		DrawSelectionMarkers( dc );

	if( GetPanning() )
		DrawPanning( dc );

}

void CDiagramEditor::Print( CDC* dc, CRect rect, double zoom ) 
/* ============================================================
	Function :		CDiagramEditor::Print
	Description :	Prints the editor to printer or print a 
					preview.
	Access :		Public

	Return :		void
	Parameters :	CDC* dc		-	Printer- or preview "CDC" to 
									draw to.
					CRect rect	-	Total rect of editor.
					double zoom	-	Desired zoom value.
					
	Usage :			The function should be called to print the 
					editor to a printer or print preview. This 
					is because the zoom will most likely be set 
					to something different from the editor itself. 
					A good zoom factor can be calculated from 
					the difference between the screen- and 
					printer resolution:

					"printerDC.GetDeviceCaps( LOGPIXELSX ) / screenDC.GetDeviceCaps( LOGPIXELSX )"

					The grid, margin and selection markers are 
					not printed.
					Can be overriden to change drawing order, or 
					add drawing of the grid etc.
   ============================================================*/
{

	UnselectAll();
	DrawBackground( dc, rect, zoom );
	DrawObjects( dc, zoom );

}

BOOL CDiagramEditor::OnEraseBkgnd( CDC* ) 
/* ============================================================
	Function :		CDiagramEditor::OnEraseBkgnd
	Description :	Handles the "WM_ERASEBKGND" message. Handled 
					to avoid flicker - the editor is completely 
					redrawn in "OnPaint".
	Access :		Protected

	Return :		BOOL	-	
	Parameters :	CDC*	-	
					
	Usage :			Called from MFC. Do not call from code.

   ============================================================*/
{

	return TRUE;

}

/////////////////////////////////////////////////////////////////////////////
// CDiagramEditor painting virtuals

void CDiagramEditor::EraseBackground( CDC* dc, CRect rect ) const
/* ============================================================
	Function :		CDiagramEditor::EraseBackground
	Description :	Draws the non-client background
	Access :		Protected

	Return :		void
	Parameters :	CDC* dc		-	"CDC" to draw to.
					CRect rect	-	Total rect to draw to.
					
	Usage :			Virtual. Can be overridden in a derived class 
					to erase the non-client area.

   ============================================================*/
{

	dc->FillSolidRect( rect, m_nonClientBkgndCol );

}

void CDiagramEditor::DrawBackground( CDC* dc, CRect rect, double /*zoom*/) const
/* ============================================================
	Function :		CDiagramEditor::DrawBackground
	Description :	Draws the virtual background
	Access :		Protected

	Return :		void
	Parameters :	CDC* dc			-	"CDC" to draw to.
					CRect rect		-	Total rect to draw to.
					double (zoom)	-	Current zoom level. 
										Coordinates can be 
										multiplied with this 
										value to get scaled.
					
	Usage :			Virtual. Can be overridden in a derived 
					class to draw the virtual paper area.

   ============================================================*/
{

	dc->FillSolidRect( &rect, m_bkgndCol );

}

void CDiagramEditor::DrawGrid( CDC* dc, CRect rect, double zoom ) const
/* ============================================================
	Function :		CDiagramEditor::DrawGrid
	Description :	Draws the grid
	Access :		Protected

	Return :		void
	Parameters :	CDC* dc		-	"CDC" to draw to.
					CRect rect	-	Total rect to draw to.
					double zoom	-	Current zoom level. 
									Coordinates can be 
									multiplied with this value 
									to get scaled.
					
	Usage :			Virtual. Can be overridden in a derived 
					class to draw the grid. Will not be called 
					if the grid is not visible.

   ============================================================*/
{

	COLORREF gridcol = GetGridColor();
	int gridstyle = GetGridPenStyle();

	CPen pen;
	pen.CreatePen( gridstyle, 1, gridcol );

	dc->SelectObject( &pen );

	// To avoid accumulating rounding errors, we don't
	// precalculate the grid size for the given zoom...

	int width = rect.Width();
	int height = rect.Height();

	int stepx = GetVirtualSize().cx / GetGridSize().cx;
	int stepy = GetVirtualSize().cy / GetGridSize().cy;

	// ...instead we calculate the position of each line.
	for( int x = 0 ; x <= stepx ; x++ )
	{
		dc->MoveTo( round( static_cast< double >( GetGridSize().cx * x ) * zoom ), 0 );
		dc->LineTo( round( static_cast< double >( GetGridSize().cx * x ) * zoom ), height );
	}

	for( int y = 0; y <= stepy ; y++ )
	{
		dc->MoveTo( 0, round( static_cast< double >( GetGridSize().cy * y ) * zoom ) );
		dc->LineTo( width, round( static_cast< double >( GetGridSize().cy * y ) * zoom ) );
	}

	dc->SelectStockObject( BLACK_PEN );

}

void CDiagramEditor::DrawMargins( CDC* dc, CRect rect, double zoom ) const
/* ============================================================
	Function :		CDiagramEditor::DrawMargins
	Description :	Draws the margins.
	Access :		Protected

	Return :		void
	Parameters :	CDC* dc		-	"CDC" to draw to.
					CRect rect	-	Total rect to draw to.
					double zoom	-	Current zoom level. 
									Coordinates can be 
									multiplied with this value 
									to get scaled.
					
	Usage :			Virtual. Can be overridden in a derived 
					class to draw the margins. Will not be 
					called if margins are not visible.

   ============================================================*/
{

	CPen pen;
	pen.CreatePen( PS_SOLID, 0, m_marginColor );
	dc->SelectObject( &pen );

	CPoint leftTop( rect.left + round( static_cast< double >( m_leftMargin ) * zoom ), rect.top + round( static_cast< double >( m_topMargin ) * zoom ) );
	CPoint leftBottom( rect.left + round( static_cast< double >( m_leftMargin ) * zoom ), rect.bottom - round( static_cast< double >( m_bottomMargin ) * zoom ) - 1 );
	CPoint rightTop( rect.right - round( static_cast< double >( m_rightMargin ) * zoom ) - 1, rect.top + round( static_cast< double >( m_topMargin ) * zoom ) );
	CPoint rightBottom( rect.right - round( static_cast< double >( m_rightMargin ) * zoom ) - 1, rect.bottom - round( static_cast< double >( m_bottomMargin ) * zoom ) - 1 );

	if( m_leftMargin )
	{
		dc->MoveTo( leftTop );
		dc->LineTo( leftBottom );
	}

	if( m_rightMargin )
	{
		dc->MoveTo( rightTop );
		dc->LineTo( rightBottom );
	}
	if( m_topMargin )
	{
		dc->MoveTo( leftTop );
		dc->LineTo( rightTop );
	}
	if( m_bottomMargin )
	{
		dc->MoveTo( leftBottom );
		dc->LineTo( rightBottom );
	}

	dc->SelectStockObject( BLACK_PEN );


}

void CDiagramEditor::DrawObjects( CDC* dc, double zoom ) const
/* ============================================================
	Function :		CDiagramEditor::DrawObjects
	Description :	Draws the object.
	Access :		Protected

	Return :		void
	Parameters :	CDC* dc		-	"CDC" to draw to.
					double zoom	-	Current zoom level. 
									Coordinates can be 
									multiplied with this value 
									to get scaled.
					
	Usage :			Virtual. Can be overridden in a derived 
					class to draw the data objects.

   ============================================================*/
{

	if( m_objs )
	{
		int count = 0;
		CDiagramEntity* obj;
		while( ( obj = m_objs->GetAt( count++ ) ) )
			obj->DrawObject( dc, zoom );
	}

}

void CDiagramEditor::DrawSelectionMarkers( CDC* dc ) const
/* ============================================================
	Function :		CDiagramEditor::DrawSelectionMarkers
	Description :	Draws the selection markers.
	Access :		Protected

	Return :		void
	Parameters :	CDC* dc	-	The "CDC" to draw to.
					
	Usage :			Virtual. Can be overridden in a derived 
					class to draw the selection markers in 
					another way than the default black 
					rectangles. The selection rects are 
					displayed if the editor has background 
					resizing enabled and the user clicks in 
					the resize area around the virtual page 
					border. Selection markers are displayed 
					to allow resizing of the virtual page 
					with the mouse. 

   ============================================================*/
{

	// Draw selection markers
	CRect rectSelect;

	dc->SelectStockObject( BLACK_BRUSH );
	rectSelect = GetSelectionMarkerRect( DEHT_TOPLEFT );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_TOPMIDDLE );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_TOPRIGHT );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_BOTTOMLEFT );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_BOTTOMMIDDLE );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_BOTTOMRIGHT );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_RIGHTMIDDLE );
	dc->Rectangle( rectSelect );

	rectSelect = GetSelectionMarkerRect( DEHT_LEFTMIDDLE );
	dc->Rectangle( rectSelect );

}

CRect CDiagramEditor::GetSelectionMarkerRect( UINT marker ) const
/* ============================================================
	Function :		CDiagramEditor::GetSelectionMarkerRect
	Description :	Returns the selection rect for marker
	Access :		Public

	Return :		CRect		-	The rect of the selection 
									marker.
	Parameters :	UINT marker	-	The marker to get the rect 
									for ("DEHT_"-constants 
									defined in DiagramEntity.h)
					
	Usage :			Virtual. Can be overridden in a derived 
					class to change the selection rects for 
					the virtual page. The selection rects are 
					displayed if the editor has background 
					resizing enabled and the user clicks in 
					the resize area around the virtual page 
					border. Selection markers are displayed 
					to allow resizing of the virtual page 
					with the mouse.
					"marker" can be one of the following:
						"DEHT_NONE" No hit-point
						"DEHT_BODY" Inside object body
						"DEHT_TOPLEFT" Top-left corner
						"DEHT_TOPMIDDLE" Middle top-side
						"DEHT_TOPRIGHT" Top-right corner
						"DEHT_BOTTOMLEFT" Bottom-left corner
						"DEHT_BOTTOMMIDDLE" Middle bottom-side
						"DEHT_BOTTOMRIGHT" Bottom-right corner
						"DEHT_LEFTMIDDLE" Middle left-side
						"DEHT_RIGHTMIDDLE" Middle right-side
   ============================================================*/
{

	CRect rect( 0, 0, round( static_cast< double >( GetVirtualSize().cx ) * GetZoom() ), 
						round( static_cast< double >( GetVirtualSize().cy ) * GetZoom() ) );

	CRect rectMarker;
	int horz = m_markerSize.cx / 2;
	int vert = m_markerSize.cy / 2;

	switch( marker )
	{
		case DEHT_TOPLEFT:
			rectMarker.SetRect( rect.left - horz, 
								rect.top - vert, 
								rect.left + horz, 
								rect.top + vert );
		break;

		case DEHT_TOPMIDDLE:
			rectMarker.SetRect( rect.left + ( rect.Width() / 2 ) - horz, 
								rect.top - vert, 
								rect.left + ( rect.Width() / 2 ) + horz, 
								rect.top + vert );
		break;

		case DEHT_TOPRIGHT:
			rectMarker.SetRect( rect.right - horz, 
								rect.top - vert, 
								rect.right + horz, 
								rect.top + vert );
		break;

		case DEHT_BOTTOMLEFT:
			rectMarker.SetRect( rect.left - horz, 
								rect.bottom - vert, 
								rect.left + horz, 
								rect.bottom + vert );
		break;

		case DEHT_BOTTOMMIDDLE:
			rectMarker.SetRect( rect.left + ( rect.Width() / 2 ) - horz, 
								rect.bottom - vert, 
								rect.left + ( rect.Width() / 2 ) + horz, 
								rect.bottom + vert );
		break;

		case DEHT_BOTTOMRIGHT:
			rectMarker.SetRect( rect.right - horz, 
								rect.bottom - vert, 
								rect.right + horz, 
								rect.bottom + vert );
		break;

		case DEHT_LEFTMIDDLE:
			rectMarker.SetRect( rect.left - horz, 
								rect.top + ( rect.Height() / 2 ) - vert, 
								rect.left + horz, 
								rect.top + ( rect.Height() / 2 ) + vert );
		break;

		case DEHT_RIGHTMIDDLE:
			rectMarker.SetRect( rect.right - horz, 
								rect.top + ( rect.Height() / 2 ) - vert, 
								rect.right + horz, 
								rect.top + ( rect.Height() / 2 ) + vert );
		break;
	}

	return rectMarker;

}

/////////////////////////////////////////////////////////////////////////////
// CDiagramEditor property accessors

void CDiagramEditor::SetVirtualSize( const CSize& size )
/* ============================================================
	Function :		CDiagramEditor::SetVirtualSize
	Description :	Sets the size of the virtual paper.
	Access :		Public

⌨️ 快捷键说明

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