#include "ProgressBar.h" namespace ezui { ProgressBar::ProgressBar(Object* parentObject) : Control(parentObject) { // 设置默认大小 SetFixedSize(Size(200, 25)); } ProgressBar::~ProgressBar() { } void ProgressBar::SetProgress(float progress) { // 限制进度值在 0.0f - 1.0f 范围内 float newProgress = (((0.0f) > ((((1.0f) < (progress)) ? (1.0f) : (progress)))) ? (0.0f) : ((((1.0f) < (progress)) ? (1.0f) : (progress)))); if (m_progress != newProgress) { m_progress = newProgress; Invalidate(); // 触发重绘 } } float ProgressBar::GetProgress() const { return m_progress; } void ProgressBar::SetProgressColor(const Color& color) { if (m_progressColor.GetValue() != color.GetValue()) { m_progressColor = color; Invalidate(); // 触发重绘 } } Color ProgressBar::GetProgressColor() const { return m_progressColor; } void ProgressBar::SetBackgroundColor(const Color& color) { if (m_backgroundColor.GetValue() != color.GetValue()) { m_backgroundColor = color; Invalidate(); // 触发重绘 } } Color ProgressBar::GetBackgroundColor() const { return m_backgroundColor; } void ProgressBar::SetAttribute(const UIString& attrName, const UIString& attrValue) { if (attrName == "progress") { // 设置进度值,支持百分比和小数 float progress = 0.0f; if (attrValue.find("%") != UIString::npos) { // 百分比格式,如 "50%" progress = std::atof(attrValue.substr(0, attrValue.length() - 1).c_str()) / 100.0f; } else { // 小数格式,如 "0.5" progress = std::atof(attrValue.c_str()); } SetProgress(progress); } else if (attrName == "progress-color") { // 设置进度条颜色 bool isGood = false; Color color = Color::Make(attrValue, &isGood); if (isGood) { SetProgressColor(color); } } else { // 调用基类方法处理其他属性 Control::SetAttribute(attrName, attrValue); } } bool ProgressBar::ApplyStyleProperty(const UIString& key, const UIString& value) { if (key == "progress") { // CSS 样式中的进度设置 SetAttribute("progress", value); return true; } else if (key == "progress-color") { // CSS 样式中的进度条颜色设置 SetAttribute("progress-color", value); return true; } else if (key == "background-color") { // 背景颜色 bool isGood = false; Color color = Color::Make(value, &isGood); if (isGood) { SetBackgroundColor(color); return true; } } // 调用基类方法处理其他样式 return Control::ApplyStyleProperty(key, value); } void ProgressBar::OnPaint(PaintEventArgs& args) { // 获取控件矩形 RectF rect(0, 0, (float)Width(), (float)Height()); // 绘制背景 args.Graphics.SetColor(m_backgroundColor); args.Graphics.FillRectangle(rect); // 绘制进度 if (m_progress > 0.0f) { args.Graphics.SetColor(m_progressColor); float progressWidth = rect.Width * m_progress; RectF progressRect(0, 0, progressWidth, rect.Height); args.Graphics.FillRectangle(progressRect); } // 绘制边框(如果有设置) Border border = GetStyle(State).Border; if (border.Style != StrokeStyle::None && (border.Left > 0 || border.Top > 0 || border.Right > 0 || border.Bottom > 0)) { OnBorderPaint(args, border); } } }