Home | Products | Download | Purchase | Support 


   

Sample3 Sample

 

 

The Sample3 sample demonstrates how to customize the toolbox window at left side of E-XD++ Library, this samples will showing all the images within a file folder and place then within toolbox window, then you can drag - drop these image to the canvas. It also demonstrates of how to add new ports on the shapes that dragging, it also demos how to create custom shape, create custom property values, and create custom actions:

Steps:

Steps:

1. Add a new class CMyImageShape that use CFOImageShape as base class.

2. Override the OnDraw3d and OnDrawFlat methods of CMyImageShape to handle the new drawing.


void CMyImageShape::OnDrawFlat(CDC *pDC)
{
//	CFOImageShape::OnDrawFlat(pDC);
	CRect rcTemp;
	rcTemp = CRect(m_lpShapePoints[0],m_lpShapePoints[2]);
	rcTemp.NormalizeRect();

	CString strText = GetObjectCaption();
	CRect rcNew = rcTemp;
	rcNew.InflateRect(2000,2000,2000,2000);
	CSize sizeText = GetTextSize(pDC,rcNew,strText);

	rcTemp.bottom -= sizeText.cy;
	DoDrawImage(pDC,rcTemp);

	CRect rcText;
	rcText = rcTemp;
	rcText.top = rcTemp.bottom;
	rcText.bottom = rcText.top + sizeText.cy;
	pDC->Rectangle(&rcText);
	if (m_pEdit != NULL)
	{
		if(m_bWithLabelEditing)
		{
			OnDrawTextAndEdit(pDC,rcText,FALSE);
		}
	}
	else
	{
		DrawText(pDC,GetObjectCaption(),&rcText,GetDrawFormatType());
	}

	//FODO:Add your own code below.
}

CSize CMyImageShape::GetTextSize(CDC* pDC,CRect rcPos,CString strText)
{
	CSize sizeText(0,0);
	
	CFont* pFont = NULL;
	CFont* pPrevFont = NULL;
	
	CFOCompProperties* pFontProp = (CFOCompProperties*)GetDefaultProperty();
	if (pFontProp != NULL)
	{
		pFont = pFontProp->GetFont();
		pPrevFont = (CFont*) pDC->SelectObject(pFont);
	}
	
	CStringArray setBreaks;
	CreateTextArray(pDC,strText,rcPos,setBreaks);
	
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	
	for (int nBreak = 0; nBreak < setBreaks.GetSize(); nBreak++)
	{
		CString strLine = setBreaks.GetAt(nBreak);
		CSize sizeLine(0,0);
		
		if (strLine.GetLength() > 0)
		{
			sizeLine = pDC->GetTextExtent(strLine);
		}
		else
		{
			sizeLine.cx = 1;
			sizeLine.cy = tm.tmHeight;
		}
		
		sizeText.cx = FORMMAX(sizeText.cx, sizeLine.cx);
		sizeText.cy += sizeLine.cy;
	}
	
	pDC->SelectObject(pPrevFont);
	
	return sizeText;
}


UINT CMyImageShape::GetDrawFormatType()
{
	UINT nTextHorz = GetTextHorzAlignment();
	UINT nTextVert = GetTextVertAlignment();
	UINT nAlign;
	if(IsMultiLine())
	{
		nAlign = DT_WORDBREAK;
		switch(nTextHorz)
		{
		case TextLeft: 
			{
				nAlign |= DT_LEFT;
			}
			break;
		case TextMiddle: 
			{
				nAlign |= DT_CENTER;
			}
			break;
		case TextRight:
			{
				nAlign |= DT_RIGHT;
			}
			break;
			
		}
	}
	else
	{
		nAlign = DT_SINGLELINE;
		switch(nTextHorz)
		{
		case TextLeft: 
			{
				nAlign |= DT_LEFT;
			}
			break;
		case TextMiddle: 
			{
				nAlign |= DT_CENTER;
			}
			break;
		case TextRight:
			{
				nAlign |= DT_RIGHT;
			}
			break;
			
		}
		
		switch(nTextVert)
		{
		case TextTop: 
			{
				nAlign |= DT_TOP;
			}
			break;
		case TextCenter: 
			{
				nAlign |= DT_VCENTER;
			}
			break;
		case TextBottom:
			{
				nAlign |= DT_BOTTOM;
			}
			break;
			
		}
	}
	return nAlign;
}

