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

📄 mydrawview.cpp

📁 VC++绘图软件开发教程事例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	cMyBrush.CreateBrushIndirect(&pEllipse->MyBrush);
	pOldBrush=pDC->SelectObject(&cMyBrush); 
	
	pDC->Ellipse(pEllipse->startX,pEllipse->startY,pEllipse->endX,pEllipse->endY); 	
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush); 
}

void CMyDrawView::DrawRectangle(CDC *pDC, CRectangle *pRectangle)
{
	CPen cMyPen;
	CPen* pOldPen;
	
	cMyPen.CreatePenIndirect(&pRectangle->LinePen);
	pOldPen=pDC->SelectObject(&cMyPen);
	
	CBrush cMyBrush;
	CBrush* pOldBrush;
	
	cMyBrush.CreateBrushIndirect(&pRectangle->MyBrush);
	pOldBrush=pDC->SelectObject(&cMyBrush); 
	
	pDC->Rectangle(pRectangle->startX,pRectangle->startY,
		pRectangle->endX,pRectangle->endY); 	
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush); 
}

void CMyDrawView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CEllipse* pEllipse = NULL;
	CLine* pLine=NULL;
	CRectangle* pRectangle=NULL;
	CArc* pArc=NULL;
	CText* pText=NULL;

	POSITION pos = m_ObjectList.GetHeadPosition();
	CObject* pHitItem;
	while(pos != NULL)
	{
		pHitItem=(CObject*)m_ObjectList.GetNext(pos);
		if (pHitItem->IsKindOf(RUNTIME_CLASS(CLine)))
		{
			pLine = (CLine*)pHitItem;
			if(pLine != NULL)
			{
				if (pLine->bIsSelected)
				{
					CLineProperties LineDlg;
					CString str_LineWidth;
					str_LineWidth.Format("%d",pLine->LinePen.lopnWidth.x);
					LineDlg.m_LineWidth=str_LineWidth; 
					
					if (pLine->LinePen.lopnStyle==PS_DASH)
						LineDlg.m_LineStyle="虚线";
					else if (pLine->LinePen.lopnStyle==PS_DOT)
						LineDlg.m_LineStyle="点线";
					else if(pLine->LinePen.lopnStyle==PS_DASHDOT)
						LineDlg.m_LineStyle="点划线";
					else
						LineDlg.m_LineStyle="实线";
					
					LineDlg.m_LineColor=pLine->LinePen.lopnColor;  
					if (LineDlg.DoModal()==IDOK)
					{
						pLine->LinePen.lopnWidth.x=atoi(LineDlg.m_LineWidth);
						
						if (LineDlg.m_LineStyle=="虚线")
							pLine->LinePen.lopnStyle=PS_DASH;
						else if (LineDlg.m_LineStyle=="点线")
							pLine->LinePen.lopnStyle=PS_DOT;
						else if(LineDlg.m_LineStyle=="点划线")
							pLine->LinePen.lopnStyle=PS_DASHDOT;
						else
							pLine->LinePen.lopnStyle=PS_SOLID;
						
						pLine->LinePen.lopnColor=LineDlg.m_LineColor;  
						Invalidate();
					}
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CEllipse)))
		{
			pEllipse = (CEllipse*)pHitItem;
			if(pEllipse != NULL)
			{
				if (pEllipse->bIsSelected) 
				{
					CEllipseProperties EllipseDlg;
					CString str_LineWidth;
					str_LineWidth.Format("%d",pEllipse->LinePen.lopnWidth.x);
					EllipseDlg.m_LineWidth=str_LineWidth; 
					
					if (pEllipse->LinePen.lopnStyle==PS_DASH)
						EllipseDlg.m_LineStyle="虚线";
					else if (pEllipse->LinePen.lopnStyle==PS_DOT)
						EllipseDlg.m_LineStyle="点线";
					else if(pEllipse->LinePen.lopnStyle==PS_DASHDOT)
						EllipseDlg.m_LineStyle="点划线";
					else
						EllipseDlg.m_LineStyle="实线";
					EllipseDlg.m_LineColor=pEllipse->LinePen.lopnColor;  
					
					EllipseDlg.m_BrushColor=pEllipse->MyBrush.lbColor;
					if(pEllipse->MyBrush.lbHatch==HS_BDIAGONAL)
						EllipseDlg.m_HatchStyle= "HS_BDIAGONAL";
					else if(pEllipse->MyBrush.lbHatch==HS_CROSS)
						EllipseDlg.m_HatchStyle= "HS_CROSS";
					else if(pEllipse->MyBrush.lbHatch==HS_DIAGCROSS)
						EllipseDlg.m_HatchStyle= "HS_DIAGCROSS";
					else if(pEllipse->MyBrush.lbHatch==HS_FDIAGONAL)
						EllipseDlg.m_HatchStyle= "HS_FDIAGONAL";
					else if(pEllipse->MyBrush.lbHatch==HS_HORIZONTAL)
						EllipseDlg.m_HatchStyle= "HS_HORIZONTAL";
					else
						EllipseDlg.m_HatchStyle= "HS_VERTICAL";
					
					if(pEllipse->MyBrush.lbStyle==BS_HATCHED)
						EllipseDlg.m_BrushStyle= "BS_HATCHED";
					else if(pEllipse->MyBrush.lbStyle==BS_NULL)
						EllipseDlg.m_BrushStyle= "BS_NULL";
					else
						EllipseDlg.m_BrushStyle= "BS_SOLID";
					
					if (EllipseDlg.DoModal()==IDOK)
					{
						pEllipse->LinePen.lopnWidth.x=atoi(EllipseDlg.m_LineWidth);
						
						if (EllipseDlg.m_LineStyle=="虚线")
							pEllipse->LinePen.lopnStyle=PS_DASH;
						else if (EllipseDlg.m_LineStyle=="点线")
							pEllipse->LinePen.lopnStyle=PS_DOT;
						else if(EllipseDlg.m_LineStyle=="点划线")
							pEllipse->LinePen.lopnStyle=PS_DASHDOT;
						else
							pEllipse->LinePen.lopnStyle=PS_SOLID;
						pEllipse->LinePen.lopnColor=EllipseDlg.m_LineColor;  
						
						pEllipse->MyBrush.lbColor=EllipseDlg.m_BrushColor;
						if(EllipseDlg.m_HatchStyle== "HS_BDIAGONAL")
							pEllipse->MyBrush.lbHatch=HS_BDIAGONAL;
						else if(EllipseDlg.m_HatchStyle== "HS_CROSS")
							pEllipse->MyBrush.lbHatch=HS_CROSS;
						else if(EllipseDlg.m_HatchStyle== "HS_DIAGCROSS")
							pEllipse->MyBrush.lbHatch=HS_DIAGCROSS;
						else if(EllipseDlg.m_HatchStyle== "HS_FDIAGONAL")
							pEllipse->MyBrush.lbHatch=HS_FDIAGONAL;
						else if(EllipseDlg.m_HatchStyle== "HS_HORIZONTAL")
							pEllipse->MyBrush.lbHatch=HS_HORIZONTAL;
						else
							pEllipse->MyBrush.lbHatch=HS_VERTICAL;
						
						if(EllipseDlg.m_BrushStyle=="BS_HATCHED")
							pEllipse->MyBrush.lbStyle=BS_HATCHED;
						else if(EllipseDlg.m_BrushStyle=="BS_NULL")
							pEllipse->MyBrush.lbStyle=BS_NULL;
						else
							pEllipse->MyBrush.lbStyle=BS_SOLID;
						
						Invalidate();
					}
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CArc)))
		{
			pArc = (CArc*)pHitItem;
			if(pArc != NULL)
			{
				if (pArc->bIsSelected)
				{
					CLineProperties LineDlg;
					CString str_LineWidth;
					str_LineWidth.Format("%d",pArc->LinePen.lopnWidth.x);
					LineDlg.m_LineWidth=str_LineWidth; 
					
					if (pArc->LinePen.lopnStyle==PS_DASH)
						LineDlg.m_LineStyle="虚线";
					else if (pArc->LinePen.lopnStyle==PS_DOT)
						LineDlg.m_LineStyle="点线";
					else if(pArc->LinePen.lopnStyle==PS_DASHDOT)
						LineDlg.m_LineStyle="点划线";
					else
						LineDlg.m_LineStyle="实线";
					
					LineDlg.m_LineColor=pArc->LinePen.lopnColor;  
					if (LineDlg.DoModal()==IDOK)
					{
						pArc->LinePen.lopnWidth.x=atoi(LineDlg.m_LineWidth);
						
						if (LineDlg.m_LineStyle=="虚线")
							pArc->LinePen.lopnStyle=PS_DASH;
						else if (LineDlg.m_LineStyle=="点线")
							pArc->LinePen.lopnStyle=PS_DOT;
						else if(LineDlg.m_LineStyle=="点划线")
							pArc->LinePen.lopnStyle=PS_DASHDOT;
						else
							pArc->LinePen.lopnStyle=PS_SOLID;
						
						pArc->LinePen.lopnColor=LineDlg.m_LineColor;  
						Invalidate();
					}
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CText)))
		{
			pText = (CText*)pHitItem;
			if(pText != NULL)
			{
				if (pText->bIsSelected)
				{
					CTextProperties TextDlg;
					if (pText->BkMode==TRANSPARENT)
						TextDlg.m_BkMode=true;
					else
						TextDlg.m_BkMode=false; 
					TextDlg.m_Font=pText->MyFont;
					TextDlg.m_TextColor=pText->MyColor;
					TextDlg.m_Text=pText->MyText; 

					if (TextDlg.DoModal()==IDOK)
					{
						pText->MyFont=TextDlg.m_Font;  
						pText->MyColor=TextDlg.m_TextColor; 
						pText->MyText=TextDlg.m_Text;  

						if (TextDlg.m_BkMode)
							pText->BkMode=TRANSPARENT;
						else
							pText->BkMode=OPAQUE;
						Invalidate();
					}
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CRectangle)))
		{
			pRectangle = (CRectangle*)pHitItem;
			if(pRectangle != NULL)
			{
				if (pRectangle->bIsSelected)
				{
					CEllipseProperties EllipseDlg;
					CString str_LineWidth;
					str_LineWidth.Format("%d",pRectangle->LinePen.lopnWidth.x);
					EllipseDlg.m_LineWidth=str_LineWidth; 
					
					if (pRectangle->LinePen.lopnStyle==PS_DASH)
						EllipseDlg.m_LineStyle="虚线";
					else if (pRectangle->LinePen.lopnStyle==PS_DOT)
						EllipseDlg.m_LineStyle="点线";
					else if(pRectangle->LinePen.lopnStyle==PS_DASHDOT)
						EllipseDlg.m_LineStyle="点划线";
					else
						EllipseDlg.m_LineStyle="实线";
					EllipseDlg.m_LineColor=pRectangle->LinePen.lopnColor;  
					
					EllipseDlg.m_BrushColor=pRectangle->MyBrush.lbColor;
					if(pRectangle->MyBrush.lbHatch==HS_BDIAGONAL)
						EllipseDlg.m_HatchStyle= "HS_BDIAGONAL";
					else if(pRectangle->MyBrush.lbHatch==HS_CROSS)
						EllipseDlg.m_HatchStyle= "HS_CROSS";
					else if(pRectangle->MyBrush.lbHatch==HS_DIAGCROSS)
						EllipseDlg.m_HatchStyle= "HS_DIAGCROSS";
					else if(pRectangle->MyBrush.lbHatch==HS_FDIAGONAL)
						EllipseDlg.m_HatchStyle= "HS_FDIAGONAL";
					else if(pRectangle->MyBrush.lbHatch==HS_HORIZONTAL)
						EllipseDlg.m_HatchStyle= "HS_HORIZONTAL";
					else
						EllipseDlg.m_HatchStyle= "HS_VERTICAL";
					
					if(pRectangle->MyBrush.lbStyle==BS_HATCHED)
						EllipseDlg.m_BrushStyle= "BS_HATCHED";
					else if(pRectangle->MyBrush.lbStyle==BS_NULL)
						EllipseDlg.m_BrushStyle= "BS_NULL";
					else
						EllipseDlg.m_BrushStyle= "BS_SOLID";
					
					if (EllipseDlg.DoModal()==IDOK)
					{
						pRectangle->LinePen.lopnWidth.x=atoi(EllipseDlg.m_LineWidth);
						
						if (EllipseDlg.m_LineStyle=="虚线")
							pRectangle->LinePen.lopnStyle=PS_DASH;
						else if (EllipseDlg.m_LineStyle=="点线")
							pRectangle->LinePen.lopnStyle=PS_DOT;
						else if(EllipseDlg.m_LineStyle=="点划线")
							pRectangle->LinePen.lopnStyle=PS_DASHDOT;
						else
							pRectangle->LinePen.lopnStyle=PS_SOLID;
						pRectangle->LinePen.lopnColor=EllipseDlg.m_LineColor;  
						
						pRectangle->MyBrush.lbColor=EllipseDlg.m_BrushColor;
						if(EllipseDlg.m_HatchStyle== "HS_BDIAGONAL")
							pRectangle->MyBrush.lbHatch=HS_BDIAGONAL;
						else if(EllipseDlg.m_HatchStyle== "HS_CROSS")
							pRectangle->MyBrush.lbHatch=HS_CROSS;
						else if(EllipseDlg.m_HatchStyle== "HS_DIAGCROSS")
							pRectangle->MyBrush.lbHatch=HS_DIAGCROSS;
						else if(EllipseDlg.m_HatchStyle== "HS_FDIAGONAL")
							pRectangle->MyBrush.lbHatch=HS_FDIAGONAL;
						else if(EllipseDlg.m_HatchStyle== "HS_HORIZONTAL")
							pRectangle->MyBrush.lbHatch=HS_HORIZONTAL;
						else
							pRectangle->MyBrush.lbHatch=HS_VERTICAL;
						
						if(EllipseDlg.m_BrushStyle=="BS_HATCHED")
							pRectangle->MyBrush.lbStyle=BS_HATCHED;
						else if(EllipseDlg.m_BrushStyle=="BS_NULL")
							pRectangle->MyBrush.lbStyle=BS_NULL;
						else
							pRectangle->MyBrush.lbStyle=BS_SOLID;
						
						Invalidate();
					}
				}
			}
		}
	}
	CView::OnLButtonDblClk(nFlags, point);
}

