duilib获取CSliderUI滑动事件 - 用自定义控件实现

来源:互联网 发布:拜尔电动牙刷知乎 编辑:程序博客网 时间:2024/05/23 23:45

转载自: http://www.duilibcn.com/source/217.html

用duilib也有一段时间了,但是想做个东西时发现duilib的文档如此之少,
想获取滑块的滑动事件,从而获取滑块的值都找不到一篇文章。
好吧,凭借着对Qt和MFC事件传递的理解,我想到在duilib里扩展CSliderUI,在控件里
监听控件的mouse move和mouse down事件应该也是可以办到的,来验证这个想法吧。
 
先定义一个扩展的CSliderUI
#ifndef MYSLIDER_H
#define MYSLIDER_H
 
#include <Windows.h>
#include <objbase.h>
#include <UIlib.h>
using namespace DuiLib;
 
 
class CMainWindow;
class MySlider : public CSliderUI
{
 
public:
    MySlider(CMainWindow* pMain);
    ~MySlider();
 
 
    CMainWindow* m_MainWnd;
    static const CDuiString m_className;
    static const CDuiString m_interFaceName;
 
    bool m_MouseDown;
public:
    virtual LPCTSTR GetClass() const;
    virtual LPVOID GetInterface(LPCTSTR pstrName);
    virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
 
    virtual void DoEvent(TEventUI& event);
    virtual void DoPaint(HDC hDC, const RECT& rcPaint);
 
};
 
#endif // MYSLIDER_H
其中CMainWindow是主窗体的类,它有一个方法是设置label1的值,方便我们查看,
稍后会提供下载,MySlider.cpp
它的类名是MySlider,这个很重要,一会会用到
 
#include "myslider.h"
 
#include "cmainwindow.h"
#include <iostream>
using namespace std;
 
const CDuiString MySlider::m_className = L"MySlider";
const CDuiString MySlider::m_interFaceName = L"MySliderInterface";
 
MySlider::MySlider(CMainWindow* pMain) : CSliderUI(),m_MainWnd(pMain)
{
    m_MouseDown = false;
}
 
MySlider::~MySlider()
{
 
}
 
// 这个是类名
LPCTSTR MySlider::GetClass() const
{
    return m_className.GetData();
}
 
// 这个是xml控件的名称
LPVOID MySlider::GetInterface(LPCTSTR pstrName)
{
 
    //if (_tcscmp(pstrName,m_interFaceName.GetData()) == 0)
    //   return static_cast<MySlider*>(this);
 
    return CControlUI::GetInterface(pstrName);
}
 
void MySlider::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
 
    CSliderUI::SetAttribute(pstrName, pstrValue);
}
 
 
void MySlider::DoEvent(TEventUI &event)
{
    if(event.Type == UIEVENT_MOUSEMOVE && m_MouseDown)
    {
        cout<<"--mouse over v:"<<this->GetValue()<<endl;
 
        WCHAR s[50];
        memset(s,0,2*50);
        _itow(this->GetValue(),s,10);
        m_MainWnd->setLabel1Text(CDuiString(s));
    }
    else if(event.Type == UIEVENT_BUTTONDOWN)
    {
        m_MouseDown = true;
    }
    else if(event.Type == UIEVENT_BUTTONUP)
    {
        m_MouseDown = false;
    }
 
    return CSliderUI::DoEvent(event);
}
 
