先定义个元素基类用来定义元素公共参数
基类头文件:
#pragma once
#include "CGlobalUnits.h"
/*
* 为了方便此处将变量全定义为public
*/
class CEleBase : public SWindow
{
public:
CEleBase();
virtual ~CEleBase();
public:
std::string m_strUUID; //元素唯一ID,方便后续使用
double m_dX; //元素X位置(毫米)
double m_dY; //元素Y位置(毫米)
double m_dWid;//元素长(毫米)
double m_dHei;//元素宽(毫米)
bool m_bSelected; //元素是否被选中
};
基类源文件:
#include "stdafx.h"
#include "CEleBase.h"
CEleBase::CEleBase()
{
m_bSelected = false;
}
CEleBase::~CEleBase()
{
//
}
矩形元素类定义&实现
#pragma once
#include "CEleBase.h"
class CEleRect : public CEleBase
{
DEF_SOBJECT(SWindow, L"ele_rect")
public:
CEleRect();
~CEleRect();
public:
void OnPaint(IRenderTarget* pRT);
protected:
LRESULT OnCreate(LPVOID);
protected:
SOUI_MSG_MAP_BEGIN()
MSG_WM_CREATE(OnCreate)
MSG_WM_PAINT_EX(OnPaint)
SOUI_MSG_MAP_END()
private:
};
#include "stdafx.h"
#include "CEleRect.h"
CEleRect::CEleRect()
{
}
CEleRect::~CEleRect()
{
}
void CEleRect::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->DrawRectangle(&rcWindow);
pRT->SelectObject(oldpen, NULL);
}
if (m_bSelected)
{//绘制被选中时的边框
CAutoRefPtr<IPen> pen, oldpen;
pRT->CreatePen(PS_SOLID, RGBA(0, 0, 255, 100), 2, &pen);
pRT->SelectObject(pen, (IRenderObj**)&oldpen);
CRect rcBorder(rcWindow);
rcBorder.InflateRect(2, 2, 2, 2);
pRT->DrawRectangle(rcBorder);
pRT->SelectObject(oldpen, NULL);
}
}
LRESULT CEleRect::OnCreate(LPVOID)
{
SetMsgHandled(FALSE);
//生成元素ID
std::string strUUID = CGlobalUnits::GetInstance()->GenerateUUID();
m_strUUID = strUUID;
return __super::OnCreate(NULL);
}
控件使用:
1、注册控件
m_theApp->RegisterWindowClass<CEleRect>();
2、往画板容器中放置元素
//在CPaletteContainer中进行元素添加
CRect rcContainer = GetClientRect();
CPoint point; //放置的点
CPoint ptReal(point);//实际距画板容器左上角的位置信息
ptReal.x -= rcContainer.left;
ptReal.y -= rcContainer.top;
CEleRect* pEle = (CEleRect*)SApplication::getSingleton().CreateWindowByName(L"ele_rect");
SASSERT(pEle);
SApplication::getSingleton().SetSwndDefAttr(pEle);
this->InsertChild(pEle);
pEle->SSendMessage(WM_CREATE);
SStringT sstrPos;
sstrPos.Format(_T("%d,%d,@%d,@%d"), ptReal.x, ptReal.y, 100, 100); //大小指定为100*100
pEle->SetAttribute(L"pos", sstrPos);
//将位置、长宽等信息转为毫米(待实现)
//TODO: