增加对圆环形滚动条的支持

This commit is contained in:
xuhuajie-NetEase 2019-08-14 16:03:11 +08:00
parent f46eca3a61
commit 0e7f1b6462
9 changed files with 1071 additions and 759 deletions

View File

@ -57,7 +57,13 @@
<Slider class="slider_green" value="70" margin="10"/>
</HBox>
</VBox>
<VBox width="120">
<CircleProgress name="circleprogress" circular="true" height="80" width="80"
circlewidth="10" bgcolor="gray" fgcolor="green" clockwise="true" min="1" max="100" value="75" margin="10"
textpadding="10,32,10,10" normaltextcolor="darkcolor" indicator="logo_18x18.png"/>
</VBox>
</HBox>
<Control class="splitline_hor_level1"/>
<!-- RichEdit -->
<RichEdit class="prompt" name="edit" bkcolor="bk_wnd_lightcolor" width="stretch" height="stretch"

View File

@ -154,4 +154,9 @@ void ControlForm::OnProgressValueChagned(float value)
{
auto progress = dynamic_cast<ui::Progress*>(FindControl(L"progress"));
progress->SetValue(value);
auto circleprogress = dynamic_cast<ui::Progress*>(FindControl(L"circleprogress"));
circleprogress->SetValue(value);
TCHAR szBuffer[32] = {0};
swprintf_s(szBuffer, _T("%.0f%%"), value);
circleprogress->SetText(szBuffer);
}

View File

