duilib开发(九):定时器和超链接

代码地址:https://github.com/yangpan4485/duilib/tree/develop/MyDemo

一、定时器

1、在界面上新建一个 Button 和 一个 Container,Button 用来启动定时器,Container 用来存放背景图片

<?xml version="1.0" encoding="utf-8"?>
<Window size="800,600" caption="0,0,300,20">
  <Font id="0" shared="true" name="宋体" size="18" bold="false" underline="false" italic="false" />
  <Font id="1" shared="true" name="宋体" size="20" bold="false" underline="false" italic="false" />
  <Font id="2" shared="true" name="宋体" size="16" bold="false" underline="false" italic="false" />
  <Default shared="true" name="Button" value=" height=&quot;30&quot; width=&quot;100&quot; normalimage=&quot;file=&apos;common/button_normal.bmp&apos;&quot; hotimage=&quot;file=&apos;common/button_over.bmp&apos;&quot; pushedimage=&quot;file=&apos;common/button_down.bmp&apos;&quot; font=&quot;0&quot;" />
	<VerticalLayout bkcolor="#FFDFFDF0" >
		<Container name="timerContainer" pos="100,100,192,192" float="true" bkimage="file='timer/head_default.png'" bkcolor="#FFFFFFFF"/>
		<Button name="timerBtn" text="定时器" float="true" pos="106,210,186,240" height="30" />
	</VerticalLayout>
</Window>

当点击 Button 时启动这个定时器,定时器的间隔为 500ms

void TimerControl::OnClick(DuiLib::TNotifyUI& msg)
{
	if (msg.pSender->GetName() == _T("timerBtn"))
	{
		HWND hwnd = _paintManager.GetPaintWindow();
		SetTimer(hwnd, 1, 500, NULL);
	}
}

还需要接收WM_TIMER消息,然后做一些处理,这里每当定时器来的时候,更换一张背景图片,这样就实现了一个简单的动画效果了

LRESULT TimerControl::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static int count = 1;
    std::cout << "count:" << count << std::endl;
    std::string image = "timer/head_" + std::to_string(count);
    image = image + ".png";
	auto timerContainer = (DuiLib::CContainerUI*)_paintManager.FindControl(_T("timerContainer"));
    timerContainer->SetBkImage(image.c_str());
    count++;
    if (count > 26) {
        count = 1;
    }
	return 0;
}

二、超链接

1、超链接也就是显示一个 Text 的文本

<Text text=" {a https://www.csdn.net} 访问 CSDN {/a}"  pos="80,30,400,60" float="true" showhtml="true" font="0"/>

2、需要在代码中对超链接做出响应

if (msg.sType == _T("link"))
{
    DuiLib::CTextUI* pText = (DuiLib::CTextUI*)msg.pSender;
    DuiLib::CDuiString* str = pText->GetLinkContent(0);
    // 打开CSDN
    ShellExecute(NULL, "open", str->GetData(), NULL, NULL, SW_SHOWNORMAL);
}

三、全部代码

xml

<?xml version="1.0" encoding="utf-8"?>
<Window size="800,600" caption="0,0,300,20">
  <Font id="0" shared="true" name="宋体" size="18" bold="false" underline="false" italic="false" />
  <Font id="1" shared="true" name="宋体" size="20" bold="false" underline="false" italic="false" />
  <Font id="2" shared="true" name="宋体" size="16" bold="false" underline="false" italic="false" />
  <Default shared="true" name="Button" value=" height=&quot;30&quot; width=&quot;100&quot; normalimage=&quot;file=&apos;common/button_normal.bmp&apos;&quot; hotimage=&quot;file=&apos;common/button_over.bmp&apos;&quot; pushedimage=&quot;file=&apos;common/button_down.bmp&apos;&quot; font=&quot;0&quot;" />
	<VerticalLayout bkcolor="#FFDFFDF0" >
		<Container name="timerContainer" pos="100,100,192,192" float="true" bkimage="file='timer/head_default.png'" bkcolor="#FFFFFFFF"/>
		<Button name="timerBtn" text="定时器" float="true" pos="106,210,186,240" height="30" />

		<Text text=" {a https://www.csdn.net} 访问 CSDN {/a}"  pos="80,30,400,60" float="true" showhtml="true" font="0"/>
	</VerticalLayout>
