C/C++ Duilib自定义控件

一、根据当前的需要来继承相应的控件,已便减少重复开发

一般继承 DoPaint来写,若需要在最上层显示的,则要继承DoPostPaint()

例读取图片旋转显示,配合GDI+来,注意Gdiplus::Bitmap的构造函数,本例PNG,使用PixelFormat32bppPARGB

bool RotateAnimation::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
{
		Gdiplus::PointF centerPos(m_rcItem.left + GetWidth()/ 2, m_rcItem.top + GetHeight()/ 2);

		Gdiplus::Graphics graphics(hDC);
		//graphics.SetSmoothingMode(SmoothingModeAntiAlias);

		graphics.TranslateTransform(centerPos.X, centerPos.Y);
		graphics.RotateTransform(m_fCurAngle);
		graphics.TranslateTransform(-centerPos.X, -centerPos.Y);

		const TImageInfo* imgInfo = GetManager()->GetImageEx(m_diBk.sImageName);
		BITMAP bmp;
		GetObject(imgInfo->hBitmap, sizeof(BITMAP), &bmp);		
		Gdiplus::Bitmap gdi_bmp(imgInfo->nX, imgInfo->nY, imgInfo->nX * 4, PixelFormat32bppPARGB, (BYTE*)bmp.bmBits);
	
		graphics.DrawImage(&gdi_bmp, m_rcItem.left, m_rcItem.top, GetWidth(), GetHeight());
		return true;
	}	
}

再细看一下DoPostPaint,首先要加入m_aPostPaintControls这个数组,在所有控件都被paint完成后,最后调用之,所以,它比DoPaint更在最上层

查看Duilib的源码的时候,都可以看到
在这里插入图片描述
powered by:小乌龟在大乌龟背上
更多文章:https://blog.csdn.net/what951006

Duilib是一个基于C++的开源UI库,它提供了丰富的控件和功能,可以用于快速开发Windows桌面应用程序。在Duilib中,你可以自定义控件来满足特定的需求。 下面是一个示例,演示如何在Duilib中创建一个自定义控件: ```cpp // 自定义控件的头文件 CustomControl.h #pragma once #include "UIlib.h" class CCustomControl : public DuiLib::CControlUI { public: CCustomControl(); virtual ~CCustomControl(); LPCTSTR GetClass() const; LPVOID GetInterface(LPCTSTR pstrName); void DoEvent(DuiLib::TEventUI& event); void PaintStatusImage(HDC hDC); protected: bool m_bMouseHover; bool m_bMousePressed; }; // 自定义控件的实现文件 CustomControl.cpp #include "CustomControl.h" CCustomControl::CCustomControl() : m_bMouseHover(false) , m_bMousePressed(false) { } CCustomControl::~CCustomControl() { } LPCTSTR CCustomControl::GetClass() const { return _T("CustomControl"); } LPVOID CCustomControl::GetInterface(LPCTSTR pstrName) { if (_tcscmp(pstrName, _T("CustomControl")) == 0) return static_cast<CCustomControl*>(this); return CControlUI::GetInterface(pstrName); } void CCustomControl::DoEvent(DuiLib::TEventUI& event) { if (event.Type == DuiLib::UIEVENT_MOUSEENTER) { m_bMouseHover = true; Invalidate(); } else if (event.Type == DuiLib::UIEVENT_MOUSELEAVE) { m_bMouseHover = false; Invalidate(); } else if (event.Type == DuiLib::UIEVENT_BUTTONDOWN) { m_bMousePressed = true; Invalidate(); } else if (event.Type == DuiLib::UIEVENT_BUTTONUP) { m_bMousePressed = false; Invalidate(); } CControlUI::DoEvent(event); } void CCustomControl::PaintStatusImage(HDC hDC) { if (m_bMousePressed) { // 绘制按下状态的控件外观 } else if (m_bMouseHover) { // 绘制鼠标悬停状态的控件外观 } else { // 绘制正常状态的控件外观 } } ``` 在上面的示例中,我们创建了一个名为`CCustomControl`的自定义控件类,继承自`DuiLib::CControlUI`。在这个类中,我们重写了一些方法来处理控件的事件和绘制外观。你可以根据自己的需求来实现这些方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值