@ -0,0 +1,206 @@
#include "stdafx.h"
#include "CircleProgress.h"
#include "shlwapi.h"
namespace ui
{
CircleProgress::CircleProgress() :
m_bCircular(true),
m_bClockwise(true),
m_nCircleWidth(1),
m_dwBackgroundColor(0),
m_dwForegroundColor(0),
m_dwGradientColor(0),
m_pIndicator(nullptr)
{
}
void CircleProgress::SetAttribute(const std::wstring& srName, const std::wstring& strValue)
{
if (srName == _T("circular")) SetCircular(strValue == _T("true"));
else if (srName == _T("circlewidth")) SetCircleWidth(_ttoi(strValue.c_str()));
else if (srName == _T("indicator")) SetIndicator(strValue);
else if (srName == _T("clockwise")) SetClockwiseRotation(strValue == _T("true"));
else if (srName == _T("bgcolor")) {
LPCTSTR pValue = strValue.c_str();
while (*pValue > _T('\0') && *pValue <= _T(' ')) pValue = ::CharNext(pValue);
SetBackgroudColor(pValue);
}
else if (srName == _T("fgcolor")) {
LPCTSTR pValue = strValue.c_str();
while (*pValue > _T('\0') && *pValue <= _T(' ')) pValue = ::CharNext(pValue);
SetForegroudColor(pValue);
}
else if (srName == _T("gradientcolor")) {
LPCTSTR pValue = strValue.c_str();
while (*pValue > _T('\0') && *pValue <= _T(' ')) pValue = ::CharNext(pValue);
SetCircleGradientColor(pValue);
}
else Progress::SetAttribute(srName, strValue);
}
void CircleProgress::PaintStatusImage(IRenderContext* pRender)
{
Progress::PaintStatusImage(pRender);
if (m_bCircular)
{
//目前IRenderContext还有很多GDI+接口未实现暂时直接用gdi+画图了
//以后可能会调整需实现1、DrawArc 2、Pen增加brush(渐变)入参 3、可以自由设置Graphics属性
int direction = m_bClockwise ? 1 : -1; //旋转方向
int bordersize = 1; //弧度宽度目前使用1像素
Gdiplus::Graphics graphics(pRender->GetDC());
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
Gdiplus::Pen bgPen(m_dwBackgroundColor, m_nCircleWidth);
// 圆形中心
CPoint center;
center.x = m_rcItem.left + (m_rcItem.right - m_rcItem.left) / 2;
center.y = m_rcItem.top + (m_rcItem.bottom - m_rcItem.top) / 2;
// 控件矩形内的最大正方形的边界
int side = min(m_rcItem.right - m_rcItem.left, m_rcItem.bottom - m_rcItem.top);
//UiRect rcBorder; 仍然存在UiRect 到 RectF的转换所以直接用gdi的RectF了
Gdiplus::RectF rcBorder;
rcBorder.X = center.x - side / 2;
rcBorder.Y = center.y - side / 2;
rcBorder.Width = rcBorder.Height = side;
Gdiplus::RectF outer = rcBorder;
if (m_pIndicator) {
outer.Inflate(-1.0F *m_pIndicator->GetWidth() / 2, -1.0F * m_pIndicator->GetWidth() / 2);
}
else
{
outer.Inflate(-0.5 * m_nCircleWidth, -0.5 * m_nCircleWidth);
}
outer.Inflate(-1, -1);
if (m_dwGradientColor == 0)
{
//不使用渐变色,直接用前景色铺满
Gdiplus::Pen fgPen(m_dwForegroundColor, m_nCircleWidth);
graphics.DrawArc(&bgPen, outer, 270, 360); //270从最上面开始递增设为0的话是最右边开始
graphics.DrawArc(&fgPen, outer, 270, direction * 360 * (m_nValue - m_nMin) / (m_nMax - m_nMin));
}
else
{
Gdiplus::REAL factors[4] = { 0.0f, 0.4f, 0.6f, 1.0f };
Gdiplus::REAL positions[4] = { 0.0f, 0.2f, 0.8f, 1.0f };
Gdiplus::LinearGradientBrush lgbrush(rcBorder, m_dwForegroundColor, m_dwGradientColor, Gdiplus::LinearGradientModeVertical);
lgbrush.SetBlend(factors, positions, 4);
graphics.DrawArc(&bgPen, outer, 270, 360);
Gdiplus::Pen fgPen(&lgbrush, m_nCircleWidth);
graphics.DrawArc(&fgPen, outer, 270, direction * 360 * (m_nValue - m_nMin) / (m_nMax - m_nMin));
}
//画旋转指示器图标,需要用到矩阵
if (m_pIndicator)
{
Gdiplus::Matrix matrix;
matrix.RotateAt(direction * 360 * (m_nValue - m_nMin) / (m_nMax - m_nMin), Gdiplus::PointF(center.x, center.y), Gdiplus::MatrixOrderAppend);
graphics.SetTransform(&matrix);
Gdiplus::RectF rectf;
rectf.X = center.x - m_pIndicator->GetWidth() / 2;
rectf.Y = outer.Y + bordersize / 2 -m_pIndicator->GetHeight() / 2;
rectf.Width = m_pIndicator->GetWidth();
rectf.Height = m_pIndicator->GetHeight();
graphics.DrawImage(m_pIndicator, rectf);
}
}
}
void CircleProgress::ClearImageCache()
{
__super::ClearImageCache();
if (m_pIndicator)
{
delete m_pIndicator;
m_pIndicator = nullptr;
}
}
void CircleProgress::SetCircular(bool bCircular /*= true*/)
{
m_bCircular = bCircular;
Invalidate();
}
void CircleProgress::SetClockwiseRotation(bool bClockwise /*= true*/)
{
if (bClockwise != m_bClockwise)
{
m_bClockwise = bClockwise;
if (m_pIndicator)
{
//已经旋转了图片,旋转到相反的方向
m_pIndicator->RotateFlip(Gdiplus::Rotate180FlipNone);
}
}
}
void CircleProgress::SetCircleWidth(int nCircleWidth)
{
m_nCircleWidth = nCircleWidth;
Invalidate();
}
void CircleProgress::SetBackgroudColor(const std::wstring& strColor)
{
m_dwBackgroundColor = GlobalManager::GetTextColor(strColor);
ASSERT(m_dwBackgroundColor != 0);
Invalidate();
}
void CircleProgress::SetForegroudColor(const std::wstring& strColor)
{
m_dwForegroundColor = GlobalManager::GetTextColor(strColor);
ASSERT(m_dwForegroundColor != 0);
Invalidate();
}
void CircleProgress::SetIndicator(const std::wstring& sIndicatorImage)
{
if (m_sIndicatorImage != sIndicatorImage)
{
m_sIndicatorImage = sIndicatorImage;
if (m_pIndicator)
{
delete m_pIndicator;
m_pIndicator = nullptr;
}
std::wstring imagepath = m_sIndicatorImage;
if (!::PathFileExistsW(imagepath.c_str())) {
imagepath = GlobalManager::GetResourcePath() + m_pWindow->GetWindowResourcePath() + imagepath;
}
if (!::PathFileExistsW(imagepath.c_str())) {
return;
}
m_pIndicator = new Gdiplus::Image(imagepath.c_str());
Gdiplus::Status state = m_pIndicator->GetLastStatus();
if (Gdiplus::Ok == state)
{
// 假定图片指向上
m_pIndicator->RotateFlip(m_bClockwise ? Gdiplus::Rotate90FlipNone : Gdiplus::Rotate270FlipNone);
Invalidate();
}
}
}
void CircleProgress::SetCircleGradientColor(const std::wstring& strColor)
{
m_dwGradientColor = GlobalManager::GetTextColor(strColor);
ASSERT(m_dwGradientColor != 0);
Invalidate();
}
}

View File

