MFC 给不规则的多边形闭合区域填充颜色

问题描述:

现有多个由线段line汇成的闭合区域,将每个区域使用不同的颜色填充。 

实现的完整代码:

//绘制图形
	// find the client area
	CRect rect;
	GetClientRect(rect);
	// draw with a thick blue pen
	CPen penBlue(PS_SOLID, 2, RGB(0, 0, 255));
	CPen* pOldPen = pDC->SelectObject(&penBlue);
	int colorIndexR;
	int colorIndexG;
	int colorIndexB;
	CBrush *pOldBrush;
	for (unsigned int i = 0;i < m_minClosedArea.size(); i++)  //m_minClosedArea集合为多个有线段围成的闭合区域
	{
		vector<CLine> m_oneClosedArea= m_minClosedArea[i];//闭合区域A
		int nSize = (int)m_oneClosedArea.size(); //闭合区域的线段数量(点数量)
		CPolyline m_polyline;
		CPoint pts[100];
	
		//将闭合区域A中的点存放在pts中
		for (unsigned int j=0; j<m_oneClosedArea.size(); j++) 
		{		
			Point m_ptStart = m_oneClosedArea[j].getStartPoint();		
			int x1 = (int)m_ptStart.x;
			int y1 = (int)m_ptStart.y;
			pts[j].SetPoint(x1,-y1);
		}

		colorIndexR = (i*50 - 50)%256; //颜色索引
		colorIndexG = (i*100 + 100)%256; //颜色索引
		colorIndexB = (i*200 - 200)%256; //颜色索引

		CBrush brushColor(RGB(colorIndexR,colorIndexG, colorIndexB)); //定义画刷的颜色
		pOldBrush = pDC->SelectObject(&brushColor); //将画刷颜色赋予*pDC
		pDC->Polygon(pts, nSize);  //绘制多边形

		pDC->SelectObject(&pOldPen);	
		pDC->SelectObject(pOldBrush);
	}

效果:

  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值