使用Duilib做win32应用的一个小示例

来源:赵克立博客 分类: C/C++ 标签:duilib发布时间:2014-07-09 13:07:00最后更新:2014-07-09 15:21:10浏览:5229
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2014-07-09 15:21:10
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

下面先写一个简单的win32弹出信息示例

新建一个空的win32项目,新建一个main.cpp文件,将以下代码复制进去:


#include <windows.h>
#include <tchar.h>

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    ::MessageBox(NULL, _T("Hello World !"), NULL, NULL);
    return 0;
}
下面就使用duilib写一个win32程序




#pragma once 

#define WIN32_LEAN_AND_MEAN   
#define _CRT_SECURE_NO_DEPRECATE 

#include  
#include  


#include "..\\DuiLib\\UIlib.h" 

using namespace DuiLib; 

#ifdef _DEBUG 
#   ifdef _UNICODE 
#       pragma comment(lib, "..\\DuiLib\\bin\\DuiLib_ud.lib") 
#   else 
#       pragma comment(lib, "..\\DuiLib\\bin\\DuiLib_d.lib") 
#   endif 
#else 
#   ifdef _UNICODE 
#       pragma comment(lib, "..\\DuiLib\\bin\\DuiLib_u.lib") 
#   else 
#       pragma comment(lib, "..\\DuiLib\\bin\\DuiLib.lib") 
#   endif 
#endif 
// 窗口实例及消息响应部分
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 Notify(TNotifyUI& msg)
    {
        if( msg.sType == _T("click") ) {
            if( msg.pSender->GetName() == _T("closebtn") ) {
                Close();
            }
        }
    }

    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        if( uMsg == WM_CREATE ) {
			//使用xml界面
			 //m_pm.Init(m_hWnd);
    //        CDialogBuilder builder;
    //        CControlUI* pRoot = builder.Create(_T("test.xml"), (UINT)0, NULL, &m_pm);
    //        ASSERT(pRoot && "Failed to parse XML");
    //        m_pm.AttachDialog(pRoot);
    //        m_pm.AddNotifier(this);

			////使用默认界面
            m_pm.Init(m_hWnd);
            CControlUI *pButton = new CButtonUI;
			pButton->SetText(_T("Hello World"));   // 设置文字
            pButton->SetName(_T("closebtn"));
            pButton->SetBkColor(0xFFFF0000);
            m_pm.AttachDialog(pButton);
            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;
}
下面是效果



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python