- Basic framework
A simple Duilib program is generally as follows:
// Duilib use settings section #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
//Window instance and message response part
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 ) { m_pm.Init(m_hWnd); CControlUI *pButton = new CButtonUI; pButton->SetName(_T("closebtn")); pButton->SetBkColor(0xFFFF0000); m_pm.AttachDialog(pButton); m_pm.AddNotifier(this); return 0; } else if( uMsg == WM_DESTROY ) { ::PostQuitMessage(0); } LRESULT lRes = 0; if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes; return CWindowWnd::HandleMessage(uMsg, wParam, lParam); } public: CPaintManagerUI m_pm; }; // Program entry and Duilib initialization 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("test"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); pFrame->ShowWindow(true); CPaintManagerUI::MessageLoop(); return 0; }
As you can see, this program is divided into three parts:
- Duilib uses the settings section, which is all the header files needed to use duilib and automatically links to the corresponding duilib library. Generally speaking, it is basically unchanged.
- Window instance and message response part, basic window implementation class and simple message response need to focus on event handling in void notify (tnotifyui & MSG), which is the most important part of the Duilib program.
- Program entry and Duilib initialization, Duilib initialization and window creation.
When compiling this program, the following effects will appear:
Picture 1
Click anywhere in the red area and the window will close immediately. In this way, we have finished the simplest program of Duilib. Although this example can't show the power of Duilib, it's also a sparrow, small and full of courage. Because many beautiful interfaces don't use the title bar and border of the system, which are not drawn in the client area, we also drop this and modify it
CFrameWindowWnd:: HandleMessage by: LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { if( uMsg == WM_CREATE ) { m_pm.Init(m_hWnd); CControlUI *pButton = new CButtonUI; 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); }
After compiling and running, we get the following results:
Picture 2