Home | Products | Download | Purchase | Support 


   

CustomDraw Sample

 

The CustomDraw sample demonstrates the how to create a new style double complex ellipse drawing shape on the canvas, you can create this shape with codes or drawing with mouse. It demonstrates of how to using the event features of E-XD++, override the mouse down or up events.

Steps:

1. Add new drawing action flags:

enum my_newaction
{
// Draw pie state.
State_DoubleEllipse = 100,
State_DoubleEllipseStart,
State_DoDoubleEllipse,
State_DoubleEllipseLine1Start,
State_DoDoubleEllipseLine1
};
2. Add the following new methods for drawing:
// Draw Track Pie
virtual void OnDrawTrackDoubleEllipse(CPoint point);

void CCustomDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GetCurrentModel()->IsDesignMode())
	{
		if(m_action_state == State_DoubleEllipse ||
			m_action_state == State_DoubleEllipseStart ||
			m_action_state == State_DoDoubleEllipse ||
			m_action_state == State_DoubleEllipseLine1Start)
		{
			m_pSelectedHelpLine = NULL;
			CPoint local = point;
			if(m_action_state != State_DoInfront_Shape &&
				m_action_state != State_DoBehind_Shape)
			{
				if(m_pCurHilightShape != NULL)
				{
					CFODrawShapeList lstUpdate;
					lstUpdate.AddTail(m_pCurHilightShape);
					
					m_pCurHilightShape = NULL;
					
					UpdateShapes(&lstUpdate);
				}
			}
			
			m_bResetSelect = FALSE;
			m_ptCurrentDevice = point;
			m_ptCurrentLog = m_ptCurrentDevice;
			FODPtoLP(&m_ptCurrentLog,1);
			if(m_bTabOrder)
			{
				DoChangeTabOrder(local);
				return;
			}
			
			CPoint ptDev = point;
			CPoint ptLog(ptDev);
			FODPtoLP(&ptLog, 1);
			
			BOOL bEnableGridSnap = TRUE;
			if(m_bEnableNearest)
			{
				CPoint ptNear;
				if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,ptLog,TRUE))
				{
					ptLog = ptNear;
					ptDev = ptLog;
					FOLPtoDP(&ptDev,1);
					
					bEnableGridSnap = FALSE;
				}
			}
			
			if(bEnableGridSnap)
			{
				if (GetCurrentModel()->IsGrid())
				{
					ptLog = FindCloseGridPoint(ptLog);
					ptDev = ptLog;
					FOLPtoDP(&ptDev,1);
				}
			}
			
			m_arPointsPolygon.RemoveAll();
			m_arPointsPolygon.Add(ptLog);
			
			OnDrawTrackDoubleEllipse(ptDev);
			
			m_action_state = State_DoubleEllipseStart;
		}
		else if(m_action_state == State_DoDoubleEllipseLine1)
		{
			m_pSelectedHelpLine = NULL;
			CPoint local = point;
			if(m_action_state != State_DoInfront_Shape &&
				m_action_state != State_DoBehind_Shape)
			{
				if(m_pCurHilightShape != NULL)
				{
					CFODrawShapeList lstUpdate;
					lstUpdate.AddTail(m_pCurHilightShape);
					
					m_pCurHilightShape = NULL;
					
					UpdateShapes(&lstUpdate);
				}
			}
			
			m_bResetSelect = FALSE;
			m_ptCurrentDevice = point;
			m_ptCurrentLog = m_ptCurrentDevice;
			FODPtoLP(&m_ptCurrentLog,1);
			if(m_bTabOrder)
			{
				DoChangeTabOrder(local);
				return;
			}
		}
		else
		{
			CFODrawView::OnLButtonDown(nFlags, point);
		}
	}
	else
	{
		CFODrawView::OnLButtonDown(nFlags, point);
	}
}

void CCustomDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GetCurrentModel()->IsDesignMode())
	{
		if(m_action_state == State_DoubleEllipse ||
			m_action_state == State_DoubleEllipseStart ||
			m_action_state == State_DoDoubleEllipse ||
			m_action_state == State_DoubleEllipseLine1Start ||
			m_action_state == State_DoDoubleEllipseLine1)
		{
			BOOL bTimerKilled = FALSE;
			bTimerKilled = CanvasWnd()->KillTimer(nDeafultAutoScrollItmerID);
			if(bTimerKilled)
			{
				DoUpdateRuler();
				FOPInvalidate(FALSE);
			}
			
			CRect rcDrawTrack = m_rcLastTrackingRect;
			//	DocToClient(rcDrawTrack);
			
			if (m_bEnableRulerTrack)
			{
				if (m_bEraseRulerTrackingRect)
				{
					CFOWndDC dc(this, FALSE);
					DrawRulerShapeRect(&dc, rcDrawTrack);
					m_bEraseRulerTrackingRect = FALSE;
				}
			}
			
			switch(m_action_state)
			{
			case State_DoubleEllipseStart:
				{
					StopMouseCapture();
					m_action_state = State_DoubleEllipse;
				}
				break;
				
			case State_DoDoubleEllipse:
				{
					CPoint ptDev = point;
					CPoint ptLog(ptDev);
					FODPtoLP(&ptLog,1);
					
					BOOL bEnableGridSnap = TRUE;
					if(m_bEnableNearest)
					{
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,ptLog,TRUE))
						{
							ptLog = ptNear;
							ptDev = ptLog;
							FOLPtoDP(&ptDev,1);
							bEnableGridSnap = FALSE;
						}
					}
					
					if(bEnableGridSnap)
					{
						if (GetCurrentModel()->IsGrid() && (nFlags & MK_CONTROL))
						{
							ptLog = FindCloseGridPoint(ptLog);
							ptDev = ptLog;
							FOLPtoDP(&ptDev,1);
						}
					}
					
					OnDrawTrackDoubleEllipse(m_ptMouseDown);
					OnDrawTrackGluespot(m_ptSaveGlue);
					
					if ((nFlags & MK_SHIFT) || m_bUpRightMode)
					{
						CPoint ptRef = m_arPointsPolygon.GetAt(m_arPointsPolygon.GetSize()-1);
						FOLPtoDP(&ptRef, 1);
						OffsetSamePoint(&ptDev);
						ptLog = ptDev;
						FODPtoLP(&ptLog, 1);
					}
					
					m_arPointsPolygon.Add(ptLog);
					int nCount = m_arPointsPolygon.GetSize();
					m_action_state = State_DoubleEllipseLine1Start;
					
				}
				break;
				
			case State_DoDoubleEllipseLine1:
				{
					CPoint ptDev = point;
					
					CPoint ptTrack = ptDev;
					FODPtoLP(&ptTrack, 1);
					
					LimitDrawShapeTrackPoint(&m_ptCurrentLog, &ptTrack, 1);
					
					FOLPtoDP(&ptTrack, 1);
					
					StopMouseCapture();
					
					OnDrawTrackDoubleEllipse(m_ptMouseDown);
					OnDrawTrackGluespot(m_ptSaveGlue);
					
					if ((nFlags & MK_SHIFT) || m_bUpRightMode)
						MakeUpRight(&m_ptCurrentDevice, &ptDev);
					
					POINT pptPoint[2];
					pptPoint[0] = m_ptCurrentLog;
					pptPoint[1] = ptDev;
					FODPtoLP(&pptPoint[1], 1);
					
					BOOL bEnableGridSnap = TRUE;
					if(m_bEnableNearest)
					{
						CPoint ptx = pptPoint[1];
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,ptx,TRUE))
						{
							pptPoint[1] = ptNear;
							
							bEnableGridSnap = FALSE;
						}
					}
					
					if(bEnableGridSnap)
					{
						if (GetCurrentModel()->IsGrid())
						{
							pptPoint[1] = FindCloseGridPoint(pptPoint[1]);
						}
					}
					
					CPoint ptTemp1 = CPoint(m_arPointsPolygon[0]);
					CPoint ptTemp2 = CPoint(m_arPointsPolygon[1]);
					CPoint ptTemp3 = pptPoint[1];

					CRect rcCreate(ptTemp1, ptTemp2);
					rcCreate.NormalizeRect();
					
					if(m_b1stPointAsCenter)
					{
						if(ptTemp1.x < ptTemp2.x)
						{
							rcCreate.left += rcCreate.left - rcCreate.right;
						}
						else
						{
							rcCreate.right += rcCreate.right - rcCreate.left;
						}
						
						if(ptTemp1.y < ptTemp2.y)
						{
							rcCreate.top += rcCreate.top - rcCreate.bottom;
						}
						else
						{
							rcCreate.bottom += rcCreate.bottom - rcCreate.top;
						}
					}

					CRect rcEllipse = CRect(ptTemp1,ptTemp2);

					CPoint ptCenter = rcEllipse.CenterPoint();
					
					int nWidth2 =(int)( rcEllipse.Width() / 2.0);
					int nHeight2 = (int)(rcEllipse.Height() / 2.0);
					
					int xDis = abs(ptTemp3.x - ptCenter.x);
					int yDis = abs(ptTemp3.y - ptCenter.y);
					if(xDis > nWidth2)
					{
						xDis = nWidth2 -2;
					}
					
					if(yDis > nHeight2)
					{
						yDis = nHeight2 -2;
					}
					
					CRect rcNew = rcEllipse;
					rcNew.DeflateRect(nWidth2 - xDis,nHeight2 - yDis,nWidth2 - xDis,nHeight2 - yDis);

					CExtDataModel *pModel = (CExtDataModel *)GetCurrentModel();
					pModel->AddDoubleEllipseShape(m_drawshape,rcEllipse,rcNew);
					
					if(gfxData.IsContinueEnableDrawing())
					{
						m_action_state = State_DoubleEllipse;
					}
					else
					{
						m_bUpRightMode = FALSE;
						m_drawshape = FO_COMP_NONE;
						ResetToSelectMode();
						FOSetSystemCursor(IDC_ARROW);
					}
				}
				break;
			}
		}
		else
		{
			CFODrawView::OnLButtonUp(nFlags, point);
		}
	}
	else
	{
		CFODrawView::OnLButtonUp(nFlags, point);
	}
}

void CCustomDrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CPoint local = point;
	if(GetCurrentModel()->IsDesignMode())
	{
		if(m_action_state == State_DoubleEllipse ||
			m_action_state == State_DoubleEllipseStart ||
			m_action_state == State_DoDoubleEllipse ||
			m_action_state == State_DoubleEllipseLine1Start ||
			m_action_state == State_DoDoubleEllipseLine1)
		{
			DoDrawRulerMousePos(point);
			
			if(m_bRulerUpdate)
			{
				FOPInvalidate(FALSE);
				m_bRulerUpdate = FALSE;
			}
			
			if(m_ptMouseMoveEnd == point)
			{
				return;
			}
			
			m_ptMouseMoveEnd = point;
			BOOL bMoveScroll = FALSE;
			
			switch(m_action_state)
			{
			case State_DoubleEllipse:
				{
					CPoint ptDev = point;
					CPoint ptLog(ptDev);
					FODPtoLP(&ptLog,1);
					
					if(m_bEnableNearest)
					{
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,ptLog,TRUE))
						{
							FOSetCursor(IDC_FO_CURSOR_ANCHOR);
						}
						else
						{
							FOSetCursor(m_nCurrentCursor);
						}
					}
					else
					{
						FOSetCursor(m_nCurrentCursor);
					}
					
				}
				break;
				
			case State_DoubleEllipseStart:
				{
					LimitDrawShapeTrackPoint(&m_ptCurrentLog, &m_ptCurrentLog, 1);
					FODPtoLP(&m_ptCurrentDevice, 1);
					LimitDrawShapeTrackPoint(&m_ptCurrentLog, &m_ptCurrentDevice, 1);
					FOLPtoDP(&m_ptCurrentDevice, 1);
					
					BOOL bEnableGridSnap = TRUE;
					if(m_bEnableNearest)
					{
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,m_ptCurrentLog,TRUE))
						{
							m_ptCurrentLog = ptNear;
							m_ptCurrentDevice = m_ptCurrentLog;
							FOLPtoDP(&m_ptCurrentDevice, 1);
							bEnableGridSnap = FALSE;
						}
					}
					
					if(bEnableGridSnap)
					{
						if (GetCurrentModel()->IsGrid())
						{
							m_ptCurrentLog = FindCloseGridPoint(m_ptCurrentLog);
							m_ptCurrentDevice = m_ptCurrentLog;
							FOLPtoDP(&m_ptCurrentDevice, 1);
						}
					}
					
					OnDrawTrackGluespot(m_ptSaveGlue);
					
					PrepareMouseCapture();
					OnDrawTrackDoubleEllipse(m_ptCurrentDevice);
					m_action_state = State_DoDoubleEllipse;
				}
				
			case State_DoDoubleEllipse:
				{
					CPoint ptDev = point;
					CPoint ptLog(ptDev);
					FODPtoLP(&ptLog,1);
					
					BOOL bEnableGridSnap = TRUE;
					if(m_bEnableNearest)
					{
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,ptLog,TRUE))
						{
							ptLog = ptNear;
							ptDev = ptLog;
							FOLPtoDP(&ptDev,1);
							FOSetCursor(IDC_FO_CURSOR_ANCHOR);
							
							if(m_ptSaveGlue != ptNear)
							{
								OnDrawTrackGluespot(m_ptSaveGlue);
								OnDrawTrackGluespot(ptNear);
								m_ptSaveGlue = ptNear;
							}
							
							bEnableGridSnap = FALSE;
						}
						else
						{
							FOSetCursor(m_nCurrentCursor);
						}
					}
					else
					{
						FOSetCursor(m_nCurrentCursor);
					}
					
					if(bEnableGridSnap)
					{
						if (GetCurrentModel()->IsGrid() && (nFlags & MK_CONTROL))
						{
							ptLog = FindCloseGridPoint(ptLog);
							ptDev = ptLog;
							FOLPtoDP(&ptDev,1);
						}
					}
					
					OnDrawTrackDoubleEllipse(m_ptMouseDown);
					
					if ((nFlags & MK_SHIFT) || m_bUpRightMode)
					{
						OffsetSamePoint(&ptDev);
					}
					
					OnDrawTrackDoubleEllipse(ptDev);
					
					CPoint ptMouse = point;
					FODPtoLP(&ptMouse,1);
					ChangeMousePosString(ptMouse.x,ptMouse.y);
					
					bMoveScroll = TRUE;
				}
				break;

			case State_DoubleEllipseLine1Start:
				{
					PrepareMouseCapture();
					
					BOOL bEnableGridSnap = TRUE;
					if(m_bEnableNearest)
					{
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,m_ptCurrentLog,TRUE))
						{
							m_ptCurrentLog = ptNear;
							bEnableGridSnap = FALSE;
						}
					}
					
					if(bEnableGridSnap)
					{
						if (GetCurrentModel()->IsGrid())
						{
							m_ptCurrentLog = FindCloseGridPoint(m_ptCurrentLog);
						}
					}
					
					LimitDrawShapeTrackPoint(&m_ptCurrentLog, &m_ptCurrentLog, 1);
					
					OnDrawTrackGluespot(m_ptSaveGlue);
					
					m_ptCurrentDevice = m_ptCurrentLog;
					FOLPtoDP(&m_ptCurrentDevice,1);
					OnDrawTrackDoubleEllipse(m_ptCurrentDevice);
					m_action_state = State_DoDoubleEllipseLine1;
				}
				
			case State_DoDoubleEllipseLine1:
				{
					CPoint ptDev = point;
					CPoint ptLog(ptDev);
					FODPtoLP(&ptLog,1);		
					
					BOOL bEnableGridSnap = TRUE;
					if(m_bEnableNearest)
					{
						CPoint ptNear;
						if(GetCurrentModel()->PickNearestPoint(m_listSelectComp,ptNear,ptLog,TRUE))
						{
							ptLog = ptNear;
							ptDev = ptLog;
							FOLPtoDP(&ptDev,1);
							FOSetCursor(IDC_FO_CURSOR_ANCHOR);
							
							if(m_ptSaveGlue != ptNear)
							{
								OnDrawTrackGluespot(m_ptSaveGlue);
								OnDrawTrackGluespot(ptNear);
								m_ptSaveGlue = ptNear;
							}
							
							bEnableGridSnap = FALSE;
						}
						else
						{
							FOSetCursor(m_nCurrentCursor);
						}
					}
					else
					{
						FOSetCursor(m_nCurrentCursor);
					}
					
					if(bEnableGridSnap)
					{
						if (GetCurrentModel()->IsGrid() && (nFlags & MK_CONTROL))
						{
							ptLog = FindCloseGridPoint(ptLog);
							ptDev = ptLog;
							FOLPtoDP(&ptDev,1);
						}
					}
					
					OnDrawTrackDoubleEllipse(m_ptMouseDown);
					
					if ((nFlags & MK_SHIFT) || m_bUpRightMode)
					{
						CPoint ptRef = m_arPointsPolygon.GetAt(m_arPointsPolygon.GetSize()-1);
						FOLPtoDP(&ptRef, 1);
						MakeUpRight(&ptRef, &ptDev);
					}
					
					OnDrawTrackDoubleEllipse(ptDev);
					
					CPoint ptMouse = point;
					FODPtoLP(&ptMouse,1);
					ChangeMousePosString(ptMouse.x,ptMouse.y);
					
					bMoveScroll = TRUE;
				}
				break;

			}
		}
		else
		{
			CFODrawView::OnMouseMove(nFlags, point);
		}
		
	}
	else
	{
		CFODrawView::OnMouseMove(nFlags, point);
	}
}

