二维码元素的移动、拉伸参考rect的实现,这里我们主要看看二维码的绘制部分代码
#pragma once
#include "CEleBase.h"
class CEleQRcode : public CEleBase
{
DEF_SOBJECT(SWindow, L"ele_qrcode")
public:
CEleQRcode();
~CEleQRcode();
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 "CEleQRcode.h"
CEleQRcode::CEleQRcode()
{
//
}
CEleQRcode::~CEleQRcode()
{
//
}
void CEleQRcode::OnPaint(IRenderTarget* pRT)
{
SetMsgHandled(FALSE);
pRT->SetAntiAlias(TRUE);
CRect rcWindow = GetWindowRect();
//QRCode
SStringW sstrContent = L"12359ABCDE";
std::string strContent = S_CW2A(sstrContent);
struct zint_symbol* symbol;
symbol = ZBarcode_Create();
symbol->symbology = BARCODE_QRCODE;
symbol->input_mode = DATA_MODE; //编码格式
//symbol->option_1; //设置QRCode的EccLevel
//symbol->option_2; //设置QRCode的Version
//symbol->option_3; //设置QRCode的掩码
int nRet = ZBarcode_Encode_and_Buffer_Vector(symbol, (unsigned char*)strContent.c_str(), strContent.size(), 0);
if (nRet == 0)
{
ZBarcode_Print(symbol, 0);
std::vector<CRect> vecRects;
if (symbol->vector)
{
struct zint_vector_rect* rect = symbol->vector->rectangles;
while (rect)
{
CRect rcTmp;
rcTmp.left = rect->x;
rcTmp.top = rect->y;
rcTmp.right = rcTmp.left + rect->width;
rcTmp.bottom = rcTmp.top + rect->height;
vecRects.push_back(rcTmp);
rect = rect->next;
}
}
double dDrawXUint = (double)rcWindow.Width() / symbol->width;
double dDrawYUint = (double)rcWindow.Height() / symbol->rows;
double dXUint = (double)symbol->bitmap_width / symbol->width;
double dYUint = (double)symbol->bitmap_height / symbol->rows;
std::vector<CRect> vecDrawRects;
for (int i = 0; i < vecRects.size(); i++)
{
CRect rcTmp(vecRects[i]);
rcTmp.left = (rcTmp.left / dXUint * dDrawXUint);
rcTmp.right = (rcTmp.right / dXUint * dDrawXUint);
rcTmp.top = (rcTmp.top / dYUint * dDrawYUint);
rcTmp.bottom = (rcTmp.bottom / dYUint * dDrawYUint);
rcTmp.InflateRect(0, 1, 0, 1);
vecDrawRects.push_back(rcTmp);
}
CAutoRefPtr<IPath> path;
GETRENDERFACTORY->CreatePath(&path);
for (int i = 0; i < vecDrawRects.size(); i++)
{
CRect rcRect;
rcRect.left += vecDrawRects[i].left + rcWindow.left;
rcRect.top += vecDrawRects[i].top + rcWindow.top;
rcRect.right = rcRect.left + vecDrawRects[i].Width();
rcRect.bottom = rcRect.top + vecDrawRects[i].Height();
path->addRect(rcRect);
}
COLORREF clrFrgnd = GETCOLOR(L"RGB(0,0,0)");
CAutoRefPtr<IBrush> brush, oldbrush;
pRT->CreateSolidColorBrush(clrFrgnd, &brush);
pRT->SelectObject(brush, (IRenderObj**)&oldbrush);
pRT->FillPath(path);
pRT->SelectObject(oldbrush, NULL);
}
}
LRESULT CEleQRcode::OnCreate(LPVOID)
{
SetMsgHandled(FALSE);
//生成元素ID
std::string strUUID = CGlobalUnits::GetInstance()->GenerateUUID();
m_strUUID = strUUID;
return __super::OnCreate(NULL);
}
void CEleQRcode::OnLButtonDown(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
SetCapture();
//...
}
void CEleQRcode::OnLButtonUp(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
ReleaseCapture();
//...
}
void CEleQRcode::OnMouseMove(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
//...
}
void CEleQRcode::OnLButtonDblClk(UINT nFlags, SOUI::CPoint point)
{
SetMsgHandled(FALSE);
}
void CEleQRcode::OnRButtonUp(UINT nFlags, CPoint point)
{
SetMsgHandled(FALSE);
}
控件使用:
先注册,再在容器中创建元素
m_theApp->RegisterWindowClass<CEleQRcode>();
CEleQRcode* pEle = (CEleQRcode*)SApplication::getSingleton().CreateWindowByName(L"ele_qrcode");
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;
以上就是二维码控件的实现以及使用,此处只实现了QRCODE码,其他的诸如DATAMATRIX、PDF417等码制可参考二维码的实现方式实现。
效果如下: