图片元素的移动、拉伸参考rect的实现,这里我们主要看看图片的绘制部分代码
超级简单的就是加载图片然后把图片贴到对应的区域。
实现代码如下:
#pragma once
#include "CEleBase.h"
class CElePicture : public CEleBase
{
DEF_SOBJECT(SWindow, L"ele_picture")
public:
CElePicture();
~CElePicture();
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;
};
#include "stdafx.h"
#include "CElePicture.h"
CElePicture::CElePicture()
{
//
}
CElePicture::~CElePicture()
{
//
}
void CElePicture::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
SStringW sstrPicPath = L"test.png";
SAutoRefPtr<IBitmap> pBmp = SResLoadFromFile::LoadImage(sstrPicPath);
DWORD mode = MAKELONG(EM_STRETCH, kNone_FilterLevel);
if (pBmp)
{
CRect rcSrc(0, 0, pBmp->Width(), pBmp->Height());
pRT->DrawBitmapEx(rcWindow, pBmp, &rcSrc, mode, 255);
pBmp->Release();
}
else
{
//image load failed
}
//...
}
LRESULT CElePicture::OnCreate(LPVOID)
{
SetMsgHandled(FALSE);
//生成元素ID
std::string strUUID = CGlobalUnits::GetInstance()->GenerateUUID();
m_strUUID = strUUID;
return __super::OnCreate(NULL);
}
void CElePicture::OnLButtonDown(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
SetCapture();
//...
}
void CElePicture::OnLButtonUp(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
ReleaseCapture();
//...
}
void CElePicture::OnMouseMove(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
//...
}
void CElePicture::OnLButtonDblClk(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
}
void CElePicture::OnRButtonUp(UINT nFlags, CPoint point)
{
SetMsgHandled(FALSE);
}
控件使用:
先注册,再在容器中创建元素
m_theApp->RegisterWindowClass<CElePicture>();
CElePicture* pEle = (CElePicture*)SApplication::getSingleton().CreateWindowByName(L"ele_picture");
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, 150, 150);
pEle->SetAttribute(L"pos", sstrRectPos);
//将位置、长宽等信息转为毫米
CPoint ptRB(ptReal.x + 150, ptReal.y + 150);
pEle->m_ptLT = ptReal;
pEle->m_ptRB = ptRB;
以上就是图片控件的实现以及使用。
效果如下: