基于soui4实现一个打印排版软件(五)文本元素控件

移动以及拉伸处理跟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;

效果如下:自上而下依次为单行文本、多行文本、填充文本
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值