duilib github地址:https://github.com/Arikes/duilib
开发环境vs2015
Demo1:
#pragma once
#include <windows.h>
#include <objbase.h>
#include "..\DuiLib\UIlib.h"
using namespace DuiLib;
#ifdef _DEBUG
# ifdef _UNICODE
# pragma comment(lib, "..\\Lib\\DuiLib_ud.lib")
# else
# pragma comment(lib, "..\\Lib\\DuiLib_d.lib")
# endif
#else
# ifdef _UNICODE
# pragma comment(lib, "..\\Lib\\DuiLib_u.lib")
# else
# pragma comment(lib, "..\\Lib\\DuiLib.lib")
# endif
#endif
using namespace DuiLib;
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)
{
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);
return lRes;
}
以下3个消息WM_NCACTIVATE、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;
};
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.ShowModal();
return 0;
}
其中:
类 INotifyUI:
窗口中按键消息回调处理函数,继承此类必须实现如下接口:
virtual void Notify(TNotifyUI& msg) = 0;
类CWindowWnd:
该类为窗口基类,必须实现如下接口:
virtual LPCTSTR GetWindowClassName() const = 0;
窗口消息处理函数,如窗体创建,窗体销毁:
virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam){}
UI管理器,窗体的控件:
CPaintManagerUI m_PaintManager;
如果有按键处理,需要
pWnd->SetName(_T("btnHello"));
m_PaintManager.AddNotifier(this);
然后再在Notify()函数中添加如下处理:
virtual void Notify(TNotifyUI& msg)
{
if (msg.sType == _T("click"))
{
if (msg.pSender->GetName() == _T("btnHello"))
{
::MessageBox(NULL, _T("我是按钮"), _T("点击了按钮"), NULL);
}
}
}