duilib入门

duilib入门
git地址https://github.com/duilib/duilib

环境vs2017
编译错误:
duilib提示缺少v120_xp,
右键DuiLib.vcxproj用notepad++打开,把所有的v120_xp替换为v141
右键DuiLib_Static.vcxproj用notepad++打开,把所有的v120_xp替换为v141

提示无法打开StdAfx.h
路径问题,包含StdAfx.h的cpp文件和StdAfx.h不在同一目录。添加StdAfx.h所在目录到包含目录
C/C++->常规->附加包含目录
.\
.\就是工程文件所在目录,项目中StdAfx.h和工程文件在同一目录

提示代码页错误
cpp文件用记事本打开,另存为ascii格式(notepad可以用不同编码格式查看,但是保存后还是utf8)

QQDemo运行起来后,修改签名,中文变成韩文了,看不懂的韩文
编译unicode版本的duilib库,然后编译unicode版本的QQDemo

代码:
新增Windows桌面应用程序

cpp文件

#pragma once
#include "UIlib.h"
using namespace DuiLib;

#ifdef _DEBUG
#       pragma comment(lib, "DuiLib_d.lib")
#else
#       pragma comment(lib, "DuiLib.lib")
#endif

class CDuiFrameWnd : public CWindowWnd, public INotifyUI
{
public:
	virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }
	virtual void    Notify(TNotifyUI& msg) {
		if (msg.sType == DUI_MSGTYPE_CLICK)
		{
			if (msg.pSender->GetName() == _T("float button"))
			{
				MessageBox(NULL, msg.pSender->GetName(), msg.sType, MB_OK);
			}
		}
	}

	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		LRESULT lRes = 0;
		switch (uMsg)
		{
		case WM_CREATE:
		{
			initWindow();
			
			return lRes;
		}
		case WM_LBUTTONDOWN:
		{
			break;
		}
		default:
			break;
		}

		if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes))
		{
			return lRes;
		}

		return __super::HandleMessage(uMsg, wParam, lParam);
	}

private:
	void initWindow();

protected:
	CPaintManagerUI m_PaintManager;
};

void CDuiFrameWnd::initWindow()
{
	//代码创建控件
	//CContainerUI *pWnd = new CContainerUI;
	//pWnd->SetName(_T("wnd"));
	//pWnd->SetText(_T("Hello World"));   // 设置文字
	//pWnd->SetBkColor(0xffd8d8d8);       // 设置背景色
	//pWnd->SetBorderSize(2);
	//pWnd->SetBorderColor(0xffff6321);
	//pWnd->SetBorderRound({ 20, 20 });
	//pWnd->SetMaxWidth(200);

	//CButtonUI *pButton = new CButtonUI;
	//pButton->SetText(_T("buttton buttton button"));
	//pButton->SetBkColor(0xffff6321);
	//pButton->SetBkColor2(0xffd8d8d8);
	//pButton->SetPadding({30, 40, 0, 0});
	//pWnd->Add(pButton);

	//CButtonUI *pButton2 = new CButtonUI;
	//pButton2->SetFloat(true);
	//pButton2->SetText(_T("float button"));
	//pButton2->SetName(_T("float button"));
	//pButton2->SetBkColor(0x3ff4f5f9);
	//pButton2->SetFixedWidth(150);
	//pButton2->SetFixedHeight(100);
	//pButton2->SetFixedXY({100, 100});
	//pButton2->SetPos({100, 100, 250, 200});
	//pWnd->Add(pButton2);

	//m_PaintManager.Init(m_hWnd);
	//m_PaintManager.AttachDialog(pWnd);
	//m_PaintManager.AddNotifier(this);

	//解析xml文件创建控件
	m_PaintManager.Init(m_hWnd);
	//从xml中加载界面
	CDialogBuilder builder;
	CControlUI* pRoot = builder.Create(_T("test.xml"), (UINT)0, NULL, &m_PaintManager);
	m_PaintManager.AttachDialog(pRoot);
	m_PaintManager.AddNotifier(this);

	CVerticalLayoutUI* pShortMsgList = static_cast<CVerticalLayoutUI*>(m_PaintManager.FindControl(_T("videoArea")));
	CScrollBarUI* pShortListVer = pShortMsgList->GetVerticalScrollBar();
}

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;
}

mian函数中几乎是固定的代码,除非有额外的逻辑

写一个继承自public CWindowWnd, public INotifyUI的类,