void MySlider::DoPaint(HDC hDC, const RECT &rcPaint)
{
 
    return CSliderUI::DoPaint(hDC,rcPaint);
}
接下来在cmainwindow.xml里自定义一个控件,它的名称是MySlider
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="800,600" sizebox="4,4,4,4" caption="0,0,0,32" mininfo="600,400">
    <VerticalLayout bkcolor="#FFF0F0F0" bkcolor2="#FFAAAAA0">
        <HorizontalLayout height="32" bkcolor="#FFE6E6DC" bkcolor2="#FFAAAAA0">
            <HorizontalLayout>
                <Label name="apptitle" text=" Hello World By DuiLib" height="32" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" />
            </HorizontalLayout>
            <HorizontalLayout width="77">
                <Button name="minbtn" tooltip="最小化" float="true" pos="0,5,0,0" width="23" height="19" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage=" file=&apos;skin\MinNormal.bmp&apos; " hotimage=" file=&apos;skin\MinFocus.bmp&apos; " pushedimage=" file=&apos;skin\MinFocus.bmp&apos; " />
                <Button name="maxbtn" tooltip="最大化" float="true" pos="22,5,0,0" width="23" height="19" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage=" file=&apos;skin\MaxNormal.bmp&apos; " hotimage=" file=&apos;skin\MaxFocus.bmp&apos; " pushedimage=" file=&apos;skin\MaxFocus.bmp&apos; " />
                <Button name="restorebtn" tooltip="还原" visible="false" float="true" pos="22,5,0,0" width="23" height="19" align="center" normalimage=" file=&apos;skin\StoreNormal.bmp&apos; " hotimage=" file=&apos;skin\StoreFocus.bmp&apos; " pushedimage=" file=&apos;skin\StoreFocus.bmp&apos; " />
                <Button name="closebtn" tooltip="关闭" float="true" pos="44,5,0,0" width="28" height="19" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage=" file=&apos;skin\CloseNormal.bmp&apos; " hotimage=" file=&apos;skin\CloseFocus.bmp&apos; " pushedimage=" file=&apos;skin\CloseFocus.bmp&apos; " />
            </HorizontalLayout>
        </HorizontalLayout>
        <VerticalLayout>
            <Label name="label1" text="20" float="true" pos="318,50,0,0" width="164" height="38" bkcolor="#FFFF0000" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />
            <Button name="button1" text="Hello" float="true" pos="318,265,0,0" width="164" height="38" bkcolor="#FFFF0000" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />
            <MySlider align="center" bkcolor="#FFF0F0F0" disabledtextcolor="#FFA7A6AA" float="true" height="21" hor="true" max="100" min="0" name="slider1" pos="241,131,0,0" textcolor="#FF000000" thumbimage="skin\00.jpg" thumbsize="10,10" value="20" width="259" />
        </VerticalLayout>
    </VerticalLayout>
</Window>
 
CMainWindow集成自WindowImplBase,它有一个关键的创建自定义控件的成员函数
CControlUI* CMainWindow::CreateControl(LPCTSTR pstrClass)
{
    if(_tcsicmp(pstrClass,MySlider::m_className.GetData()) == 0)
    {
        return new MySlider(this);
    }
    return NULL;
}
通过return new MySlider(this);我们实现了XML界面的MySlider和MySlider类的绑定。
 
下面是实现自定义控件监听事件的关键代码
void MySlider::DoEvent(TEventUI &event)
{
    if(event.Type == UIEVENT_MOUSEMOVE && m_MouseDown)
    {
        cout<<"--mouse over v:"<<this->GetValue()<<endl;
        WCHAR s[50];
        memset(s,0,2*50);
        _itow(this->GetValue(),s,10);
        m_MainWnd->setLabel1Text(CDuiString(s));
    }
    else if(event.Type == UIEVENT_BUTTONDOWN)
    {
        m_MouseDown = true;
    }
    else if(event.Type == UIEVENT_BUTTONUP)
    {
        m_MouseDown = false;
    }
 
    return CSliderUI::DoEvent(event);
}
通过扩展控件,我们监听了UIEVENT_MOUSEMOVE和UIEVENT_BUTTONDOWN事件,在控件按下鼠标键并
滑动鼠标的时候成功的获取了滑动事件。
 
源码和编译好的EXE例子下载
http://download.csdn.net/detail/hats8888/9347735

未经duilib中文网同意禁止转载!

0 0
原创粉丝点击