void CMyDrawView::OnButtonLine() 
{
	// TODO: Add your command handler code here
	CLine* pLine=new CLine;
	m_ObjectList.AddTail(pLine); 
	Invalidate();
}

void CMyDrawView::OnButtonEllipse() 
{
	// TODO: Add your command handler code here
	CEllipse* pEllipse=new CEllipse;
	m_ObjectList.AddTail(pEllipse); 
	Invalidate();
}

void CMyDrawView::OnButtonRectangle() 
{
	// TODO: Add your command handler code here
	CRectangle* pRectangle=new CRectangle;
	m_ObjectList.AddTail(pRectangle); 
	Invalidate();
}

void CMyDrawView::OnButtonArc() 
{
	// TODO: Add your command handler code here
	CArc* pArc=new CArc;
	srand(::GetTickCount());
	if(rand()%10+1>5)//产生一个1到10的随机数
		pArc->Direction=0;
	else
		pArc->Direction=1;
	m_ObjectList.AddTail(pArc); 
	Invalidate();
}

void CMyDrawView::OnButtonText() 
{
	// TODO: Add your command handler code here
	CText* pText=new CText;
	m_ObjectList.AddTail(pText); 
	Invalidate();
}

void CMyDrawView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CEllipse* pEllipse = NULL;
	CLine* pLine=NULL;
	CRectangle* pRectangle=NULL;
	CArc* pArc=NULL;
	CText* pText=NULL;
	
	if (46==nChar)
	{
		POSITION pos = m_ObjectList.GetTailPosition();
		CObject* pHitItem;
		while(pos != NULL)
		{
			POSITION curpos=pos;
			pHitItem=(CObject*)m_ObjectList.GetPrev(pos);
			if (pHitItem->IsKindOf(RUNTIME_CLASS(CEllipse)))
			{
				pEllipse = (CEllipse*)pHitItem;
				if(pEllipse != NULL)
				{
					if (pEllipse->bIsSelected)
					{
						m_ObjectList.RemoveAt(curpos);
						Invalidate();
						break;
					}
				}
			}
			else if (pHitItem->IsKindOf(RUNTIME_CLASS(CLine)))
			{
				pLine = (CLine*)pHitItem;
				if(pLine != NULL)
				{
					if (pLine->bIsSelected)
					{
						m_ObjectList.RemoveAt(curpos);
						Invalidate();
						break;
					}
				}
			}
			else if (pHitItem->IsKindOf(RUNTIME_CLASS(CArc)))
			{
				pArc = (CArc*)pHitItem;
				if(pArc != NULL)
				{
					if (pArc->bIsSelected)
					{
						m_ObjectList.RemoveAt(curpos);
						Invalidate();
						break;
					}
				}
			}
			else if (pHitItem->IsKindOf(RUNTIME_CLASS(CText)))
			{
				pText = (CText*)pHitItem;
				if(pText != NULL)
				{
					if (pText->bIsSelected)
					{
						m_ObjectList.RemoveAt(curpos);
						Invalidate();
						break;
					}
				}
			}
			else if (pHitItem->IsKindOf(RUNTIME_CLASS(CRectangle)))
			{
				pRectangle = (CRectangle*)pHitItem;
				if(pRectangle != NULL)
				{
					if (pRectangle->bIsSelected)
					{
						m_ObjectList.RemoveAt(curpos);
						Invalidate();
						break;
					}
				}
			}
		}
	}
	CView::OnKeyUp(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

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