int CMyImageShape::CreateTextArray(CDC* pDC, CString strText,CRect rcBox,CStringArray& arLines)
{
	return CFODrawShape::CreateTextArray(pDC,strText,rcBox,arLines);
}

void CMyImageShape::OnDrawShadow(CDC *pDC)
{
	FillShadowPoly(pDC,m_lpShapePoints, m_nCompPtCount);
}

void CMyImageShape::OnDraw3d(CDC *pDC)
{
//	CFOImageShape::OnDraw3d(pDC);
	CRect rcTemp;
	rcTemp = CRect(m_lpShapePoints[0],m_lpShapePoints[2]);
	rcTemp.NormalizeRect();

	CString strText = GetObjectCaption();
	CRect rcNew = rcTemp;
	rcNew.InflateRect(2000,2000,2000,2000);
	CSize sizeText = GetTextSize(pDC,rcNew,strText);

	rcTemp.bottom -= sizeText.cy;
	DoDrawImage(pDC,rcTemp);

	CRect rcText;
	rcText = rcTemp;
	rcText.top = rcTemp.bottom;
	rcText.bottom = rcText.top + sizeText.cy;
	pDC->Rectangle(&rcText);

	if (m_pEdit != NULL)
	{
		if(m_bWithLabelEditing)
		{
			OnDrawTextAndEdit(pDC,rcText,FALSE);
		}
	}
	else
	{
		DrawText(pDC,GetObjectCaption(),&rcText,GetDrawFormatType());
	}
}

3. Create a new toolbox item object class CMyListItemObj that use CFOToolBoxItem as base class.

4. Create a new toolbox window class CMyToolBoxWnd that use CFOMultiToolBoxWnd as base class.

5. Create a new toolbox window bar class CMyToolBoxBar that use CFOPControlBar as base class.

6. Override the following method of CSample3View:


/////////////////////////////////////////////////////////////////////////////
// CSample3View drawing
CFODrawShape *CSample3View::GetOleDataObject(COleDataObject* pDataObject)
{
	CMyListItemObj *pItem = new  CMyListItemObj;
	if(!CFOToolBoxPageWnd::CreateFromOleData (pItem,pDataObject))
	{
		delete pItem;
		pItem = NULL;
		return NULL;
	}

	CFODrawShape *pReturn = NULL;
	if(pItem != NULL)
	{
		CRect rcCreate = CRect(0,0,0,0);
		pReturn = GetCurrentModel()->DoCreateShapeByType(pItem->GetType(),rcCreate,"",pItem);
		delete pItem;
		pItem = NULL;
	}

	return pReturn;
}

7. Override the following methods of CMyExtDataModel:

CFODrawShape *CMyExtDataModel::DoCreateShapeByType(UINT m_drawshape,CRect &rcCreate,CString strFileName,CFOToolBoxItem *pCurItem)
{
	CFODrawShape *pReturn = NULL;
	CString strCaption;
	CString strName;
	strCaption = "";
	strName = "";
	CPoint ptTrack = rcCreate.TopLeft();
	if(m_drawshape != FO_COMP_NONE)
	{
		CRect rc(rcCreate);
		rc.NormalizeRect();
		if(rc.IsRectEmpty()||(rc.Width()<=10 && rc.Height()<20))
		{
			if(m_drawshape == MY_LINE_TYPE)
			{
				rc = CRect(ptTrack.x-70,ptTrack.y-70,ptTrack.x+70,ptTrack.y+70);
			}
			else if(m_drawshape == 1220)
			{
				rc = CRect(ptTrack.x-70,ptTrack.y-70,ptTrack.x+70,ptTrack.y+70);
			}
		}
		if(m_drawshape == MY_LINE_TYPE)
		{
			pReturn = new CMyCorsssLineShape;
			pReturn->AddRef();
			pReturn->Create(rc,"");
			pReturn->UpdatePosition();
			strCaption = GetUniqueCaption(pReturn->GetType());
			strName = GetUniqueName(pReturn->GetType());
			pReturn->SetObjectCaption(strCaption);
			pReturn->SetObjectName(strName);
		}
		else if(m_drawshape == 1220)
		{
			if(pCurItem->IsKindOf(RUNTIME_CLASS(CMyListItemObj)))
			{
				CMyListItemObj *pItem = (CMyListItemObj *)pCurItem;
				pReturn = new CMyImageShape;
				pReturn->AddRef();
				pReturn->Create(rc,"");
				if(!pItem->m_strImageFileName.IsEmpty())
				{
					// Get application path.
					CString strPathX = AfxGetApp()->m_pszHelpFilePath;
					strPathX = strPathX.Left(strPathX.ReverseFind('\\'));
					
					// Get icon file path.
					CString strFile;
					strFile = strPathX+"\\Tools\\";
					strFile += pItem->m_strImageFileName;

					((CMyImageShape *)pReturn)->LoadImage(strFile);
				}
				strCaption = "First Text Line\nSecond Text Line\nEnd Text Line";//GetUniqueCaption(FO_COMP_IMAGE);
				strName = GetUniqueName(FO_COMP_IMAGE);
				pReturn->SetObjectCaption(strCaption);
				pReturn->SetObjectName(strName);
			}
		}
		else
		{
			pReturn = CFODataModel::DoCreateShapeByType(m_drawshape,rcCreate,strFileName);
		}
	}
	return pReturn;

}

