当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » C/C++  » 图形界面开发/GUI/CGI
蔡天宇

学习duilib的一个demo

蔡天宇 发布于 2012年06月20日 16时, 4评/10305阅
分享到: 
收藏 +0
1
基于duilib做的一个在C++下跑的demo,基本功能:调用子窗口,按键事件触发等!需要的平台:Vc,duilib v1.1。感谢duilib的分享。具体的配置请配合duilib的“入门文档”中的说明。另外请一定要先下载duilib v1.1 因为本demo中text1.xml中的几张图片是来自duilib v1.1中的Gamedemo。
标签: DuiLib

代码片段(3) [全屏查看所有代码]

1. [文件] taste.cpp ~ 9KB     下载(197)     跳至 [1] [2] [全屏预览]

#pragma once 
 
#define WIN32_LEAN_AND_MEAN   
#define _CRT_SECURE_NO_DEPRECATE 
 
#include <windows.h> 
#include <objbase.h> 


#include "..\DuiLib\UIlib.h" 
 
using namespace DuiLib; 
 
#ifdef _DEBUG 
#   ifdef _UNICODE 
#       pragma comment(lib, "..\\bin\\DuiLib_ud.lib") 
#   else 
#       pragma comment(lib, "..\\bin\\DuiLib_d.lib") 
#   endif 
#else 
#   ifdef _UNICODE 
#       pragma comment(lib, "..\\bin\\DuiLib_u.lib") 
#   else 
#       pragma comment(lib, "..\\bin\\DuiLib.lib") 
#   endif 
#endif 
 
//  窗口实例及消息响应部分 

class CLoginFrameWnd : public CWindowWnd, public INotifyUI
{
public:
    CLoginFrameWnd() { };
    LPCTSTR GetWindowClassName() const { return _T("UILoginFrame"); };
    UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; };
    void OnFinalMessage(HWND /*hWnd*/) { delete this; };

    void Init() {
        CComboUI* pAccountCombo = static_cast<CComboUI*>(m_pm.FindControl(_T("accountcombo")));
        CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
        if( pAccountCombo && pAccountEdit ) pAccountEdit->SetText(pAccountCombo->GetText());
    }

    void Notify(TNotifyUI& msg)
    {
        if( msg.sType == _T("click") ) 
		{
            if( msg.pSender->GetName() == _T("closebtn") ) 
			{ 
				Close(); 
				return; 
			}
            else if( msg.pSender->GetName() == _T("loginBtn") ) 
			{ 
				Close(); 
				return; 
			}
        }
    }

    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
        styleValue &= ~WS_CAPTION;
        ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
        RECT rcClient;
        ::GetClientRect(*this, &rcClient);
        ::SetWindowPos(*this, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, \
            rcClient.bottom - rcClient.top, SWP_FRAMECHANGED);

        m_pm.Init(m_hWnd);
        CDialogBuilder builder;

        CControlUI* pRoot = builder.Create(_T("login.xml"), (UINT)0, NULL, &m_pm);
        ASSERT(pRoot && "Failed to parse XML");
        m_pm.AttachDialog(pRoot);
        m_pm.AddNotifier(this);

        SIZE szInitWindowSize = m_pm.GetInitSize();
        if( szInitWindowSize.cx != 0 ) {
            ::SetWindowPos(*this, NULL, 0, 0, szInitWindowSize.cx, szInitWindowSize.cy, SWP_NOZORDER | SWP_NOMOVE);
        }

        Init();
        return 0;
    }

    LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        return (wParam == 0) ? TRUE : FALSE;
    }

    LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        return 0;
    }

    LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        return 0;
    }

    LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
        ::ScreenToClient(*this, &pt);

        RECT rcClient;
        ::GetClientRect(*this, &rcClient);

        RECT rcCaption = m_pm.GetCaptionRect();
        if( pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
            && pt.y >= rcCaption.top && pt.y < rcCaption.bottom ) {
                CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
                if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 )
                    return HTCAPTION;
        }

        return HTCLIENT;
    }

    LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        SIZE szRoundCorner = m_pm.GetRoundCorner();
        if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) ) {
            RECT rcClient;
            ::GetClientRect(*this, &rcClient);
            RECT rc = { rcClient.left, rcClient.top + szRoundCorner.cx, rcClient.right, rcClient.bottom };
            HRGN hRgn1 = ::CreateRectRgnIndirect( &rc );
            HRGN hRgn2 = ::CreateRoundRectRgn(rcClient.left, rcClient.top, rcClient.right + 1, rcClient.bottom - szRoundCorner.cx, szRoundCorner.cx, szRoundCorner.cy);
            ::CombineRgn( hRgn1, hRgn1, hRgn2, RGN_OR );
            ::SetWindowRgn(*this, hRgn1, TRUE);
            ::DeleteObject(hRgn1);
            ::DeleteObject(hRgn2);
        }

        bHandled = FALSE;
        return 0;
    }

    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        LRESULT lRes = 0;
        BOOL bHandled = TRUE;
        switch( uMsg ) {
        case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
        case WM_NCACTIVATE:    lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
        case WM_NCCALCSIZE:    lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
        case WM_NCPAINT:       lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
        case WM_NCHITTEST:     lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
        case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
        default:
            bHandled = FALSE;
        }
        if( bHandled ) return lRes;
        if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
        return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
    }

public:
    CPaintManagerUI m_pm;
};


class CFrameWindowWnd : public CWindowWnd, public INotifyUI 
{ 
public: 
     CFrameWindowWnd()
	 { 
	 
	 }; 

     LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); }; 
     
	 UINT GetClassStyle() const { return UI_CLASSSTYLE_FRAME | CS_DBLCLKS; }; 
     
	 void OnFinalMessage(HWND /*hWnd*/) { delete this; }; 
     void Prepare()
	 {
		CListUI* gamelist = static_cast<CListUI*>(m_pm.FindControl(_T("gamelist")));
		
	 }

	 void Notify(TNotifyUI& msg) 
     { 
		 if(msg.sType == _T("windowinnit"))
		{
			Prepare(); 
		 }
         else if( msg.sType == _T("click") ) { 
             if( msg.pSender->GetName() == _T("closebtn") )
			 { 
                 Close(); 
             } 
			 else  if( msg.pSender->GetName() == _T("changeskinbtn") )
			 {
				 		CLoginFrameWnd* pLoginFrame = new CLoginFrameWnd();
						if( pLoginFrame == NULL )
						{ 
							Close(); 
							return; 
						}
						pLoginFrame->Create(m_hWnd, _T(""), UI_WNDSTYLE_DIALOG, UI_WNDSTYLE_EX_DIALOG, 0, 0, 0, 0, NULL);
						pLoginFrame->CenterWindow();
						pLoginFrame->ShowModal();
             } 
         } 
		 else if(msg.sType == _T("selectchanged"))
		 {
			 if(msg.pSender->GetName() == _T("OptionUI1"))
			 {   

				 CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("testedit")));
				 pAccountEdit->SetText(_T("进入游戏大厅"));
			 }
			 if(msg.pSender->GetName() == _T("OptionUI2"))
			 {
				CEditUI* pAccountEdit_1 = static_cast<CEditUI*>(m_pm.FindControl(_T("testedit")));
			     pAccountEdit_1->SetText(_T("进入游戏商城"));
			 }
			 if(msg.pSender->GetName() == _T("OptionUI3"))
			 {
				CEditUI* pAccountEdit_1 = static_cast<CEditUI*>(m_pm.FindControl(_T("testedit")));
			     pAccountEdit_1->SetText(_T("进入会员中心"));
			 }
			 if(msg.pSender->GetName() == _T("OptionUI4"))
			 {
				CEditUI* pAccountEdit_1 = static_cast<CEditUI*>(m_pm.FindControl(_T("testedit")));
			     pAccountEdit_1->SetText(_T("进入客户论坛"));
			 }
		 }
		
     } ;
 
     LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) 
     { 
         if( uMsg == WM_CREATE )
		 { 
			 m_pm.Init(m_hWnd); 
             CDialogBuilder builder; 
             CControlUI* pRoot = builder.Create(_T("test1.xml"), (UINT)0, NULL, &m_pm); 
             ASSERT(pRoot && "Failed to parse XML"); 
             m_pm.AttachDialog(pRoot); 
             m_pm.AddNotifier(this); 
             return 0; 
		 } 
         else if( uMsg == WM_DESTROY ) 
		 { 
             ::PostQuitMessage(0); 
         } 
         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; 
         } 
         LRESULT lRes = 0; 
         if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes; 
         return CWindowWnd::HandleMessage(uMsg, wParam, lParam); 
	 } ;