</Window>

timer.h

#pragma once

#include "UIlib.h"

class TimerControl : public DuiLib::CWindowWnd, public DuiLib::INotifyUI
{
public:
	TimerControl();
	~TimerControl();

	void Init();
	bool CreateDUIWindow();
	void ShowWindow();

	LPCTSTR GetWindowClassName() const override;
	void Notify(DuiLib::TNotifyUI& msg) override;
	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override;

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam);
	LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam);

	void OnClick(DuiLib::TNotifyUI& msg);
	void OnValueChange(DuiLib::TNotifyUI& msg);
	LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam);

private:
	HINSTANCE _hInstance;
	DuiLib::CPaintManagerUI _paintManager{};
	HWND _ownerWnd{};
};

timer.cpp

#include "timer.h"

#include <iostream>
#include <string>

TimerControl::TimerControl()
{

}
TimerControl::~TimerControl()
{

}

void TimerControl::Init()
{
	SetProcessDPIAware();
	_hInstance = GetModuleHandle(0);
	DuiLib::CPaintManagerUI::SetInstance(_hInstance);
	DuiLib::CPaintManagerUI::SetResourcePath(DuiLib::CPaintManagerUI::GetInstancePath() + +_T("resources"));
}
bool TimerControl::CreateDUIWindow()
{
	_ownerWnd = Create(NULL, _T("Timer"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	if (!_ownerWnd)
	{
		std::cout << "create dui window failed" << std::endl;
		return false;
	}
	return true;
}
void TimerControl::ShowWindow()
{
	ShowModal();
}

LPCTSTR TimerControl::GetWindowClassName() const
{
	return _T("DUITimerFrame");
}
void TimerControl::Notify(DuiLib::TNotifyUI& msg)
{
	if (msg.sType == _T("click"))
	{
		OnClick(msg);
	}
	else if (msg.sType == _T("valuechanged"))
	{
		OnValueChange(msg);
	}
    if (msg.sType == _T("link"))
    {
        DuiLib::CTextUI* pText = (DuiLib::CTextUI*)msg.pSender;
        DuiLib::CDuiString* str = pText->GetLinkContent(0);
        // CSDN
        ShellExecute(NULL, "open", str->GetData(), NULL, NULL, SW_SHOWNORMAL);

    }
}
LRESULT TimerControl::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	switch (uMsg) {
	case WM_CREATE:
		lRes = OnCreate(uMsg, wParam, lParam);
		break;
	case WM_CLOSE:
		lRes = OnClose(uMsg, wParam, lParam);
		break;
	case WM_TIMER: 
		lRes = OnTimer(uMsg, wParam, lParam); 
		break;
	default:
		break;
	}
	if (_paintManager.MessageHandler(uMsg, wParam, lParam, lRes))
	{
		return lRes;
	}

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

LRESULT TimerControl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	_paintManager.Init(m_hWnd);
	DuiLib::CDialogBuilder builder;
	DuiLib::CControlUI* pRoot = builder.Create(_T("timer.xml"), (UINT)0, NULL, &_paintManager);
	_paintManager.AttachDialog(pRoot);
	_paintManager.AddNotifier(this);

	return 0;
}
LRESULT TimerControl::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return 0;
}

void TimerControl::OnClick(DuiLib::TNotifyUI& msg)
{
	if (msg.pSender->GetName() == _T("timerBtn"))
	{
		HWND hwnd = _paintManager.GetPaintWindow();
		SetTimer(hwnd, 1, 500, NULL);
	}
}
LRESULT TimerControl::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static int count = 1;
    std::cout << "count:" << count << std::endl;
    std::string image = "timer/head_" + std::to_string(count);
    image = image + ".png";
	auto timerContainer = (DuiLib::CContainerUI*)_paintManager.FindControl(_T("timerContainer"));
    timerContainer->SetBkImage(image.c_str());
    count++;
    if (count > 26) {
        count = 1;
    }
	return 0;
}
void TimerControl::OnValueChange(DuiLib::TNotifyUI& msg)
{

} 

运行结果

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值