找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5187|回复: 0

DirectUI界面技术和lua脚本

[复制链接]
发表于 2012-1-18 10:14:11 | 显示全部楼层 |阅读模式
Duilib库地址
http://duilib.googlecode.com/files/duilib%20v1.1.zip


Lua官方地址, for windows.
http://luaforwindows.googlecode.com/files/LuaForWindows_v5.1.4-45.exe


将testapp修改成dll工程FrameSdk,导出5个函数#ifdef __cplusplus
  1. extern "C"
  2. {
  3. #endif
  4. FRAMESDK_API
  5. int CreateApp(HINSTANCE hInstance
  6.             , const char * xmlPath
  7.             , const char * luaPath);
  8. #ifdef __cplusplus
  9. };
  10. #endif
  11. // App.cpp : Defines the entry point for the application.
  12. //
  13. #include "stdafx.h"
  14. #include <exdisp.h>
  15. #include <comdef.h>
  16. #include <string>
  17. /**/
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "lua.h"
  22. #include "lualib.h"
  23. #include "lauxlib.h"
  24. #ifdef __cplusplus
  25. }
  26. #endif
  27. #pragma comment(lib, "lua5.1.lib")
  28. #include "FrameSdk.h"
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33.     FRAMESDK_API
  34.         int
  35.         FindControl(lua_State * L);
  36.     FRAMESDK_API
  37.         int
  38.         SetAttr(lua_State * L);
  39.     FRAMESDK_API
  40.         int
  41.         GetAttr(lua_State * L);
  42.     FRAMESDK_API
  43.         int
  44.         RegEvent(lua_State * L);
  45. #ifdef __cplusplus
  46. };
  47. #endif
  48. class CFrameWindowWnd : public CWindowWnd, public INotifyUI
  49. {
  50. public:
  51.     CFrameWindowWnd(const char * xmlPath
  52.                     , const char * luaPath)
  53.                     : m_xmlPath(xmlPath)
  54.                     , m_luaPath(luaPath)
  55.                     , m_L(NULL){ }
  56.     LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); };
  57.     UINT GetClassStyle() const { return CS_DBLCLKS; };
  58.     void OnFinalMessage(HWND /*hWnd*/) { delete this; };
  59.     void Init()
  60.     {
  61.         m_L = lua_open();
  62.         luaL_openlibs(m_L);
  63.             luaL_dofile(m_L, m_luaPath.c_str());
  64.     }
  65.     bool OnHChanged(void* param) {
  66.         TNotifyUI* pMsg = (TNotifyUI*)param;
  67.         if( pMsg->sType == _T("valuechanged") ) {
  68.             short H, S, L;
  69.             CPaintManagerUI::GetHSL(&H, &S, &L);
  70.             CPaintManagerUI::SetHSL(true, (static_cast<CSliderUI*>(pMsg->pSender))->GetValue(), S, L);
  71.         }
  72.         return true;
  73.     }
  74.     bool OnSChanged(void* param) {
  75.         TNotifyUI* pMsg = (TNotifyUI*)param;
  76.         if( pMsg->sType == _T("valuechanged") ) {
  77.             short H, S, L;
  78.             CPaintManagerUI::GetHSL(&H, &S, &L);
  79.             CPaintManagerUI::SetHSL(true, H, (static_cast<CSliderUI*>(pMsg->pSender))->GetValue(), L);
  80.         }
  81.         return true;
  82.     }
  83.     bool OnLChanged(void* param) {
  84.         TNotifyUI* pMsg = (TNotifyUI*)param;
  85.         if( pMsg->sType == _T("valuechanged") ) {
  86.             short H, S, L;
  87.             CPaintManagerUI::GetHSL(&H, &S, &L);
  88.             CPaintManagerUI::SetHSL(true, H, S, (static_cast<CSliderUI*>(pMsg->pSender))->GetValue());
  89.         }
  90.         return true;
  91.     }
  92.     bool OnAlphaChanged(void* param) {
  93.         TNotifyUI* pMsg = (TNotifyUI*)param;
  94.         if( pMsg->sType == _T("valuechanged") ) {
  95.             m_pm.SetTransparent((static_cast<CSliderUI*>(pMsg->pSender))->GetValue());
  96.         }
  97.         return true;
  98.     }
  99.     void OnPrepare()
  100.     {
  101.         CSliderUI* pSilder = static_cast<CSliderUI*>(m_pm.FindControl(_T("alpha_controlor")));
  102.         if( pSilder )
  103.             pSilder->OnNotify += MakeDelegate(this, &CFrameWindowWnd::OnAlphaChanged);
  104.         pSilder = static_cast<CSliderUI*>(m_pm.FindControl(_T("h_controlor")));
  105.         if( pSilder )
  106.             pSilder->OnNotify += MakeDelegate(this, &CFrameWindowWnd::OnHChanged);
  107.         pSilder = static_cast<CSliderUI*>(m_pm.FindControl(_T("s_controlor")));
  108.         if( pSilder )
  109.             pSilder->OnNotify += MakeDelegate(this, &CFrameWindowWnd::OnSChanged);
  110.         pSilder = static_cast<CSliderUI*>(m_pm.FindControl(_T("l_controlor")));
  111.         if( pSilder )
  112.             pSilder->OnNotify += MakeDelegate(this, &CFrameWindowWnd::OnLChanged);
  113.     }
  114.     void Notify(TNotifyUI& msg)
  115.     {
  116.         if( msg.sType == _T("windowinit") ) OnPrepare();
  117.         else if( msg.sType == _T("click") ) {
  118.             if( msg.pSender->GetName() == _T("insertimagebtn") ) {
  119.                 CRichEditUI* pRich = static_cast<CRichEditUI*>(m_pm.FindControl(_T("testrichedit")));
  120.                 if( pRich ) {
  121.                     pRich->RemoveAll();
  122.                 }
  123.             }
  124.             else if( msg.pSender->GetName() == _T("changeskinbtn") ) {
  125.                 if( CPaintManagerUI::GetResourcePath() == CPaintManagerUI::GetInstancePath() )
  126.                     CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin\\FlashRes"));
  127.                 else
  128.                     CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());
  129.                 CPaintManagerUI::ReloadSkin();
  130.             }
  131.         }
  132.     }
  133.     LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  134.     {
  135.         if( uMsg == WM_CREATE ) {
  136.             // 设置窗口无边框样式
  137.              LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  138.              styleValue &= ~WS_CAPTION ; //& ~WS_BORDER & ~WS_THICKFRAME;
  139.              ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  140.             m_pm.Init(m_hWnd);
  141.             CDialogBuilder builder;
  142.                     //CDialogBuilderCallbackEx cb;
  143.             wchar_t szXmlPath [MAX_PATH] = L"";
  144.             MultiByteToWideChar(CP_ACP, 0, m_xmlPath.c_str(), -1, szXmlPath, _countof(szXmlPath));
  145.             CControlUI* pRoot = builder.Create(szXmlPath, (UINT)0, NULL, &m_pm);
  146.             ASSERT(pRoot && "Failed to parse XML");
  147.             m_pm.AttachDialog(pRoot);
  148.             m_pm.AddNotifier(this);
  149.             // 圆角
  150.             SIZE szRoundCorner = m_pm.GetRoundCorner();
  151.             if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) ) {
  152.                 CRect rcWnd;
  153.                 ::GetWindowRect(*this, &rcWnd);
  154.                 rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  155.                 rcWnd.right++; rcWnd.bottom++;
  156.                 HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right,rcWnd.bottom
  157.                                                 , szRoundCorner.cx, szRoundCorner.cy);
  158.                 ::SetWindowRgn(*this, hRgn, TRUE);
  159.                 ::DeleteObject(hRgn);
  160.             }
  161.             Init();
  162.             return 0;
  163.         }
  164.         else if( uMsg == WM_DESTROY ) {
  165.             ::PostQuitMessage(0L);
  166.         }
  167.         else if( uMsg == WM_ERASEBKGND ) {
  168.             return 1;
  169.         }
  170.         
  171.         LRESULT lRes = 0;
  172.         if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) )
  173.             return lRes;
  174.         return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  175.     }
  176. public:
  177.     // 绘制管理器
  178.     CPaintManagerUI m_pm;
  179.     // 布局文件
  180.     std::string m_xmlPath;
  181.     // 控制脚本
  182.     std::string m_luaPath;
  183. private:
  184.     // lua脚本状态机
  185.     lua_State * m_L;
  186. };
  187. FRAMESDK_API
  188. int FindControl(lua_State * L)
  189. {
  190.     MessageBoxA(GetActiveWindow(), __FUNCTION__, __FUNCTION__, 0);
  191.     return 0;
  192. }
  193. FRAMESDK_API
  194. int
  195. SetAttr(lua_State * L)
  196. {
  197.     return 0;
  198. }
  199. FRAMESDK_API
  200. int
  201. GetAttr(lua_State * L)
  202. {
  203.     return 0;
  204. }
  205. FRAMESDK_API
  206. int
  207. RegEvent(lua_State * L)
  208. {
  209.     return 0;
  210. }
  211. FRAMESDK_API
  212. int
  213. CreateApp(HINSTANCE hInstance, const char * xmlPath, const char * luaPath)
  214. {
  215.     CPaintManagerUI::SetInstance(hInstance);
  216.     CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());
  217.     HRESULT Hr = ::CoInitialize(NULL);
  218.     if( FAILED(Hr) ) return 0;
  219.     CFrameWindowWnd* pFrame = new CFrameWindowWnd(
  220.         strrchr(xmlPath, '\\')+1
  221.         //, strrchr(luaPath, '\\')+1
  222.         , luaPath
  223.         );
  224.     if( pFrame == NULL ) return 0;
  225.     pFrame->Create(NULL
  226.         , _T("FrameApp")
  227.         , WS_VISIBLE
  228.         | WS_POPUPWINDOW
  229.         //| WS_OVERLAPPEDWINDOW
  230.         , 0L, 0, 0, 400, 300);
  231.     pFrame->CenterWindow();
  232.     pFrame->ShowWindow(true);
  233.     CPaintManagerUI::MessageLoop();
  234.     ::CoUninitialize();
  235.     return 0;
  236. }
复制代码


作者:wuzh1230 发表于2012-1-17 14:42:43 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-5 07:31 , Processed in 0.019270 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表