8. Modify the codes of CMainFrame to create the new style toolbox window.


9. Create custom shape with class CMyCorsssLineShape.

10. Crate custom property class CMyCustomProperties, you can do it with ClassWizard.exe.

11. Attach property with shape:

CMyCustomProperties propLine;
AddNewProperty(propLine);

12. Modify the custom property values:


void CSample3View::OnShapeCustom() 
{
	// TODO: Add your command handler code here
	CFODrawShapeList lstUpdate;

	CFODrawShapeList m_ListTemp;
	int nCount = 0;
	BOOL bFirst = TRUE;
	CFODrawShape *pShape = NULL;
	CMyCorsssLineShape *pFirst = NULL;
	POSITION pos = m_listSelectComp.GetHeadPosition();
	while(pos != NULL)
	{
		pShape = (CFODrawShape *)m_listSelectComp.GetNext(pos);
		if(HAS_BASE(pShape,CMyCorsssLineShape))
		{
			if(bFirst)
			{
				pFirst = static_cast<CMyCorsssLineShape *>(pShape);
				bFirst = FALSE;
			}
			m_ListTemp.AddTail(pShape);
			nCount ++;
		}
		lstUpdate.AddTail(pShape);
	}

	if(nCount > 0)
	{
		CLinePropDlg dlg;
		dlg.m_strOldDescription = dlg.m_strDescription = pFirst->GetDescription();
		dlg.m_nOldLineWidth = dlg.m_nLineWidth = pFirst->GetLineWidth();
		dlg.m_crOldLine = dlg.m_crLine = pFirst->GetLineColor();
		if(dlg.DoModal() == IDOK)
		{
			if(dlg.m_bModify)
			{
				CFOMultiShapePropAction* pAction = GetCurrentModel()->GetShapesPropAction();

				pAction->AddShapesString(m_listSelectComp,dlg.m_strDescription,M_ID_DESCRIPTION);
				pAction->AddShapesInt(m_listSelectComp,dlg.m_nLineWidth,P_ID_LINE_WIDTH);
				pAction->AddShapesColor(m_listSelectComp,dlg.m_crLine,P_ID_LINE_COLOR);
				
				GetCurrentModel()->Do(pAction, TRUE);
				GetCurrentModel()->SetModifiedFlag();
			}
		}
	}

	if(lstUpdate.GetCount()>0)
	{
		UpdateShapes(&lstUpdate);
	}
}

void CSample3View::OnUpdateShapeCustom(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	BOOL bDesign = GetCurrentModel()->IsDesignMode();
	int nCount = GetCustomCount();
	pCmdUI->Enable(nCount > 0 && bDesign);
}

13. Create link with codes:


