DuiLib(1)——简单的win32窗口

来源:互联网 发布:2017世乒赛丁宁知乎 编辑:程序博客网 时间:2024/06/07 16:37

资源下载

https://yunpan.cn/cqF6icWRN5CTc  访问密码 92e3


注:DUILIB库.7z 是vs2015下编译好的动态库及静态库,如上图所示

一、新建一个win32工程

项目设置中选择:debug,常规中:全程无优化-全程无优化,多线程调试 (/MTd);

我的项目选择的是静态编译,使用的是静态库,就不需要带duilib.dll文件了

代码如下:

#include <windows.h>#include <tchar.h>#include "UIlib.h"using namespace DuiLib;#ifdef _DEBUG#pragma comment(lib,"DuiLib_ud.lib")#else#pragma comment(lib,"DuiLib_u.lib")#endifclass CDuiFrameWnd:public CWindowWnd, public INotifyUI {public:virtual LPCTSTR GetWindowClassName()const { return _T("DuiFrameWnd"); }virtual void Notify(TNotifyUI& msg) {}virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) {LRESULT lres = 0;if (uMsg == WM_CREATE) {CControlUI* pWnd = new CButtonUI;pWnd->SetText(_T("Hello World!"));pWnd->SetBkColor(0xFF00FF00);pWnd->SetBkColor2(0xFF900000);m_paintManager.Init(m_hWnd);m_paintManager.AttachDialog(pWnd);return lres;}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 nShowCmd){CPaintManagerUI::SetInstance(hInstance);CDuiFrameWnd duiFrame;duiFrame.Create(NULL, _T("DuiTest"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);duiFrame.ShowModal();return 0;}

运行的结果图

中间一大块是按钮,下面给它添加一个点击相应事件

首先给这个按钮,取一个名字

pWnd->SetName(_T("btnHello"));

再把当前的主窗口送到管家的消息处理部门,等待处理

m_paintManager.AddNotifier(this);

再重写UI的消息处理函数,管家的消息处理部门会在有消息时通知本窗口的消息处理函数

virtual void Notify(TNotifyUI& msg) {if (msg.sType == _T("click")){if (msg.pSender->GetName() == _T("btnHello")) {::MessageBox(NULL, _T("我是按钮,再点就关闭"), _T("点击了按钮"), NULL);PostQuitMessage(0);}}}

如果要屏蔽自带的标题栏可以这么做

在HandleMessage函数中,去掉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;}

使用xml文件做界面,修改uMsg == WM_CREATE中的部分

if (uMsg == WM_CREATE) {m_paintManager.Init(m_hWnd);CDialogBuilder dlg;CControlUI* dlgUI = dlg.Create(_T("skin.xml"),(UINT)0,NULL,&m_paintManager);m_paintManager.AttachDialog(dlgUI);m_paintManager.AddNotifier(this);return lres;}
再修改一下资源的存放路径为本exe的目录

CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());

skin.xml文件内容如下,保存为utf8格式,放在exe目录下

<?xml version="1.0" encoding="UTF-8"?><Window size="800,600"> <!-- 窗口的初始尺寸 -->    <HorizontalLayout bkcolor="#FF00FF00"> <!-- 整个窗口的背景 -->        <Button name="btnHello" text="Hello World"/> <!-- 按钮的属性,如名称、文本 -->    </HorizontalLayout></Window>

上面我们使用的是DuiLib的原生的window父类,这些动作其实以及被封装好了,我们直接继承WindowImplBase这个类就可以了,代码更简单了,如下

#include "UIlib.h"using namespace DuiLib;#ifdef _DEBUG#pragma comment(lib,"DuiLib_ud.lib")#else#pragma comment(lib,"DuiLib_u.lib")#endifclass CDuiFrameWnd:public WindowImplBase{public:virtual LPCTSTR GetWindowClassName()const { return _T("DuiFrameWnd"); }virtual CDuiString GetSkinFile() { return _T("skin.xml"); }virtual CDuiString GetSkinFolder() { return _T(""); }};int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd){CPaintManagerUI::SetInstance(hInstance);CDuiFrameWnd duiFrame;duiFrame.Create(NULL, _T("DuiTest"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);duiFrame.CenterWindow();duiFrame.ShowModal();return 0;}

使用zip文件作为资源,把所有文件压缩成zip,添加到资源中,资源名字为ZIPRES

virtual UILIB_RESOURCETYPE GetResourceType() const {return UILIB_ZIPRESOURCE;}virtual LPCTSTR GetResourceID() const {return MAKEINTRESOURCE(IDR_ZIPRES1);}




0 0
原创粉丝点击