OPessoa Blog

MFC - GDI - Drawing Marker Like Google Maps

Posted on 2016-11-14 18:31:51


How to draw marker using path.
.h File
void DrawMarker(CDC* pDC, int radius,int xPos, int yPos, COLORREF color, float insideCirclePerc = 0.3 );

.cpp File
void CChildView::DrawMarker(CDC* pDC, int radius,int xPos, int yPos, COLORREF color, float insideCirclePerc )
{
CRect rc;
CPen* pOldPen;
CBrush* pOldBrush;
CPen pBlack(PS_SOLID,1,RGB(0,0,0));
CBrush brBlack(RGB(0,0,0));

CBrush br(color);
CBrush* pOldBr = pDC->SelectObject(&br);
CPoint ptsRightBz[3] = { CPoint(xPos+radius/2, yPos+radius/2),  CPoint(xPos, yPos+radius/2),CPoint(xPos, yPos+radius*1.3) };
CPoint ptsLeftBz[3] = { CPoint(xPos, yPos+radius/2),CPoint(xPos-radius/2, yPos+radius/2), CPoint(xPos-radius/2, yPos) };

pDC->BeginPath();
pDC->MoveTo(CPoint(xPos+radius/2,yPos));
pDC->PolyBezierTo(ptsRightBz,3);
pDC->PolyBezierTo(ptsLeftBz,3);
pDC->ArcTo(xPos-radius/2,yPos-radius/2,1+xPos+radius/2,1+yPos+radius/2,1+xPos+radius/2,yPos,1+xPos-radius/2,yPos);
pDC->EndPath();

pOldPen = pDC->SelectObject(&pBlack);
pDC->StrokeAndFillPath();


/*Draw line over the black bezier line*/

CPen p(PS_SOLID,2,color);
pDC->SelectObject(&p);
pDC->MoveTo(CPoint(1+xPos-radius/2,yPos));
pDC->LineTo(CPoint(-1+xPos+radius/2,yPos));

pDC->SelectObject(pOldPen);

pDC->SelectObject(&brBlack);
pDC->Ellipse(xPos-(radius/2)*insideCirclePerc,yPos-(radius/2)*insideCirclePerc,xPos+(radius/2)*insideCirclePerc,yPos+(radius/2)*insideCirclePerc);
pDC->SelectObject(pOldBr);
}
Using:

DrawMarker(&dc,100,100,100,RGB(255,0,0));