移动以及拉伸处理跟rect元素一样。不一样的地方就在绘制的实现。
#pragma once
#include "CEleBase.h"
class CEleText : public CEleBase
{
DEF_SOBJECT(SWindow, L"ele_text")
public:
CEleText();
~CEleText();
public:
void OnPaint(IRenderTarget* pRT);
protected:
LRESULT OnCreate(LPVOID);
void OnLButtonDown(UINT nFlags, SOUI::CPoint point);
void OnLButtonUp(UINT nFlags, SOUI::CPoint point);
void OnMouseMove(UINT nFlags, SOUI::CPoint point);
void OnLButtonDblClk(UINT nFlags, SOUI::CPoint point);
void OnRButtonUp(UINT nFlags, CPoint point);
protected:
SOUI_MSG_MAP_BEGIN()
MSG_WM_CREATE(OnCreate)
MSG_WM_PAINT_EX(OnPaint)
MSG_WM_LBUTTONDOWN(OnLButtonDown)
MSG_WM_LBUTTONUP(OnLButtonUp)
MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
MSG_WM_RBUTTONUP(OnRButtonUp)
MSG_WM_MOUSEMOVE(OnMouseMove)
SOUI_MSG_MAP_END()
private:
CPoint m_ptDown;
EcPosType m_curEcPosType;
SStringW m_sstrContent;
};
#include "stdafx.h"
#include "CEleText.h"
CEleText::CEleText()
{
m_sstrContent = L"13579ABCDEF测试测试";
}
CEleText::~CEleText()
{
//
}
void CEleText::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
SStringW sstrFont = L"face:微软雅黑,bold:0,italic:0,underline:0,strike:0,size:20";
SOUI::IFontPtr pFont = SFontPool::GetFont(sstrFont, GetScale());
pRT->SelectObject(pFont);
{
//单行文本测试
//pRT->DrawText(m_sstrContent, -1, (LPRECT)rcWindow, DT_LEFT | DT_TOP | DT_SINGLELINE | DT_NOPREFIX);
//多行文本测试
//pRT->DrawText(m_sstrContent, -1, (LPRECT)rcWindow, DT_LEFT | DT_TOP | DT_WORDBREAK | DT_NOPREFIX);
//填充文本测试
SIZE szContent;
pRT->MeasureText(m_sstrContent, m_sstrContent.GetLength(), &szContent);
float fXScale = (float)(rcWindow.Width()) / szContent.cx;
float fYScale = (float)(rcWindow.Height()) / szContent.cy;
SMatrix matrix1;
matrix1.setScale(fXScale, fYScale);
pRT->SetTransform(matrix1.fMat, NULL);
pRT->TextOutW(rcWindow.left / fXScale, rcWindow.top / fYScale, m_sstrContent, m_sstrContent.GetLength());
pRT->SetTransform(SMatrix().fMat);
}
//draw border
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);
}
CalcPos();
if (m_bSelected)
{
CAutoRefPtr<IBrush> brush, oldbrush;
pRT->CreateSolidColorBrush(RGBA(255, 0, 0, 255), &brush);
pRT->SelectObject(brush, (IRenderObj**)&oldbrush);
for (int i = 0; i < 8; ++i)
{
CRect rcDot(m_rcPos[i]);
pRT->FillRectangle(rcDot);
}
pRT->SelectObject(oldbrush, NULL);
}
}
LRESULT CEleText::OnCreate(LPVOID)
{
SetMsgHandled(FALSE);
//生成元素ID
std::string strUUID = CGlobalUnits::GetInstance()->GenerateUUID();
m_strUUID = strUUID;
return __super::OnCreate(NULL);
}
void CEleText::OnLButtonDown(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
//...
}
void CEleText::OnLButtonUp(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
//...
}
void CEleText::OnMouseMove(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
//...
}
void CEleText::OnLButtonDblClk(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
}
void CEleText::OnRButtonUp(UINT nFlags, CPoint point)
{
SetMsgHandled(FALSE);
}
控件使用:
注册
m_theApp->RegisterWindowClass<CEleText>();
使用
CEleText* pEle = (CEleText*)SApplication::getSingleton().CreateWindowByName(L"ele_text");
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;
效果如下:自上而下依次为单行文本、多行文本、填充文本