public: 
     CPaintManagerUI m_pm; 
}; 

//  程序入口及Duilib初始化部分 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow) 
{ 
     CPaintManagerUI::SetInstance(hInstance); 
     CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); 
 
     CFrameWindowWnd* pFrame = new CFrameWindowWnd(); 
     if( pFrame == NULL ) return 0; 
     pFrame->Create(NULL, _T("测试"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); 
     pFrame->ShowWindow(true); 
     CPaintManagerUI::MessageLoop();  
     return 0; 
} 

2. [文件] login.xml ~ 4KB     下载(165)     跳至 [1] [2] [全屏预览]

<?xml version="1.0" encoding="UTF-8"?>
<Window size="308,228" caption="0,0,0,40" roundcorner="8,8" >
	<Default name="VScrollBar" value="button1normalimage=&quot;file='scrollbar.bmp' source='0,90,16,106' mask='#FFFF00FF'&quot; button1hotimage=&quot;file='scrollbar.bmp' source='18,90,34,106' mask='#FFFF00FF'&quot; button1pushedimage=&quot;file='scrollbar.bmp' source='36,90,52,106' mask='#FFFF00FF'&quot; button1disabledimage=&quot;file='scrollbar.bmp' source='54,90,70,106' mask='#FFFF00FF'&quot; button2normalimage=&quot;file='scrollbar.bmp' source='0,108,16,124' mask='#FFFF00FF'&quot; button2hotimage=&quot;file='scrollbar.bmp' source='18,108,34,124' mask='#FFFF00FF'&quot; button2pushedimage=&quot;file='scrollbar.bmp' source='36,108,52,124' mask='#FFFF00FF'&quot; button2disabledimage=&quot;file='scrollbar.bmp' source='54,108,70,124' mask='#FFFF00FF'&quot; thumbnormalimage=&quot;file='scrollbar.bmp' source='0,126,16,142' corner='2,2,2,2' mask='#FFFF00FF'&quot; thumbhotimage=&quot;file='scrollbar.bmp' source='18,126,34,142' corner='2,2,2,2' mask='#FFFF00FF'&quot; thumbpushedimage=&quot;file='scrollbar.bmp' source='36,126,52,142' corner='2,2,2,2' mask='#FFFF00FF'&quot; thumbdisabledimage=&quot;file='scrollbar.bmp' source='54,126,70,142' corner='2,2,2,2' mask='#FFFF00FF'&quot; railnormalimage=&quot;file='scrollbar.bmp' source='0,144,16,160' corner='2,2,2,2' mask='#FFFF00FF'&quot; railhotimage=&quot;file='scrollbar.bmp' source='18,144,34,160' corner='2,2,2,2' mask='#FFFF00FF'&quot; railpushedimage=&quot;file='scrollbar.bmp' source='36,144,52,160' corner='2,2,2,2' mask='#FFFF00FF'&quot; raildisabledimage=&quot;file='scrollbar.bmp' source='54,144,70,160' corner='2,2,2,2' mask='#FFFF00FF'&quot; bknormalimage=&quot;file='scrollbar.bmp' source='0,162,16,178' corner='2,2,2,2' mask='#FFFF00FF'&quot; bkhotimage=&quot;file='scrollbar.bmp' source='18,162,34,178' corner='2,2,2,2' mask='#FFFF00FF'&quot; bkpushedimage=&quot;file='scrollbar.bmp' source='36,162,52,178' corner='2,2,2,2' mask='#FFFF00FF'&quot; bkdisabledimage=&quot;file='scrollbar.bmp' source='54,162,70,178' corner='2,2,2,2' mask='#FFFF00FF'&quot; " />
	<VerticalLayout inset="4,0,4,4" bkcolor="#FF60B1FE" bordercolor="#FF1B4876" bordersize="1" >
		<Control height="1" bkcolor="#FF1B4876" />
		<Control height="2" bkcolor="#FFD1EDFF" />
		<HorizontalLayout height="26" inset="2,2,2,0" bkcolor="#FF8DD3FF" bkcolor2="#FF60B1FE">
			<Label text="游戏登录" width="80" />
			<Control />
			<Button name="closebtn" width="42" maxheight="18" tooltip="点击这里直接退出游戏" normalimage="file='frame_btn_close_normal.bmp' mask='#FFFF00FF'" hotimage="file='frame_btn_close_hot.bmp' mask='#FFFF00FF'" pushedimage="file='frame_btn_close_down.bmp' mask='#FFFF00FF'"/>
		</HorizontalLayout>
		<Control height="60" bkcolor="#FFFF0000" />
		<Progress height="10" bkcolor="#FFF8B343" />
		<Container bkcolor="#FFFFFFFF" >
			<Label text="账号:" float="true" pos="10, 20, 78, 42" />
			<Combo name="accountcombo" float="true" pos="80, 20, 220, 42" tooltip="请点击这里选择您准备使用的账号" normalimage="file='Combo_nor.bmp' corner='2,2,24,2'"  >
				<ListLabelElement text="张三" selected="true" />
				<ListLabelElement text="李四" />
			</Combo>
			<Button text="{u}{a}注册账号{/a}{/u}" showhtml="true" float="true" pos="230, 20, 300, 42" tooltip="如果你还没有账号,请点击注册一个账号" />
			<Edit name="accountedit" float="true" pos="80, 21, 200, 41" tooltip="请在这里输入您的账号" />
			<Label text="密码:" float="true" pos="10, 55, 78, 75" />
			<Edit name="pwdedit" float="true" pos="80, 55, 220, 75" password="true" tooltip="请在这里输入您的密码" />
			<Button text="{u}{a}找回密码{/a}{/u}" showhtml="true" float="true" pos="230, 55, 300, 75" tooltip="如果你忘记了密码,请点击找回密码"/>
			<Button name="loginBtn" text="登录" float="true" pos="180, 80, 240, 100" tooltip="点击这里登录游戏" />
		</Container>
    </VerticalLayout>
