duilib基本布局学习(模仿百度网盘)及绑定按钮事件

使用的网易版本的duilib:

https://github.com/netease-im/NIM_Duilib_Framework

重写demo中basic.xml页面的布局,熟悉布局语法,类似于html语言,这里模仿百度网盘的UI:
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<Window size="662,442" caption="0,0,0,35">
<VBox>

  <VBox bkcolor="bk_wnd_darkcolor">
    <HBox width="stretch" height="35" bkcolor="">
      <Control />
      <Button class="btn_wnd_min" name="minbtn" margin="4,6,0,0"/>
      <Box width="21" margin="4,6,0,0">
        <Button class="btn_wnd_max" name="maxbtn"/>
        <Button class="btn_wnd_restore" name="restorebtn" visible="false"/>
      </Box>
      <Button class="btn_wnd_close" name="closebtn" margin="4,6,8,0"/>
	</HBox>
    
	<HBox width="stretch" height = "60">
		<Control />
		<Label bkimage = "logo.png"/>
		<Control />
    </HBox>
	
	<HBox width="stretch" bkcolor="white">
		<Control width="70"/>
		
		<VBox valign="center" halign="center">
			<Control height = "40"/>
			<Label name="tooltip" text="扫一扫登录"/>
			<Control height = "20"/>
			<Label name="tooltip" text="使用百度APP扫码登录" normaltextcolor="blue"/>
			<Control height = "20"/>
			<Label bkimage = "QrCode.png" />
			<Control height = "20"/>
			<Button text ="刷新二维码" name = "refresh_code" height = "30" width = "100" bordercolor="blue" bordersize="1" normaltextcolor="blue"/>
		 </VBox>
		 <Control width = "30"/>
		 <VBox valign="center" halign="center">
			<Control height = "40"/>
			<HBox width="stretch" bkcolor="white">
				<Label  text="账号密码登录"/>
				<Control width = "30"/>
				<Label  text="短信快捷登录>" normaltextcolor="blue" />
			</HBox>
			<RichEdit name="name" multiline="false" bordersize="1" bordercolor="blue" height = "30" width = "220"autohscroll="true"/>
			<Control height = "10"/>
			<RichEdit name="password" multiline="false" bordersize="1" bordercolor="blue" height = "30" width = "220" autohscroll="true" password="true"/>
			<Control height = "10"/>
			<HBox width="stretch" bkcolor="white">
				<CheckBox />
				<Label  text="记住密码" />
				<Control width = "10"/>
				<CheckBox />
				<Label  text="自动登录" />
			</HBox>
			<Button text ="登录" name = "login" height = "30" width = "220" bkcolor="blue" normaltextcolor="white"/>
			<Control height = "10"/>
			<Label  text="注册账号" normaltextcolor="blue"/>
			<Control />
		 </VBox> 
		 
		 <Control width="50"/>
	</HBox>
  </VBox>
  
</VBox>
  
</Window>

通过实现GetSkinFile接口告诉render去渲染basic.xml页面:

std::wstring BasicForm::GetSkinFile()
{
	return L"basic.xml";
}

在之前xml中定义的登录button名字为login,代码中通过名字找到对象,然后绑定按钮事件:

void BasicForm::InitWindow()
{
	this->SetShadowAttached(true);
	/* Show settings menu */
	ui::Button* msg = dynamic_cast<ui::Button*>(FindControl(L"login"));
	msg->AttachClick([this](ui::EventArgs* args) {
		ui::RichEdit* edit_name = dynamic_cast<ui::RichEdit*>(FindControl(L"name"));
		ui::RichEdit* edit_password = dynamic_cast<ui::RichEdit*>(FindControl(L"password"));
		auto str_name = edit_name->GetText();
		auto str_pass = edit_password->GetText();
		if (str_name == str_pass)
		{
			MessageBoxA(0, "ok", "", 0);
		}
		return true;
		});
}

最后实现的效果:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.duilib简介 duilib是一个开源的DirectUI界面库,简洁但是功能强大。而且还是BSD的license,所以即便是在商业上,大家也可以安心使用。 现在大家可以从这个网站获取到他们所有的源码:/p/duilib/ 为了让我们能更简单的了解其机制,我们按照如下顺序一步一步的来对他进行观察: 工具库:用于支撑整个项目的基础 控件库:这是dui最关键的部分之一,相信也是大家最关注的部分之一,另外这里也来看看它是如何管理这些控件的 消息流转:有了控件库,我们需要将Windows窗口的原生消息流转给这些控件,另外在这里也来看看Focus,Capture等等的实现 资源组织和皮肤加载:有了上面所有的这些,我们再来看看它是如何自动创建皮肤的 简单使用:最后,来看看到底要如何使用它 以下是duilib工程带的一副总体设计图,在看代码之前看看这幅图,对看代码会很有帮助。 duilib: 2.工具库 由于duilib没有对外部的任何库进行依赖,所以在其内部实现了很多用于支撑项目的基础类,这些类分布在Util文件夹中: UI相关:CPoint/CSize/CDuiRect 简单容器:CStdPtrArray/CStdValArray/CStdString/CStdStringPtrMap 上面这些类看名字就基本能够理解其具体的含义了,当然除了基本的基础库,还有一些和窗口使用相关的工具的封装: 窗口工具:WindowImplBase,这个工具我们在这里不详述,后面会再次提到。 3.控件库 控件库在duilib的实现中被分为了两块:Core和Control: Core中包含的是所有控件公用的部分,里面主要是一些基类和绘制的封装。 Control中包含的就是各个不同的控件的行为了。 Core部分和控件相关的类图非常简单: duilib-core: 3.1.控件基类:CControlUI CControlUI在整个控件体系中非常重要,它是所有控件的基类,也是组成控件树的基本元素,控件树中所有的节点都是一个CControlUI。 他基本包括了所有控件公共的属性,如:位置,大小,颜色,是否有焦点,是否被启用,等等等等。当然这个类中还提供了非常多的基础函数,用于重载来实现子控件,如获取控件名称和ClassName,是否显示,等等等等。 另外为了方便从XML中直接解析出控件的各个属性,这个类中还在提供了一个SetAttribute的方法,传入字符串的属性名称和值对特定的属性进行设置,内部其实就是挨个比较字符串去完成的,所以平时使用的时候就还是不要使用的...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值