duilib 界面庫 實現timer定時器

看了大神介紹的duilib感受已被同齡人狠狠地甩在背後。因此痛下決心,以後要多花時間寫代碼。html

大神教程傳送門:java

http://www.cnblogs.com/Alberl/p/3341956.htmlc++

如今的問題是想基於duilib實現一個timer定時器。工程基礎大概是在函數

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

由於本身的東西是基於大神的東西寫的,因此要把大神的教程看得差很少才知道我在說什麼。O(∩_∩)O~~spa

前臺大概長這個樣子:rest

 

稍微修改了一下就是這樣了,很簡陋,只是爲了說明問題。(很佩服本身裝做不少人會看的樣子n(*≧▽≦*)n)code

這裏引用大神的後臺代碼:orm

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秒被調用一次。xml

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
#endif

class 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>

更多精彩,都在基類的源代碼裏面!

相關文章
相關標籤/搜索