</Window>

3. [文件] test1.XML ~ 2KB     下载(188)     [全屏预览]



开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(4)

  • 1楼:pj220 发表于 2012-06-21 09:34 回复此评论

    无图无真相啊

  • 2楼:rual 发表于 2012-10-25 19:02 回复此评论
    可以问你一个问题吗? 我下载的v1.1  里面有几个demo  但是demo的文件夹里面没有 dsw文件之类的东西  我怎么把工程建起来  ?  而且里面为什么没有。xml文件 呢 ? 不好意思 我是小白 刚接触   求讲解 
  • 3楼:蔡天宇 发表于 2012-10-25 21:45 回复此评论

    引用来自“rual”的评论

    可以问你一个问题吗? 我下载的v1.1  里面有几个demo  但是demo的文件夹里面没有 dsw文件之类的东西  我怎么把工程建起来  ?  而且里面为什么没有。xml文件 呢 ? 不好意思 我是小白 刚接触   求讲解 
    http://download.csdn.net/download/cbangyu/4419555 这是我学习duilib时写的一个demo,可以下下来看看。另外最好找到官方的“Duilib 入门文档”,按照上面的说法进行学习。另外duilib的作者在csdn上有自己的博客的,可以去他博客上看看。
  • 4楼:纳兰清风 发表于 2014-04-01 17:37 回复此评论
    怎么实现从SHowModal返回后父窗口不退出?
开源从代码分享开始 分享代码
蔡天宇的其它代码 全部(2)...