Duilib自定义空间实现拖动

1、CNetDevice.h文件

#pragma once

#pragma once
#include "../duilib/UIlib.h"
using namespace DuiLib;

class CNetDevice : public CContainerUI      //这里继承了CContainerUI,你也可以继承任意控件类如CButtonUI等,实现自定义控件
{
public:
    CNetDevice();
    ~CNetDevice();

    LPVOID GetInterface(LPCTSTR pstrName);
    void DoEvent(TEventUI& event);
    void DoPostPaint(HDC hDC, const RECT& rcPaint);

    //CControlUI* CreateControl(LPCTSTR pstrClass);

private:
    UINT m_uButtonState;
    POINT m_ptLastMouse;
    RECT m_rcNewPos;

    //CPaintManagerUI m_PaintManager;
};

2、CNetDevice.cpp

#include "stdafx.h"
#include "CNetDevice.h"


CNetDevice::CNetDevice()
{
    m_uButtonState = 0;
}


CNetDevice::~CNetDevice()
{
}

LPVOID CNetDevice::GetInterface(LPCTSTR pstrName)
{
    if (_tcscmp(pstrName, _T("NetDeviceInfo")) == 0)
    {
        return static_cast<CNetDevice*>(this);
    }
    else
    {
        return CContainerUI::GetInterface(pstrName);
    }
}

 void CNetDevice::DoEvent(TEventUI& event)
{
    if (event.Type == UIEVENT_BUTTONDOWN && IsEnabled())
    {
        if (::PtInRect(&m_rcItem, event.ptMouse))
        {
            m_uButtonState |= UISTATE_CAPTURED;
            m_ptLastMouse = event.ptMouse;
            m_rcNewPos = m_rcItem;

            if (m_pManager)
                m_pManager->AddPostPaint(this);
            return;
        }
    }
    else if (event.Type == UIEVENT_BUTTONUP)
    {
         if ((m_uButtonState & UISTATE_CAPTURED) != 0)
         {
            m_uButtonState &= ~UISTATE_CAPTURED;
            if (m_uButtonState & UISTATE_HOT)
            {

                m_uButtonState &= ~UISTATE_HOT;
                int height = this->GetHeight();
                m_rcNewPos.top -= height;
                m_rcNewPos.bottom -= height;    //高度存在偏差
                this->SetPos(m_rcNewPos);                   //这句是拖拽到目的地的关机,否则无法拖动到目的位置
            }

            if (m_pManager)
            {
                m_pManager->RemovePostPaint(this);
                m_pManager->Invalidate(m_rcNewPos);
            }

            NeedParentUpdate();

            return;
        }
    }
    else if (event.Type == UIEVENT_MOUSEMOVE)
    {
        if ((m_uButtonState & UISTATE_CAPTURED) != 0)
        {
            m_uButtonState |= UISTATE_HOT;

            CDuiRect rcParent = m_pParent->GetPos();
            int width = this->GetWidth();
            int height = this->GetHeight();

            LONG cx = event.ptMouse.x - m_ptLastMouse.x;
            LONG cy = event.ptMouse.y - m_ptLastMouse.y;

            m_ptLastMouse = event.ptMouse;

            RECT rcCurPos = m_rcNewPos;

            rcCurPos.left += cx;
            rcCurPos.right += cx;
            rcCurPos.top += cy;
            rcCurPos.bottom += cy;

            //加入对边界的判断
//            CDuiRect rcParent = m_pParent->GetPos();
//             int width = this->GetWidth();
//             int height = this->GetHeight();
            if (rcCurPos.left < rcParent.left)
            {
                rcCurPos.left = rcParent.left;
            }
            if (rcCurPos.left > (rcParent.right - width))
            {
                rcCurPos.left = rcParent.right - width;
            }
            if (rcCurPos.right > rcParent.right)
            {
                rcCurPos.right = rcParent.right;
            }
            if (rcCurPos.right < (rcParent.left + width))
            {
                rcCurPos.right = rcParent.left + width;
            }
            if (rcCurPos.top < rcParent.top)
            {
                rcCurPos.top = rcParent.top;
            }
            if (rcCurPos.top >(rcParent.bottom - height))
            {
                rcCurPos.top = rcParent.bottom - height;
            }
            if (rcCurPos.bottom > rcParent.bottom)
            {
                rcCurPos.bottom = rcParent.bottom;
            }
            if (rcCurPos.bottom < (rcCurPos.top + height))
            {
                rcCurPos.bottom = rcCurPos.top + height;
            }

            //将当前拖拽块的位置 和 当前拖拽块的前一时刻的位置,刷新  
            CDuiRect rcInvalidate = m_rcNewPos;
            m_rcNewPos = rcCurPos;

            rcInvalidate.Join(m_rcNewPos);
            if (m_pManager) m_pManager->Invalidate(rcInvalidate);

            return;
        }
    }
    if (event.Type == UIEVENT_SETCURSOR)
    {
        if (IsEnabled())
        {
            ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
            return;
        }
    }
    CContainerUI::DoEvent(event);
}

