椭圆、圆、三角形、菱形元素的移动以及拉伸处理跟rect元素一样。
不一样的地方就在绘制元素的地方,实现如下:
椭圆:
void CEleEllipse::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
CPoint ptCenter = rcWindow.CenterPoint();
{
COLORREF clrBorder = GETCOLOR(L"RGB(0,0,255)");
int nBorderLine = PS_SOLID;
CAutoRefPtr<IPen> pen, oldpen;
pRT->CreatePen(nBorderLine | PS_ENDCAP_SQUARE, clrBorder, 2, &pen);
pRT->SelectObject(pen, (IRenderObj**)&oldpen);
pRT->DrawEllipse(&rcWindow);
pRT->SelectObject(oldpen, NULL);
}
//...
}
圆:
void CEleCircular::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
CPoint ptCenter = rcWindow.CenterPoint();
{
COLORREF clrBorder = GETCOLOR(L"RGB(0,0,255)");
int nBorderLine = PS_SOLID;
CAutoRefPtr<IPen> pen, oldpen;
pRT->CreatePen(nBorderLine | PS_ENDCAP_SQUARE, clrBorder, 2, &pen);
pRT->SelectObject(pen, (IRenderObj**)&oldpen);
pRT->DrawEllipse(&rcWindow);
pRT->SelectObject(oldpen, NULL);
}
//...
}
三角形:
void CEleTriangle::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
CPoint ptCenter = rcWindow.CenterPoint();
{
CAutoRefPtr<IPathS> path;
GETRENDERFACTORY->CreatePath(&path);
COLORREF clrBorder = GETCOLOR(L"RGB(0,0,255)");
int nBorderLine = PS_SOLID;
CAutoRefPtr<IPen> pen, oldpen;
//根据矩形计算三角形的三个顶点
CPoint pt1, pt2, pt3;
pt1.x = rcWindow.left;
pt1.y = rcWindow.bottom;
pt2.x = ptCenter.x;
pt2.y = rcWindow.top;
pt3 = rcWindow.BottomRight();
path->moveTo(pt1.x, pt1.y);
path->lineTo(pt2.x, pt2.y);
path->lineTo(pt3.x, pt3.y);
path->lineTo(pt1.x, pt1.y);
pRT->CreatePen(nBorderLine | PS_ENDCAP_SQUARE, clrBorder, 2, &pen);
pRT->SelectObject(pen, (IRenderObj**)&oldpen);
pRT->DrawPath(path);
pRT->SelectObject(oldpen, NULL);
}
//...
}
菱形:
void CEleDiamond::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
CPoint ptCenter = rcWindow.CenterPoint();
{
CAutoRefPtr<IPathS> path;
GETRENDERFACTORY->CreatePath(&path);
COLORREF clrBorder = GETCOLOR(L"RGB(0,0,255)");
int nBorderLine = PS_SOLID;
CAutoRefPtr<IPen> pen, oldpen;
//根据矩形计算菱形的四个顶点
CPoint pt1, pt2, pt3, pt4;
pt1.x = ptCenter.x;
pt1.y = rcWindow.bottom;
pt2.x = rcWindow.left;
pt2.y = ptCenter.y;
pt3.x = ptCenter.x;
pt3.y = rcWindow.top;
pt4.x = rcWindow.right;
pt4.y = ptCenter.y;
path->moveTo(pt1.x, pt1.y);
path->lineTo(pt2.x, pt2.y);
path->lineTo(pt3.x, pt3.y);
path->lineTo(pt4.x, pt4.y);
path->lineTo(pt1.x, pt1.y);
pRT->CreatePen(nBorderLine | PS_ENDCAP_SQUARE, clrBorder, 2, &pen);
pRT->SelectObject(pen, (IRenderObj**)&oldpen);
pRT->DrawPath(path);
pRT->SelectObject(oldpen, NULL);
}
//...
}
控件使用:
1、注册控件
m_theApp->RegisterWindowClass<CEleEllipse>();
m_theApp->RegisterWindowClass<CEleCircular>();
m_theApp->RegisterWindowClass<CEleTriangle>();
m_theApp->RegisterWindowClass<CEleDiamond>();
2、往画板容器中放置元素
椭圆:
CEleEllipse* pEle = (CEleEllipse*)SApplication::getSingleton().CreateWindowByName(L"ele_ellipse");
SASSERT(pEle);
SApplication::getSingleton().SetSwndDefAttr(pEle);
this->InsertChild(pEle);
pEle->SSendMessage(WM_CREATE);
CPoint ptReal(point);
ptReal.x -= rcContainer.left;
ptReal.y -= rcContainer.top;
SStringT sstrRectPos;
sstrRectPos.Format(_T("%d,%d,@%d,@%d"), ptReal.x, ptReal.y, 100, 50);
pEle->SetAttribute(L"pos", sstrRectPos);
//将位置、长宽等信息转为毫米
CPoint ptRB(ptReal.x + 100, ptReal.y + 50);
pEle->m_ptLT = ptReal;
pEle->m_ptRB = ptRB;
圆:
CEleCircular* pEle = (CEleCircular*)SApplication::getSingleton().CreateWindowByName(L"ele_circular");
SASSERT(pEle);
SApplication::getSingleton().SetSwndDefAttr(pEle);
this->InsertChild(pEle);
pEle->SSendMessage(WM_CREATE);
CPoint ptReal(point);
ptReal.x -= rcContainer.left;
ptReal.y -= rcContainer.top;
SStringT sstrRectPos;
sstrRectPos.Format(_T("%d,%d,@%d,@%d"), ptReal.x, ptReal.y, 100, 100);
pEle->SetAttribute(L"pos", sstrRectPos);
//将位置、长宽等信息转为毫米
CPoint ptRB(ptReal.x + 100, ptReal.y + 100);
pEle->m_ptLT = ptReal;
pEle->m_ptRB = ptRB;
三角形:
CEleTriangle* pEle = (CEleTriangle*)SApplication::getSingleton().CreateWindowByName(L"ele_triangle");
SASSERT(pEle);
SApplication::getSingleton().SetSwndDefAttr(pEle);
this->InsertChild(pEle);
pEle->SSendMessage(WM_CREATE);
CPoint ptReal(point);
ptReal.x -= rcContainer.left;
ptReal.y -= rcContainer.top;
SStringT sstrRectPos;
sstrRectPos.Format(_T("%d,%d,@%d,@%d"), ptReal.x, ptReal.y, 100, 80);
pEle->SetAttribute(L"pos", sstrRectPos);
//将位置、长宽等信息转为毫米
CPoint ptRB(ptReal.x + 100, ptReal.y + 80);
pEle->m_ptLT = ptReal;
pEle->m_ptRB = ptRB;
菱形:
CEleDiamond* pEle = (CEleDiamond*)SApplication::getSingleton().CreateWindowByName(L"ele_diamond");
SASSERT(pEle);
SApplication::getSingleton().SetSwndDefAttr(pEle);
this->InsertChild(pEle);
pEle->SSendMessage(WM_CREATE);
CPoint ptReal(point);
ptReal.x -= rcContainer.left;
ptReal.y -= rcContainer.top;
SStringT sstrRectPos;
sstrRectPos.Format(_T("%d,%d,@%d,@%d"), ptReal.x, ptReal.y, 80, 100);
pEle->SetAttribute(L"pos", sstrRectPos);
//将位置、长宽等信息转为毫米
CPoint ptRB(ptReal.x + 80, ptReal.y + 100);
pEle->m_ptLT = ptReal;
pEle->m_ptRB = ptRB;