updatecolors

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数通过把客户区域的当前颜色重新映射到发前被映射过的逻辑调色板,来更新指定设备环境的客户区域。

    函数原型:BOOL UpdateColors(HDC hdc);

    参数:

    hdc:设备环境句柄。

    返回值:如果成功返回值非零,如果失败,返回值为零。

    Windows NT:若想获得更多错误信息,请调用GetLastError函数。

    注释:通过调用GetDeviceCaps函数和设置RASTERCAPS常量,一个应用可以确定一个设备是否支持调色板操作,当系统调色板发生变化时,一个映射逻辑调色板非活动窗口可以通过调用Updatecolors,作为一种选择方法刷新它的客户区域。

    UpdateColors函数更新一个客户区域比刷新该区域要快,但是,由于在系统调色板改变之前UpdateColors函数要进行基于每个像素颜色的色彩转换,因此,每一次调用该函数会导致一些颜色失真。

    一旦收到WM_PALETTECHANGED消息,该函数必须马上被调用。

    速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:不支持;头文件:wingdi.h;库文件:gdi32.lib。

#ifndef COLORFULUI_H #define COLORFULUI_H #include <QWidget> #include <QTimer> #include <QColor> #include <QLinearGradient> #include <QPropertyAnimation> QT_BEGIN_NAMESPACE namespace Ui { class ColorfulUI; } QT_END_NAMESPACE class ColorfulUI : public QWidget { Q_OBJECT public: ColorfulUI(QWidget *parent = nullptr); ~ColorfulUI(); private slots: // 定时器触发:更新窗口背景色和按钮颜色 void updateColors(); // 按钮点击:触发控件缩放动画 void on_btn_clicked(); private: Ui::ColorfulUI *ui; QTimer *colorTimer; // 控制色彩变化的定时器 int hue; // HSV色彩的色相值(0-359,循环变化实现渐变) QPropertyAnimation *btnAnim; // 按钮缩放动画 }; #endif // COLORFULUI_H#include "ColorfulUI.h" #include "ui_ColorfulUI.h" #include <QPushButton> #include <QFont> ColorfulUI::ColorfulUI(QWidget *parent) : QWidget(parent) , ui(new Ui::ColorfulUI) , hue(0) // 初始色相值(红色) { ui->setupUi(this); // 1. 窗口基础设置 setWindowTitle("炫彩动态界面"); resize(800, 600); // 窗口大小 // 2. 创建炫彩按钮(居中显示) QPushButton *btn = new QPushButton("点击触发动画", this); btn->setGeometry(300, 250, 200, 80); // 按钮位置和大小 QFont btnFont = btn->font(); btnFont.setPointSize(14); btn->setFont(btnFont); btn->setStyleSheet("QPushButton{border-radius:40px; background-color: #ff6b6b; color:white;}"); connect(btn, &QPushButton::clicked, this, &ColorfulUI::on_btn_clicked); // 3. 初始化按钮缩放动画(点击后放大再还原) btnAnim = new QPropertyAnimation(btn, "geometry", this); btnAnim->setDuration(500); // 动画时长(毫秒) btnAnim->setStartValue(QRect(300, 250, 200, 80)); // 初始大小 btnAnim->setEndValue(QRect(280, 230, 240, 120)); // 放大后大小 btnAnim->setEasingCurve(QEasingCurve::OutBounce); // 弹跳效果(更炫) // 4. 初始化色彩定时器(每50毫秒更新一次颜色,速度可调整) colorTimer = new QTimer(this); colorTimer->setInterval(50); connect(colorTimer, &QTimer::timeout, this, &ColorfulUI::updateColors); colorTimer->start(); // 启动定时器,开始动态变色 } ColorfulUI::~ColorfulUI() { delete ui; } // 核心:动态更新窗口背景和按钮颜色 void ColorfulUI::updateColors() { // 1. 窗口背景:线性渐变(随hue变化) QLinearGradient grad(0, 0, width(), height()); // 渐变方向:左上→右下 QColor color1 = QColor::fromHsv(hue, 200, 255); // 颜色1(饱和度200,亮度255) QColor color2 = QColor::fromHsv((hue + 120) % 360, 200, 255); // 颜色2(色相差120,形成对比) grad.setColorAt(0, color1); grad.setColorAt(1, color2); // 设置窗口背景为渐变 setStyleSheet(QString("background: %1;").arg(grad.toString())); // 2. 按钮颜色:随hue变化(色相+60,与背景区分) QPushButton *btn = findChild<QPushButton*>(); // 找到按钮 if (btn) { QColor btnColor = QColor::fromHsv((hue + 60) % 360, 200, 255); btn->setStyleSheet( QString("QPushButton{border-radius:40px; background-color: %1; color:white;}") .arg(btnColor.name()) ); } // 3. 更新色相值(循环0-359,实现“彩虹流动”) hue = (hue + 1) % 360; } // 按钮点击:触发缩放动画 void ColorfulUI::on_btn_clicked() { if (btnAnim->state() == QAbstractAnimation::Running) { btnAnim->stop(); // 若动画正在运行,先停止 } // 切换动画方向(放大→还原→放大...) if (btnAnim->direction() == QAbstractAnimation::Forward) { btnAnim->setDirection(QAbstractAnimation::Backward); } else { btnAnim->setDirection(QAbstractAnimation::Forward); } btnAnim->start(); // 启动动画 }详细讲述上述代码
11-25
/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * Licensed under the Oculus SDK License Agreement (the "License"); * you may not use the Oculus SDK except in compliance with the License, * which is provided at the time of installation or download, or which * otherwise accompanies this software in either electronic or hard copy form. * * You may obtain a copy of the License at * * https://developer.oculus.com/licenses/oculussdk/ * * Unless required by applicable law or agreed to in writing, the Oculus SDK * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Diagnostics; using UnityEngine; namespace Oculus.Interaction.Locomotion { /// <summary> /// This visuals component renders one curved arrow to the left, and another one to the right. /// Using the Visual parameters one can control the angle, spacing, curvature, thickness of the arrows. /// Using the controllers one can highlight one or the other arrow, make them grow or move along their trail. /// </summary> public class TurnArrowVisuals : MonoBehaviour { /// <summary> /// Renderer for the Left arrow cone /// </summary> [Header("Visual renderers")] [Tooltip("Renderer for the Left arrow cone")] [SerializeField] private Renderer _leftArrow; /// <summary> /// Renderer for the Right arrow cone /// </summary> [Tooltip("Renderer for the Right arrow cone")] [SerializeField] private Renderer _rightArrow; /// <summary> /// TubeRenderer that will draw the rail of the left arrow /// </summary> [Tooltip("TubeRenderer that will draw the rail of the left arrow")] [SerializeField] private TubeRenderer _leftRail; /// <summary> /// TubeRenderer that will draw the rail of the right arrow /// </summary> [Tooltip("TubeRenderer that will draw the rail of the right arrow")] [SerializeField] private TubeRenderer _rightRail; /// <summary> /// TubeRenderer that will draw the trail of the right arrow /// </summary> [Tooltip("TubeRenderer that will draw the trail of the right arrow")] [SerializeField] private TubeRenderer _leftTrail; /// <summary> /// TubeRenderer that will draw the trail of the right arrow /// </summary> [Tooltip("TubeRenderer that will draw the trail of the right arrow")] [SerializeField] private TubeRenderer _rightTrail; /// <summary> /// Material block for the left arrow items so they can be controller /// </summary> [Tooltip("Material block for the left arrow items so they can be controller")] [SerializeField] private MaterialPropertyBlockEditor _leftMaterialBlock; /// <summary> /// Material block for the right arrow items so they can be controller /// </summary> [Tooltip("Material block for the right arrow items so they can be controller")] [SerializeField] private MaterialPropertyBlockEditor _rightMaterialBlock; /// <summary> /// Radius of the circle in which the arrows are circunscribed /// </summary> [Header("Visual parameters")] [Tooltip("Radius of the circle in which the arrows are circunscribed")] [SerializeField] private float _radius = 0.07f; public float Radius => _radius; /// <summary> /// Gap, in degrees, left between the arrows /// </summary> [Tooltip("Gap, in degrees, left between the arrows")] [SerializeField] private float _margin = 2f; public float Margin => _margin; /// <summary> /// Length, in degrees, of the trail of the arrows /// </summary> [Tooltip("Length, in degrees, of the trail of the arrows")] [SerializeField] private float _trailLength = 15f; public float TrailLength => _trailLength; /// <summary> /// Max angle, in degrees, the arrows can follow when highlighted /// </summary> [Tooltip("Max angle, in degrees, the arrows can follow when highlighted")] [SerializeField] private float _maxAngle = 45f; public float MaxAngle => _maxAngle; /// <summary> /// Length of the transparent gap in the rail left by the arrow /// </summary> [Tooltip("Length of the transparent gap in the rail left by the arrow")] [SerializeField] private float _railGap = 0.005f; public float RailGap => _railGap; /// <summary> /// Length, in degrees, that the arrows can grow when highlighted /// </summary> [Tooltip("Length, in degrees, that the arrows can grow when highlighted")] [SerializeField] private float _squeezeLength = 5f; public float SqueezeLength => _squeezeLength; /// <summary> /// Color of the arrow when not active /// </summary> [Header("Visual controllers")] [Tooltip("Color of the arrow when not active")] [SerializeField] private Color _disabledColor = new Color(1f, 1f, 1f, 0.2f); public Color DisabledColor { get => _disabledColor; set => _disabledColor = value; } /// <summary> /// Color of the arrow when active /// </summary> [Tooltip("Color of the arrow when active")] [SerializeField] private Color _enabledColor = new Color(1f, 1f, 1f, 0.6f); public Color EnabledColor { get => _enabledColor; set => _enabledColor = value; } /// <summary> /// Color of the arrow when highlighted /// </summary> [Tooltip("Color of the arrow when highlighted")] [SerializeField] private Color _highligtedColor = new Color(1f, 1f, 1f, 1f); public Color HighligtedColor { get => _highligtedColor; set => _highligtedColor = value; } /// <summary> /// If true, the current active arrow will /// look highlighted /// </summary> [Tooltip("If true, the current active arrow will")] [SerializeField] private bool _highLight = false; public bool HighLight { get => _highLight; set => _highLight = value; } /// <summary> /// This value controls wich arrow is active, <0 for the left and >0 for the right /// </summary> [Tooltip("This value controls wich arrow is active, <0 for the left and >0 for the right")] [SerializeField] private float _value = 0f; public float Value { get => _value; set => _value = value; } /// <summary> /// Indicates how much the active arrow must grow /// </summary> [Tooltip("Indicates how much the active arrow must grow")] [SerializeField] private float _progress; public float Progress { get => _progress; set => _progress = value; } /// <summary> /// Indicates wheter the active arrow should follow the rail /// </summary> [Tooltip("Indicates wheter the active arrow should follow the rail")] [SerializeField] private bool _followArrow = false; public bool FollowArrow { get => _followArrow; set => _followArrow = value; } private const float _degreesPerSegment = 1f; private static readonly Quaternion _rotationCorrectionLeft = Quaternion.Euler(0f, -90f, 0f); private static readonly int _colorShaderPropertyID = Shader.PropertyToID("_Color"); protected bool _started; protected virtual void Start() { this.BeginStart(ref _started); this.AssertField(_leftTrail, nameof(_leftTrail)); this.AssertField(_rightTrail, nameof(_rightTrail)); this.AssertField(_leftArrow, nameof(_leftArrow)); this.AssertField(_rightArrow, nameof(_rightArrow)); this.AssertField(_leftRail, nameof(_leftRail)); this.AssertField(_rightRail, nameof(_rightRail)); this.AssertField(_leftMaterialBlock, nameof(_leftMaterialBlock)); this.AssertField(_rightMaterialBlock, nameof(_rightMaterialBlock)); InitializeVisuals(); DisableVisuals(); this.EndStart(ref _started); } protected virtual void OnDisable() { if (_started) { DisableVisuals(); } } /// <summary> /// Disables all the visual renderers /// </summary> public void DisableVisuals() { _leftTrail.enabled = false; _rightTrail.enabled = false; _leftArrow.enabled = false; _rightArrow.enabled = false; _leftRail.enabled = false; _rightRail.enabled = false; } private void InitializeVisuals() { TubePoint[] trailPoints = InitializeSegment(new Vector2(_margin, _maxAngle + _squeezeLength)); if (trailPoints == null || trailPoints.Length < 2) return; _leftTrail.RenderTube(trailPoints, Space.Self); _rightTrail.RenderTube(trailPoints, Space.Self); TubePoint[] railPoints = InitializeSegment(new Vector2(_margin, _maxAngle)); if (railPoints == null || railPoints.Length < 2) return; _leftRail.RenderTube(railPoints, Space.Self); _rightRail.RenderTube(railPoints, Space.Self); } /// <summary> /// Updates the Arrows and Colors with the provided Values. /// It also re-enables the renderers so they are instantly drawn. /// </summary> public void UpdateVisual() { UpdateArrows(Value); UpdateColors(HighLight, Value); } private void UpdateArrows(float value) { float angle = Mathf.Lerp(0f, _maxAngle, Mathf.Abs(value)); bool isLeft = value < 0; bool follow = _followArrow; float squeeze = Mathf.Lerp(0f, _squeezeLength, _progress); _leftTrail.enabled = true; _rightTrail.enabled = true; _leftArrow.enabled = true; _rightArrow.enabled = true; _rightRail.enabled = !isLeft; _leftRail.enabled = isLeft; angle = Mathf.Max(angle, _trailLength); UpdateArrowPosition(isLeft ? _trailLength : angle + squeeze, _rightArrow.transform); RotateTrail(follow && !isLeft ? angle - _trailLength : 0f, _rightTrail); UpdateTrail(isLeft ? _trailLength : (follow ? _trailLength : angle) + squeeze, _rightTrail); UpdateArrowPosition(!isLeft ? -_trailLength : -angle - squeeze, _leftArrow.transform); RotateTrail(follow && isLeft ? -angle + _trailLength : 0f, _leftTrail); UpdateTrail(!isLeft ? _trailLength : (follow ? _trailLength : angle) + squeeze, _leftTrail); UpdateRail(angle, squeeze, isLeft ? _leftRail : _rightRail); } private void UpdateArrowPosition(float angle, Transform arrow) { Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.up); arrow.localPosition = rotation * Vector3.forward * _radius; arrow.localRotation = rotation * _rotationCorrectionLeft; } private void RotateTrail(float angle, TubeRenderer trail) { trail.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.up); } private void UpdateTrail(float angle, TubeRenderer trail) { float max = _maxAngle + _squeezeLength; float segmentLenght = trail.TotalLength; float start = -100; float end = (max - angle - _margin) / max; trail.StartFadeThresold = segmentLenght * start; trail.EndFadeThresold = segmentLenght * end; trail.InvertThreshold = false; trail.RedrawFadeThresholds(); } private void UpdateRail(float angle, float extra, TubeRenderer rail) { float segmentLenght = rail.TotalLength; float start = (angle - _trailLength - _margin) / _maxAngle; float end = (_maxAngle - angle - extra - _margin) / _maxAngle; float gap = _railGap + rail.Feather; rail.StartFadeThresold = segmentLenght * start - gap; rail.EndFadeThresold = segmentLenght * end - gap; rail.InvertThreshold = true; rail.RedrawFadeThresholds(); } private void UpdateColors(bool isSelection, float value) { _leftMaterialBlock.MaterialPropertyBlock.SetColor(_colorShaderPropertyID, value < 0 ? (isSelection ? _highligtedColor : _enabledColor) : _disabledColor); _rightMaterialBlock.MaterialPropertyBlock.SetColor(_colorShaderPropertyID, value > 0 ? (isSelection ? _highligtedColor : _enabledColor) : _disabledColor); _leftMaterialBlock.UpdateMaterialPropertyBlock(); _rightMaterialBlock.UpdateMaterialPropertyBlock(); } private TubePoint[] InitializeSegment(Vector2 minMax) { const float kMinSegmentAngle = 0.01f; // 避免 0° float lowLimit = minMax.x; float upLimit = minMax.y; float sweep = Mathf.Repeat(upLimit - lowLimit, 360f); sweep = Mathf.Max(sweep, kMinSegmentAngle); // 保证至少 0.01° int segments = Mathf.Max(2, Mathf.RoundToInt(sweep / _degreesPerSegment)); TubePoint[] tubePoints = new TubePoint[segments]; float segLen = 1f / segments; for (int i = 0; i < segments; i++) { float angle = -i * sweep / segments - lowLimit; Quaternion rot = Quaternion.AngleAxis(angle, Vector3.up); tubePoints[i] = new TubePoint { position = rot * Vector3.forward * _radius, rotation = rot * _rotationCorrectionLeft, relativeLength = i * segLen }; } return tubePoints; } #region Inject public void InjectAllTurnArrowVisuals( Renderer leftArrow, Renderer rightArrow, TubeRenderer leftRail, TubeRenderer rightRail, TubeRenderer leftTrail, TubeRenderer rightTrail, MaterialPropertyBlockEditor leftMaterialBlock, MaterialPropertyBlockEditor rightMaterialBlock, float radius, float margin, float trailLength, float maxAngle, float railGap, float squeezeLength) { InjectLeftArrow(leftArrow); InjectRightArrow(rightArrow); InjectLeftRail(leftRail); InjectRightRail(rightRail); InjectLeftTrail(leftTrail); InjectRightTrail(rightTrail); InjectLeftMaterialBlock(leftMaterialBlock); InjectRightMaterialBlock(rightMaterialBlock); InjectRadius(radius); InjectMargin(margin); InjectTrailLength(trailLength); InjectMaxAngle(maxAngle); InjectRailGap(railGap); InjectSqueezeLength(squeezeLength); } public void InjectLeftArrow(Renderer leftArrow) { _leftArrow = leftArrow; } public void InjectRightArrow(Renderer rightArrow) { _rightArrow = rightArrow; } public void InjectLeftRail(TubeRenderer leftRail) { _leftRail = leftRail; } public void InjectRightRail(TubeRenderer rightRail) { _rightRail = rightRail; } public void InjectLeftTrail(TubeRenderer leftTrail) { _leftTrail = leftTrail; } public void InjectRightTrail(TubeRenderer rightTrail) { _rightTrail = rightTrail; } public void InjectLeftMaterialBlock(MaterialPropertyBlockEditor leftMaterialBlock) { _leftMaterialBlock = leftMaterialBlock; } public void InjectRightMaterialBlock(MaterialPropertyBlockEditor rightMaterialBlock) { _rightMaterialBlock = rightMaterialBlock; } public void InjectRadius(float radius) { _radius = radius; } public void InjectMargin(float margin) { _margin = margin; } public void InjectTrailLength(float trailLength) { _trailLength = trailLength; } public void InjectMaxAngle(float maxAngle) { _maxAngle = maxAngle; } public void InjectRailGap(float railGap) { _railGap = railGap; } public void InjectSqueezeLength(float squeezeLength) { _squeezeLength = squeezeLength; } #endregion } }
07-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值