void CNetDevice::DoPostPaint(HDC hDC, const RECT& rcPaint)
{
    if ((m_uButtonState & UISTATE_CAPTURED) != 0) {
        CDuiRect rcParent = m_pParent->GetPos();

        RECT rcUpdate = { 0 };
         rcUpdate.left = m_rcNewPos.left < rcParent.left ? rcParent.left : m_rcNewPos.left;
         rcUpdate.top = m_rcNewPos.top < rcParent.top ? rcParent.top : m_rcNewPos.top;
         rcUpdate.right = m_rcNewPos.right > rcParent.right ? rcParent.right : m_rcNewPos.right;
         rcUpdate.bottom = m_rcNewPos.bottom > rcParent.bottom ? rcParent.bottom : m_rcNewPos.bottom;

        CRenderEngine::DrawColor(hDC, rcUpdate, 0xFF255EA1);
        CString lcaption;
        lcaption.Format(L"%d-%d", rcUpdate.left, rcUpdate.top); //上下左右
        CRenderEngine::DrawText(hDC, m_pManager, rcUpdate, lcaption, 0xFFFFFFFF, 1, DT_LEFT | DT_TOP);
    }
}

3、调用实现

int _left = 205;
        int _top = 47;

CNetDevice *pCNetDevice = new CNetDevice();//创建自定义控件
        if (NULL != pCNetDevice)
        {
            pCNetDevice->SetFloat();                     //一定要设置为绝对定位,否则不能拖动
            SIZE leftTop = { _left, _top };
            pCNetDevice->SetFixedXY(leftTop);           // 自定义控件在背景布局的起始位置
            pCNetDevice->SetFixedWidth(80);
            pCNetDevice->SetFixedHeight(80);
            pCNetDevice->SetAttribute(_T("bordersize"), _T("2"));
            pCNetDevice->SetAttribute(_T("bkcolor"), _T("#FFC0C0C0"));
            //pCNetDevice->SetAttribute(_T("bkimage"), _T("file='SchoolNet/logo2.png' dest='16,0,64,57'"));


            CLabelUI *pUserNameLabel = new CLabelUI;     //自定义控件增加一个标签
            if (pUserNameLabel != NULL)
            {
//                pUserNameLabel->SetAttribute(_T("float"), _T("true"));
//                pUserNameLabel->SetAttribute(_T("pos"), _T("8,60,46,80"));
//                pUserNameLabel->SetAttribute(_T("pos"), _T("8,60,46,80"));
                //pUserNameLabel->SetAttribute(_T("bkimage"), _T("SchoolNet/123.png"));
                pUserNameLabel->SetText(_T("大灰狼"));
                pUserNameLabel->SetAttribute(_T("textcolor"), _T("#FF000000"));
                //pUserNameLabel->SetMouseEnabled(false);
                pCNetDevice->Add(pUserNameLabel);
            }

            pVLNet->Add(pCNetDevice);
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值