void CCustomDrawView::OnDrawTrackDoubleEllipse(CPoint ptTrackDev)
{
	m_ptMouseDown = ptTrackDev;

	CPoint ptTrack = m_ptMouseDown;
	FODPtoLP(&ptTrack, 1);
	LimitDrawShapeTrackPoint(&m_ptCurrentLog, &ptTrack, 1);
	LimitDrawShapeTrackPoint(&m_ptCurrentLog, &(m_arPointsPolygon[(m_arPointsPolygon.GetSize()-1)]), 1);
	FOLPtoDP(&ptTrack, 1);

	// Prepare dc.
	CFOWndDC dc(this,FALSE);
	PrepareTrackLineDC(&dc);

	int nCount = m_arPointsPolygon.GetSize();
	if (nCount > 0)
	{
		switch(nCount)
		{
		case 1:
			{
				CPoint ptFirst = m_arPointsPolygon.GetAt(0);
				FOLPtoDP(&ptFirst, 1);
				
				CPoint ptSecond = ptTrack;
				FOPRect rcEllipse(ptFirst, ptSecond);
				rcEllipse.NormalizeRect();
				rcEllipse.right++;
				rcEllipse.bottom++;
				
				if(m_b1stPointAsCenter)
				{
					if(ptFirst.x < ptSecond.x)
					{
						rcEllipse.left += rcEllipse.left - rcEllipse.right;
					}
					else
					{
						rcEllipse.right += rcEllipse.right - rcEllipse.left;
					}
					
					if(ptFirst.y < ptSecond.y)
					{
						rcEllipse.top += rcEllipse.top - rcEllipse.bottom;
					}
					else
					{
						rcEllipse.bottom += rcEllipse.bottom - rcEllipse.top;
					}
				}

				dc.Ellipse(rcEllipse);
			}
			break;

		case 2:
			{
				CPoint ptFirst = m_arPointsPolygon.GetAt(0);
				FOLPtoDP(&ptFirst, 1);
				
				CPoint ptSecond = ptTrack;
				CPoint pt = m_arPointsPolygon.GetAt(1);
				FOLPtoDP(&pt, 1);
				
				ptSecond = pt;
				FOPRect rcEllipse(ptFirst, ptSecond);
				rcEllipse.NormalizeRect();
				rcEllipse.right++;
				rcEllipse.bottom++;
				
				if(m_b1stPointAsCenter)
				{
					if(ptFirst.x < ptSecond.x)
					{
						rcEllipse.left += rcEllipse.left - rcEllipse.right;
					}
					else
					{
						rcEllipse.right += rcEllipse.right - rcEllipse.left;
					}
					
					if(ptFirst.y < ptSecond.y)
					{
						rcEllipse.top += rcEllipse.top - rcEllipse.bottom;
					}
					else
					{
						rcEllipse.bottom += rcEllipse.bottom - rcEllipse.top;
					}
				}
				dc.Ellipse(rcEllipse);

				CPoint ptCenter = rcEllipse.CenterPoint();

				int nWidth2 =(int)( rcEllipse.Width() / 2.0);
				int nHeight2 = (int)(rcEllipse.Height() / 2.0);

				int xDis = abs(ptTrack.x - ptCenter.x);
				int yDis = abs(ptTrack.y - ptCenter.y);
				if(xDis > nWidth2)
				{
					xDis = nWidth2 -2;
				}

				if(yDis > nHeight2)
				{
					yDis = nHeight2 -2;
				}

				CRect rcNew = rcEllipse;
				rcNew.DeflateRect(nWidth2 - xDis,nHeight2 - yDis,nWidth2 - xDis,nHeight2 - yDis);

				dc.Ellipse(rcNew);
			}
			break;
		}

	}
	// Clear dc.
	ClearTrackLineDC(&dc);
}

void CCustomDrawView::OnDrawTool1() 
{
	// TODO: Add your command handler code here
	m_drawshape = FO_COMP_PIE;
	SetCurrentDrawingCursor(IDC_FO_DRAW_PIE);
	m_action_state = State_DoubleEllipse;
	m_bUpRightMode = FALSE;
}

void CCustomDrawView::OnUpdateDrawTool1(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	BOOL bDesign = GetCurrentModel()->IsDesignMode();
	pCmdUI->Enable(bDesign);
	if(bDesign)
	{
		pCmdUI->SetCheck(m_drawshape == FO_COMP_PIE);
	}
}

[ Home | Products | Download Area | Purchase | SupportContact us ]


Copyright ?1998-2007 UCanCode.Net Software, all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.com