void CSample3View::OnInsertLink() 
{
	// TODO: Add your command handler code here

	// Create the link start shape.
	CRect rc = CRect(100,100,200,200);
	CFORectShape *pReturn = new CFORectShape;
	pReturn->AddRef();
    pReturn->Create(rc,"");
	pReturn->RemoveAllPorts();
	CFOPortShape *pStart = pReturn->CreateDefaultPort(0.5,0.5);
	CString strCaption = GetCurrentModel()->GetUniqueCaption(pReturn->GetType());
	CString strName = GetCurrentModel()->GetUniqueName(pReturn->GetType());
	pReturn->SetObjectCaption(strCaption);
	pReturn->SetObjectName(strName);
	GetCurrentModel()->InsertShape(pReturn);
	pReturn->Release();
	pReturn = NULL;


	// Create the link end shape.
	rc = CRect(400,100,500,200);
	pReturn = new CFORectShape;
	pReturn->AddRef();
    pReturn->Create(rc,"");
	pReturn->RemoveAllPorts();
	CFOPortShape *pEnd = pReturn->CreateDefaultPort(0.5,0.5);
	strCaption = GetCurrentModel()->GetUniqueCaption(pReturn->GetType());
	strName = GetCurrentModel()->GetUniqueName(pReturn->GetType());
	pReturn->SetObjectCaption(strCaption);
	pReturn->SetObjectName(strName);
	GetCurrentModel()->InsertShape(pReturn);
	pReturn->Release();
	pReturn = NULL;

	// Create a link between two shape.
	CFOLinkShape* pLinkComp = new CFOLinkShape;
	pLinkComp->AddRef();
	CPoint ptPoints[2];
	ptPoints[0] = pStart->GetLinkPoint();
	ptPoints[1] = pEnd->GetLinkPoint();
	pLinkComp->Create(ptPoints,2);
	strCaption = GetCurrentModel()->GetUniqueCaption(FO_COMP_LINK);
	strName = GetCurrentModel()->GetUniqueName(FO_COMP_LINK);
	pLinkComp->SetObjectCaption(strCaption);
	pLinkComp->SetObjectName(strName);

	// Set arrow type.
	int nType = 3;
	pLinkComp->SetEndArrowType(nType);
	CFOAddLinkAction* pCmd = new CFOAddLinkAction(GetCurrentModel(), pLinkComp);
	pCmd->SetToPort(pEnd);
	pCmd->SetFromPort(pStart);
	GetCurrentModel()->Do(pCmd,TRUE);
	pLinkComp->Release();
	pLinkComp = NULL;
}
14. Create composite shape

void CSample3View::OnInsertComps() 
{
	// TODO: Add your command handler code here
	CRect rc = CRect(50,50,250,300);
	CFOCompositeShape *pReturn = new CFOCompositeShape;
	pReturn->AddRef();
    pReturn->Create(rc,"");
	
	CFOStaticShape *pText = pReturn->AddText("My home","My Home",CRect(0,0,200,50));
	pText->SetBrushType(1);
	pText->SetBkColor(RGB(255,0,0));
	pText->SetPenStyle(PS_SOLID);
	pText->SetNullPen(FALSE);

	CFOStaticShape *pText1 = pReturn->AddText("Father","Steven",CRect(0,50,200,100));
	pText1->SetBrushType(1);
	pText1->SetBkColor(RGB(255,255,255));
	pText1->SetPenStyle(PS_SOLID);
	pText1->SetNullPen(FALSE);

	CFOStaticShape *pText2 = pReturn->AddText("Mather","Cindy",CRect(0,100,200,150));
	pText2->SetBrushType(1);
	pText2->SetBkColor(RGB(255,255,255));
	pText2->SetPenStyle(PS_SOLID);
	pText2->SetNullPen(FALSE);

	CFOStaticShape *pText3 = pReturn->AddText("Brother0","John",CRect(0,150,200,200));
	pText3->SetBrushType(1);
	pText3->SetBkColor(RGB(255,255,255));
	pText3->SetPenStyle(PS_SOLID);
	pText3->SetNullPen(FALSE);

	CFOStaticShape *pText4 = pReturn->AddText("Brother1","Jack",CRect(0,200,200,250));
	pText4->SetBrushType(1);
	pText4->SetBkColor(RGB(255,255,255));
	pText4->SetPenStyle(PS_SOLID);
	pText4->SetNullPen(FALSE);


	pReturn->RemoveAllPorts();
	pReturn->CreateDefaultPort(0.5,0.5);
	CString strCaption = GetCurrentModel()->GetUniqueCaption(pReturn->GetType());
	CString strName = GetCurrentModel()->GetUniqueName(pReturn->GetType());
	pReturn->SetObjectCaption(strCaption);
	pReturn->SetObjectName(strName);

	pReturn->PositionShape(&rc);

	GetCurrentModel()->InsertShape(pReturn);
	pReturn->Release();
	pReturn = NULL;
}



[ 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