duilib 界面库 实现timer定时器

来源:互联网 发布:去网络代理商工作好吗 编辑:程序博客网 时间:2024/05/20 22:27
  • 看了大神介绍的duilib感觉已被同龄人狠狠地甩在背后。所以痛下决心,之后要多花时间写代码。

    大神教程传送门:

    http://www.cnblogs.com/Alberl/p/3341956.html

    现在的问题是想基于duilib实现一个timer定时器。工程基础大概是在

    http://www.cnblogs.com/Alberl/p/3343763.html

    因为自己的东西是基于大神的东西写的,所以要把大神的教程看得差不多才知道我在说什么。O(∩_∩)O~~

    前台大概长这个样子:

     

    稍微修改了一下就是这样了,很简陋,只是为了说明问题。(很佩服自己装作很多人会看的样子n(*≧▽≦*)n)

    这里引用大神的后台代码:

    复制代码
    class CDuiFrameWnd : public WindowImplBase{public:    virtual LPCTSTR    GetWindowClassName() const   {   return _T("DUIMainFrame");  }    virtual CDuiString GetSkinFile()                {   return _T("duilib.xml");  }    virtual CDuiString GetSkinFolder()              {   return _T("");  }};int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){    CPaintManagerUI::SetInstance(hInstance);    CDuiFrameWnd duiFrame;    duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);    duiFrame.CenterWindow();    duiFrame.ShowModal();    return 0;}
    复制代码

     

    任务是,点击开始按钮,注册一个timer,然后里面的事件每5秒被调用一次。

    1.为开始按钮添加响应事件

    只需要在CDuiFrameWnd类中重写OnClick方法就可以啦(1.我怎么知道这个方法可以重写?看基类的源代码后发现的。2.我是学java的,所以我不知道这里的“重写”在c++上说的恰不恰当)。

    复制代码
     1   virtual void OnClick(TNotifyUI& msg) 2     { 3         CDuiString sCtrlName = msg.pSender->GetName(); 4         if (sCtrlName == _T("closebtn")) 5         { 6             Close(); 7             return; 8         } 9         else if (sCtrlName == _T("minbtn"))10         {11             SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);12             return;13         }14         else if (sCtrlName == _T("maxbtn"))15         {16             SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);17             return;18         }19         else if (sCtrlName == _T("restorebtn"))20         {21             SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);22             return;23         }24         else if (sCtrlName == _T("btnHello"))25         {26             HWND hwnd = m_PaintManager.GetPaintWindow();27             SetTimer(hwnd, 1, 5000,    NULL);28         }29 30         return;31     }
    复制代码


    这里注意到,如果草率地只为开始按钮添加事件的话,最小化等三个按钮会失效,所以重写的时候把基类的方法拷来修改,不晓得有没有更方便的做法。

    settimer函数的四个参数分别表示:1.我也不知道。2.timer的id。3.间隔。4.时间到了要执行的函数。

    4被设为空是MFC的常用做法。因为设为空它也会默认调用类的onTimer方法。

    2.添加onTimer方法

    发现基类并无此方法(也可能是我找的不仔细,没找见),所以现在不是重写,就是添加一个方法。

    这里就是出个对话框。

        virtual LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)    {        ::MessageBox(NULL, _T("AC"), _T("随便啥"), NULL);        bHandled = TRUE;        return 0;    }

    为毛ontimer长这个样子?而onclick长另一幅样子?onclick长那副样子是因为基类里面就长那样,我们要重写就拿过来了。ontimer的话,一会儿解释。

    为毛不是重写,还加virtual?刚开始时觉得大家都有它也就有吧,显得整齐一些。然后也没报错。不加应该也可以,没有试验。(区别是,别人再继承这个类的时候就不能重写这个方法了,显然没人要继承这个类)

    运行一下,发现还是不能运行啊!

    原因是基类根本没有捕捉timer事件。

    (总之原因都在基类WindowImplBase的源代码里)

    3.捕捉timer事件

    只需要重写下面的HandleCustomMessage方法即可

    复制代码
        virtual LRESULT HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)    {        LRESULT lRes = 0;        switch (uMsg)        {        case WM_TIMER: lRes = OnTimer(uMsg, wParam, lParam, bHandled); break;        }        //bHandled = FALSE;        return 0;    }
    复制代码

    现在也搞清楚为什么我们的OnTimer长那个鸟样了。

    运行成功!

    附上我的整个cpp,

    复制代码
    #pragma once#include <UIlib.h>using namespace DuiLib;#ifdef _DEBUG#   ifdef _UNICODE#       pragma comment(lib, "DuiLib_ud.lib")#   else#       pragma comment(lib, "DuiLib_d.lib")#   endif#else#   ifdef _UNICODE#       pragma comment(lib, "DuiLib_u.lib")#   else#       pragma comment(lib, "DuiLib.lib")#   endif#endifclass CDuiFrameWnd : public WindowImplBase{public:    virtual LPCTSTR    GetWindowClassName() const   { return _T("DUIMainFrame"); }    virtual CDuiString GetSkinFile()                { return _T("playerui.xml"); }    virtual CDuiString GetSkinFolder()              { return _T(""); }        virtual void OnClick(TNotifyUI& msg)    {        CDuiString sCtrlName = msg.pSender->GetName();        if (sCtrlName == _T("closebtn"))        {            Close();            return;        }        else if (sCtrlName == _T("minbtn"))        {            SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);            return;        }        else if (sCtrlName == _T("maxbtn"))        {            SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);            return;        }        else if (sCtrlName == _T("restorebtn"))        {            SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);            return;        }        else if (sCtrlName == _T("btnHello"))        {                        HWND hwnd = m_PaintManager.GetPaintWindow();            SetTimer(hwnd, 1, 5000,    NULL);        }        return;    }    virtual LRESULT HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)    {        LRESULT lRes = 0;        switch (uMsg)        {        case WM_TIMER: lRes = OnTimer(uMsg, wParam, lParam, bHandled); break;        }        bHandled = FALSE;        return 0;    }    virtual LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)    {        ::MessageBox(NULL, _T("AC"), _T("随便"), NULL);        bHandled = TRUE;        return 0;    }    virtual void       InitWindow()    {                                    }};int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){    CPaintManagerUI::SetInstance(hInstance);    CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());   // 设置资源的默认路径(此处设置为和exe在同一目录)    CDuiFrameWnd duiFrame;    duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);    duiFrame.CenterWindow();    duiFrame.ShowModal();    return 0;}
    复制代码

    xml(我的xml叫playerui.xml,跟大神的不一样):

    复制代码
    <?xml version="1.0" encoding="utf-8" standalone="yes" ?><Window size="875,651" 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">            <VerticalLayout />            <VerticalLayout 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;SysBtn\MinNormal.bmp&apos; " hotimage=" file=&apos;SysBtn\MinFocus.bmp&apos; " pushedimage=" file=&apos;SysBtn\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;SysBtn\MaxNormal.bmp&apos; " hotimage=" file=&apos;SysBtn\MaxFocus.bmp&apos; " pushedimage=" file=&apos;SysBtn\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;SysBtn\StoreNormal.bmp&apos; " hotimage=" file=&apos;SysBtn\StoreFocus.bmp&apos; " pushedimage=" file=&apos;SysBtn\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;SysBtn\CloseNormal.bmp&apos; " hotimage=" file=&apos;SysBtn\CloseFocus.bmp&apos; " pushedimage=" file=&apos;SysBtn\CloseFocus.bmp&apos; " />            </VerticalLayout>        </HorizontalLayout>        <HorizontalLayout width="875" height="550">            <Control name="Playcon_1" bordersize="3" float="true" pos="85,24,0,0" width="311" height="239" bordercolor="#00C0C0C0" />            <Control bordersize="3" float="true" pos="477,24,0,0" width="311" height="239" bordercolor="#00C0C0C0" />            <Control bordersize="3" float="true" pos="85,290,0,0" width="311" height="239" bordercolor="#00C0C0C0" />            <Control bordersize="3" float="true" pos="477,290,0,0" width="311" height="239" bordercolor="#00C0C0C0" />        </HorizontalLayout>        <HorizontalLayout>            <VerticalLayout />            <VerticalLayout width="115" height="39">                <Button name="btnHello" text="开始播放" bordersize="2" width="114" height="39" bkcolor="#FFFFFBF0" bkcolor2="#0000FFFF" bordercolor="#00000080" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />            </VerticalLayout>            <VerticalLayout />        </HorizontalLayout>    </VerticalLayout></Window>
    复制代码

    更多精彩,都在基类的源代码里面!

1 0
原创粉丝点击