@ -0,0 +1,84 @@
/** @file CircleProgress.h
* @brief 85%
* @copyright (c) 2019-2022, NetEase Inc. All rights reserved
* @author Xuhuajie
* @date 2019/8/14
*/
#ifndef UI_CONTROL_CIRCLEPROGRESS_H_
#define UI_CONTROL_CIRCLEPROGRESS_H_
#pragma once
namespace ui
{
class UILIB_API CircleProgress : public Progress
{
public:
CircleProgress();
/// 重写父类方法,提供个性化功能,请参考父类声明
virtual void SetAttribute(const std::wstring& strName, const std::wstring& strValue) override;
virtual void PaintStatusImage(IRenderContext* pRender) override;
virtual void ClearImageCache() override;
/**
* @brief
* @param[in] bCircular true false true
* @return
*/
void SetCircular(bool bCircular = true);
/**
* @brief
* @param[in] bClockwise true false true
* @return
*/
void SetClockwiseRotation(bool bClockwise = true);
/**
* @brief
* @param[in] nCircleWidth
* @return
*/
void SetCircleWidth(int nCircleWidth);
/**
* @brief
* @param[in] strColor要设置的背景颜色字符串 global.xml
* @return
*/
void SetBackgroudColor(const std::wstring& strColor);
/**
* @brief
* @param[in] strColor要设置的前景颜色字符串 global.xml
* @return
*/
void SetForegroudColor(const std::wstring& strColor);
/**
* @brief ForegroudColor同时使用,
* @param[in] strColor要设置的前景渐变颜色字符串 global.xml
* @return
*/
void SetCircleGradientColor(const std::wstring& strColor);
/**
* @brief
* @param[in] sIndicatorImage要设置的图片
* @return
*/
void SetIndicator(const std::wstring& sIndicatorImage);
protected:
bool m_bCircular;
bool m_bClockwise;
int m_nCircleWidth;
DWORD m_dwBackgroundColor;
DWORD m_dwForegroundColor;
DWORD m_dwGradientColor;
//Image m_IndicatorImage; //使用image对象无法满足需求需要设置矩阵变换
Gdiplus::Image* m_pIndicator; //此类目前维护资源管理
std::wstring m_sIndicatorImage;
};
} // namespace ui
#endif // UI_CONTROL_CIRCLEPROGRESS_H_

View File

@ -239,6 +239,7 @@ EventType StringToEnum(const std::wstring& messageType);
#define DUI_CTR_COMBO (_T("Combo"))
#define DUI_CTR_SLIDER (_T("Slider"))
#define DUI_CTR_PROGRESS (_T("Progress"))
#define DUI_CTR_CIRCLEPROGRESS (_T("CircleProgress"))
#define DUI_CTR_SCROLLBAR (_T("ScrollBar"))
}// namespace ui

View File

@ -493,6 +493,7 @@ Control* WindowBuilder::CreateControlByClass(const std::wstring& strControlClass
break;
case 14:
if (strControlClass == DUI_CTR_VIRTUALLISTBOX) pControl = new VirtualListBox;
else if (strControlClass == DUI_CTR_CIRCLEPROGRESS) pControl = new CircleProgress;
break;
case 15:
break;

View File

@ -127,6 +127,7 @@
#include "Control/Option.h"
#include "Control/Progress.h"
#include "Control/CircleProgress.h"
#include "Control/Slider.h"
#include "Control/RichEdit.h"

View File

@ -317,6 +317,7 @@
<ClCompile Include="Box\TabBox.cpp" />
<ClCompile Include="Box\TileBox.cpp" />
<ClCompile Include="Box\VBox.cpp" />
<ClCompile Include="Control\CircleProgress.cpp" />
<ClCompile Include="Control\VirtualListBox.cpp" />
<ClCompile Include="Core\Define.cpp" />
<ClCompile Include="Core\Image.cpp" />
@ -368,6 +369,7 @@
<ClInclude Include="Box\TabBox.h" />
<ClInclude Include="Box\TileBox.h" />
<ClInclude Include="Box\VBox.h" />
<ClInclude Include="Control\CircleProgress.h" />
<ClInclude Include="Control\VirtualListBox.h" />
<ClInclude Include="Core\Image.h" />
<ClInclude Include="Core\Placeholder.h" />

View File

@ -179,6 +179,9 @@
<ClCompile Include="Utils\UnZip.cpp">
<Filter>Source Files\Utils</Filter>
</ClCompile>
<ClCompile Include="Control\CircleProgress.cpp">
<Filter>Source Files\Control</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="StdAfx.h">
@ -337,5 +340,8 @@
<ClInclude Include="Utils\UnZip.h">
<Filter>Header Files\Utils</Filter>
</ClInclude>
<ClInclude Include="Control\CircleProgress.h">
<Filter>Header Files\Control</Filter>
</ClInclude>
</ItemGroup>
</Project>