首页 > 代码库 > Duilib学习(一)

Duilib学习(一)

#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 CWindowWnd, public INotifyUI
{
public:
    virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }
    virtual void    Notify(TNotifyUI& msg) 
    {
        if (msg.sType == _T("click"))
        {
            if (msg.pSender->GetName() == _T("btnHello"))
            {
                ::MessageBox(NULL, _T("我是按钮"), _T("点击了按钮"), NULL);
            }
        }
    }

    virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        LRESULT lRes = 0;

        if (uMsg == WM_CREATE)
        {
            // 方法1
            //CControlUI *pWnd = new CButtonUI;
            //pWnd->SetName(_T("btnHello"));   // 控件的唯一标识
            //pWnd->SetText(_T("Hello World"));   // 设置文字
            //pWnd->SetBkColor(0xFF00FF00);       // 设置背景色

            //m_PaintManager.Init(m_hWnd);
            //m_PaintManager.AttachDialog(pWnd);

            //m_PaintManager.AddNotifier(this);  // 添加控件响应消息,这样消息就会达到duilib的消息循环

            // 方法2
            m_PaintManager.Init(m_hWnd);

            CDialogBuilder builder;
            CControlUI *pRoot = builder.Create(_T("duilib.xml"), NULL, NULL, &m_PaintManager, NULL);
            ASSERT(pRoot && "Failed to parse XML");

            m_PaintManager.AttachDialog(pRoot);
            m_PaintManager.AddNotifier(this);
            return lRes;
        }
        // 以下三个消息用于屏蔽系统标题栏
        // WM_NCACTIVETE WM_NCCALCSIZE WM_NCPAINT
        else if (uMsg == WM_NCACTIVATE)
        {
            if (!::IsIconic(m_hWnd))
            {
                return (wParam == 0) ? TRUE : FALSE;
            }
        }
        else if (uMsg == WM_NCCALCSIZE)
        {
            return 0;
        }
        else if (uMsg == WM_NCPAINT)
        {
            return 0;
        }

        if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes))
        {
            return lRes;
        }

        return __super::HandleMessage(uMsg, wParam, lParam);
    }

protected:
    CPaintManagerUI m_PaintManager;
};


class CDuiXmlFrameWnd : public WindowImplBase
{
public:
    // 实现以下三个纯虚函数
    virtual LPCTSTR GetWindowClassName()const { return _T("DUIMainFrame"); }
    virtual CDuiString GetSkinFile(){ return _T("duilib.xml"); }
    virtual CDuiString GetSkinFolder(){ return _T(""); }

    // 将事件添加到消息队列
    virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        LRESULT lRes = 0;

        if (uMsg == WM_CREATE)
        {
            // 方法1
            //CControlUI *pWnd = new CButtonUI;
            //pWnd->SetName(_T("btnHello"));   // 控件的唯一标识
            //pWnd->SetText(_T("Hello World"));   // 设置文字
            //pWnd->SetBkColor(0xFF00FF00);       // 设置背景色

            //m_PaintManager.Init(m_hWnd);
            //m_PaintManager.AttachDialog(pWnd);

            //m_PaintManager.AddNotifier(this);  // 添加控件响应消息,这样消息就会达到duilib的消息循环

            // 方法2
            m_PaintManager.Init(m_hWnd);

            CDialogBuilder builder;
            CControlUI *pRoot = builder.Create(_T("duilib.xml"), NULL, NULL, &m_PaintManager, NULL);
            ASSERT(pRoot && "Failed to parse XML");

            m_PaintManager.AttachDialog(pRoot);
            m_PaintManager.AddNotifier(this);
            return lRes;
        }
        // 以下三个消息用于屏蔽系统标题栏
        // WM_NCACTIVETE WM_NCCALCSIZE WM_NCPAINT
        else if (uMsg == WM_NCACTIVATE)
        {
            if (!::IsIconic(m_hWnd))
            {
                return (wParam == 0) ? TRUE : FALSE;
            }
        }
        else if (uMsg == WM_NCCALCSIZE)
        {
            return 0;
        }
        else if (uMsg == WM_NCPAINT)
        {
            return 0;
        }

        if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes))
        {
            return lRes;
        }

        return __super::HandleMessage(uMsg, wParam, lParam);
    }

    // 实现控件响应事件
    virtual void Notify(TNotifyUI& msg)
    {
        if (msg.pSender->GetName() == _T("editHello"))
        {
            // 获取控件对象方法
            CEditUI *pEdit = static_cast<CEditUI *>(m_PaintManager.FindControl(_T("editHello")));
        }
        // 处理控件响应事件
        if (msg.sType == _T("click"))
        {
            if (msg.pSender->GetName() == _T("btnHello"))
            {
                ::MessageBox(NULL, _T("我是按钮"), _T("点击了按钮"), NULL);

                // 人为修改控件焦点
                CEditUI *pEdit2 = static_cast<CEditUI *>(m_PaintManager.FindControl(_T("editWorld")));
                m_PaintManager.SetFocus(pEdit2, TRUE);
            }
        }
    }
};

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    // 实例句柄与渲染类关联
    CPaintManagerUI::SetInstance(hInstance);

    // 设置资源的默认路径(设置和exe在同一个目录)
    CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());

    /*CDuiFrameWnd duiFrame;
    duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
    duiFrame.CenterWindow();
    duiFrame.ShowModal();*/

    // 方法2
    CDuiXmlFrameWnd duixmlFrame;
    duixmlFrame.Create(NULL, _T("DuiWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
    duixmlFrame.CenterWindow();
    duixmlFrame.ShowModal();
    
    return 0;
}

 

Duilib学习(一)