代码地址: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="30" width="100" normalimage="file='common/button_normal.bmp'" hotimage="file='common/button_over.bmp'" pushedimage="file='common/button_down.bmp'" font="0"" />
<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="30" width="100" normalimage="file='common/button_normal.bmp'" hotimage="file='common/button_over.bmp'" pushedimage="file='common/button_down.bmp'" font="0"" />
<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)
{
}
运行结果