动态创建控件:

	//代码创建控件
	CContainerUI *pWnd = new CContainerUI;
	pWnd->SetName(_T("wnd"));
	pWnd->SetText(_T("Hello World"));   // 设置文字
	pWnd->SetBkColor(0xffd8d8d8);       // 设置背景色
	pWnd->SetBorderSize(2);
	pWnd->SetBorderColor(0xffff6321);
	pWnd->SetBorderRound({ 20, 20 });
	pWnd->SetMaxWidth(200);

	CButtonUI *pButton = new CButtonUI;
	pButton->SetText(_T("buttton buttton button"));
	pButton->SetBkColor(0xffff6321);
	pButton->SetBkColor2(0xffd8d8d8);
	pButton->SetPadding({30, 40, 0, 0});
	pWnd->Add(pButton);

	CButtonUI *pButton2 = new CButtonUI;
	pButton2->SetFloat(true);
	pButton2->SetText(_T("float button"));
	pButton2->SetName(_T("float button"));
	pButton2->SetBkColor(0x3ff4f5f9);
	pButton2->SetFixedWidth(150);
	pButton2->SetFixedHeight(100);
	pButton2->SetFixedXY({100, 100});
	pButton2->SetPos({100, 100, 250, 200});
	pWnd->Add(pButton2);

	m_PaintManager.Init(m_hWnd);
	m_PaintManager.AttachDialog(pWnd);
	m_PaintManager.AddNotifier(this);

动态创建控件就是忘Contain中add新控件,所以最好以CContainerUI作为Root控件

AttachDialog就是设置一个Root控件

CContainerUI *pWnd = (CContainerUI*)m_PaintManager.GetRoot();就是获取Root控件,类型转换要看情况

如果以xml文件加载

<?xml version="1.0" encoding="UTF-8"?>

<Window size="800,600" mininfo="800,600" > 
<Default name="VScrollBar" value="bkcolor=&quot;#ffa0a0a0&quot;" />

	
    <HorizontalLayout bkcolor="#ffececec" hscrollbar="true" vscrollbar="true"> 
		<VerticalLayout name="videoArea" width="220" vscrollbar="true" vscrollbarstyle="thumbnormalimage=&quot;fullPrice.png&quot; width=&quot;8&quot;  height=&quot;110&quot;">
			<VerticalLayout name="videoItem1" width="220" height="250">
				<GifAnim name="video" bkimage="1.gif" height="200" padding="10, 10, 10, 0"/>
				<Button name="name" height="30" bkcolor="#ffa0a0a0" text="老师:小伙子" textcolor="#ffff6321" padding="10, 0, 10, 0" />
				<Button name="" bkimage="jingyin_nor.png" float="true" pos="10, 10,, 60, 60" width="80" height="80" />
			</VerticalLayout>
			
			<VerticalLayout name="videoItem1" width="200" padding="0, 10, 0, 0" height="250">
				<GifAnim name="video" bkimage="1.gif" height="180" padding="10, 10, 10, 0"/>
				<Button name="name" height="30" bkcolor="#ffa0a0a0" text="学生:爱新觉罗·努尔哈赤" padding="10, 0, 10, 0" />
				<Button name="" bkimage="jingyin_nor.png" float="true" pos="10, 10,, 60, 60" width="80" height="80" />
			</VerticalLayout>
			
			<VerticalLayout name="videoItem1" width="200" padding="0, 10, 0, 0" height="250">
				<GifAnim name="video" bkimage="1.gif" height="180" padding="10, 10, 10, 0"/>
				<Button name="name" height="30" bkcolor="#ffa0a0a0" text="学生:铁木真" padding="10, 0, 10, 0" />
				<Button name="" bkimage="jingyin_nor.png" float="true" pos="10, 10,, 60, 60" width="80" height="80" />
			</VerticalLayout>
			
			<VerticalLayout name="videoItem1" width="200" padding="0, 10, 0, 0" height="250">
				<GifAnim name="video" bkimage="1.gif" height="180" padding="10, 10, 10, 0"/>
				<Button name="name" height="30" bkcolor="#ffa0a0a0" text="学生:拿破仑" padding="10, 0, 10, 0" />
				<Button name="" bkimage="jingyin_nor.png" float="true" pos="10, 10,, 60, 60" width="80" height="80" />
			</VerticalLayout>
			
			<VerticalLayout name="videoItem1" width="200" padding="0, 10, 0, 0" height="250">
				<Button name="test" text="哔哩哔哩"/>
			</VerticalLayout>
		</VerticalLayout>
		
		<HorizontalLayout name="canvasArea" bkcolor="#ffff6321" bkcolor2="7fa0a0a0">
			<VerticalLayout />
			
			<VerticalLayout name="tools" width="50">
				<VerticalLayout />
				
				<VerticalLayout width="50" height="300" bkcolor="7ff0f0f0">
					<Button bkimage="1.png" height="40" padding="5,10,5,10" />
					<Button bkimage="2.png" height="40" padding="5,10,5,10" />
					<Button bkimage="3.png" height="40" padding="5,10,5,10" />
					<Button bkimage="4.png" height="40" padding="5,10,5,10" />
					<Button bkimage="5.png" height="40" padding="5,10,5,10" />
				</VerticalLayout>
				<VerticalLayout />
			</VerticalLayout>
		</HorizontalLayout>
	
    </HorizontalLayout>
</Window>

size设置初始大小

mininfo设置最小大小

default设置默认类型双引号要用&quot;转义

xml具体配置

转载自:http://blog.sina.com.cn/s/blog_400a019b0100uxdt.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值