用duilib实现一个按住就变化的按钮
时间: 2024-02-05 12:14:17 浏览: 137
在Duilib中,我们可以使用按钮控件(CButtonUI)来实现一个按住就变化的按钮。具体实现步骤如下:
1. 创建一个CButtonUI控件,并设置它的普通状态和按下状态的背景图片。
```cpp
<Control name="btnChange" class="ButtonUI" pos="100, 100, 100, 40" normalimage="btn_normal.png" hotimage="btn_hot.png" pushedimage="btn_pushed.png" />
```
2. 在按钮控件的事件处理函数中,监听鼠标按下和松开事件。
```cpp
class CMyWnd : public CWindowWnd, public INotifyUI
{
public:
virtual void Notify(TNotifyUI& msg)
{
if (msg.sType == _T("click"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
// do something
}
}
else if (msg.sType == _T("mouseenter"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
msg.pSender->SetBkImage(_T("btn_hot.png"));
}
}
else if (msg.sType == _T("mouseleave"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
msg.pSender->SetBkImage(_T("btn_normal.png"));
}
}
}
};
```
3. 在鼠标进入按钮控件和离开按钮控件时,更改按钮控件的背景图片。
```cpp
else if (msg.sType == _T("mouseenter"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
msg.pSender->SetBkImage(_T("btn_hot.png"));
}
}
else if (msg.sType == _T("mouseleave"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
msg.pSender->SetBkImage(_T("btn_normal.png"));
}
}
```
完整代码示例:
```cpp
#include <Windows.h>
#include <stdio.h>
#include "UIlib.h"
using namespace DuiLib;
class CMyWnd : public CWindowWnd, public INotifyUI
{
public:
virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }
virtual CDuiString GetSkinFile() { return _T("test.xml"); }
virtual CDuiString GetSkinFolder() { return _T(""); }
virtual void Notify(TNotifyUI& msg)
{
if (msg.sType == _T("click"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
// do something
}
}
else if (msg.sType == _T("mouseenter"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
msg.pSender->SetBkImage(_T("btn_hot.png"));
}
}
else if (msg.sType == _T("mouseleave"))
{
if (msg.pSender->GetName() == _T("btnChange"))
{
msg.pSender->SetBkImage(_T("btn_normal.png"));
}
}
}
virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lRes = 0;
BOOL bHandled = TRUE;
if (uMsg == WM_CREATE)
{
m_PaintManager.Init(m_hWnd);
CDialogBuilder builder;
CControlUI* pRoot = builder.Create(_T("test.xml"), (UINT)0, NULL, &m_PaintManager);
ASSERT(pRoot && "Failed to parse XML");
m_PaintManager.AttachDialog(pRoot);
m_PaintManager.AddNotifier(this);
return lRes;
}
else if (uMsg == WM_NCACTIVATE)
{
if (!::IsIconic(*this)) {
return (wParam == 0) ? TRUE : FALSE;
}
}
if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes))
bHandled = TRUE;
if (!bHandled)
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
return lRes;
}
protected:
CPaintManagerUI m_PaintManager;
};
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
HRESULT Hr = ::CoInitialize(NULL);
if (FAILED(Hr)) return 0;
CMyWnd* pFrame = new CMyWnd();
if (pFrame == NULL) return 0;
pFrame->Create(NULL, _T("Duilib Test"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
pFrame->CenterWindow();
pFrame->ShowWindow(true);
CPaintManagerUI::MessageLoop();
::CoUninitialize();
return 0;
}
```
阅读全文