我怎样才能使我的图形更新
本文关键字:我的 图形 更新 | 更新日期: 2023-09-27 17:51:04
目前我正在做一个项目,需要你的CPU temperature
,然后把它放在一个很酷的图形。但它只取给定的第一个值,然后停止。它不会一直更新。我试过while循环,但它们似乎不起作用。这是我当前的代码:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using OpenHardwareMonitor;
using OpenHardwareMonitor.Hardware;
namespace pc_version
{
public class CircularProgressBar : Control
{
#region "Properties"
private Color _BorderColor;
public Color BorderColor
{
get { return _BorderColor; }
set
{
_BorderColor = value;
this.Invalidate();
}
}
private Color _InnerColor;
public Color InnerColor
{
get { return _InnerColor; }
set
{
_InnerColor = value;
this.Invalidate();
}
}
private bool _ShowPercentage;
public bool ShowPercentage
{
get { return _ShowPercentage; }
set
{
_ShowPercentage = value;
this.Invalidate();
}
}
private int _BorderWidth;
public int BorderWidth
{
get { return _BorderWidth; }
set
{
_BorderWidth = value;
this.Invalidate();
}
}
private float _Value;
public float Value
{
get { return _Value; }
set
{
//----------------------------------GETTING THE CPU TEMPS--------------------------
Computer thisComputer;
thisComputer = new Computer() { CPUEnabled = true };
thisComputer.Open();
String temp = "";
List<String> list = new List<string>();
foreach (var hardwareItem in thisComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.CPU)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareItem.SubHardware)
subHardware.Update();
//Temps
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
temp = sensor.Value.ToString();
int chunkSize = 2;
int stringLength = temp.Length;
for (int i = 0; i < stringLength; i += chunkSize)
{
if (i + chunkSize > stringLength) chunkSize = stringLength - i;
list.Add(temp.Substring(i, chunkSize));
}
}
}
}
}
// ---------------------------------- giving the graph a value--------------------
float average = float.Parse(list[4]);
_Value = average;
this.Invalidate();
}
}
// -----------------------------drawing the graph---------------------------------------------------------------
#endregion
#region "Constructor"
public CircularProgressBar()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
_Value = 100;
_BorderColor = Color.Orange;
_BorderWidth = 30;
_ShowPercentage = true;
_InnerColor = Color.DarkGray;
this.ForeColor = Color.White;
}
#endregion
#region "Painting"
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//Measure the single parts
int Diameter = Math.Min(this.ClientSize.Width, this.ClientSize.Height);
int InnerDiameter = Diameter - BorderWidth;
Rectangle PieRect = new Rectangle(Convert.ToInt32(this.ClientSize.Width / 2 - Diameter / 2), Convert.ToInt32(this.ClientSize.Height / 2 - Diameter / 2), Diameter, Diameter);
Rectangle InnerRect = new Rectangle(Convert.ToInt32(this.ClientSize.Width / 2 - InnerDiameter / 2), Convert.ToInt32(this.ClientSize.Height / 2 - InnerDiameter / 2), InnerDiameter, InnerDiameter);
//Draw outer ring
using (SolidBrush b = new SolidBrush(BorderColor))
{
e.Graphics.FillPie(b, PieRect, 0, Value / 100 * 360);
}
//Draw inner ring
using (SolidBrush b = new SolidBrush(this._InnerColor))
{
e.Graphics.FillEllipse(b, InnerRect);
}
//Draw percentage
if (ShowPercentage)
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
using (SolidBrush b = new SolidBrush(this.ForeColor))
{
e.Graphics.DrawString(Convert.ToInt32(Value).ToString() + "°C", this.Font, b, InnerRect, sf);
}
}
}
this.Update();
}
}
#endregion
}
这是结果的图像:http://pasteboard.co/PayHmNB.png
我如何添加到现有的代码,以保持更新的值和值的形式?谢谢你
这相当直接。从Value属性的setter中剥离逻辑,并将其放入自己的函数中。除了获取和设置私有成员值之外,将逻辑放入getter和setter中通常是不好的做法。像这样:
Timer pollTimer = new System.Timers.Timer(1000); // fires every second
public void Initialize()
{
// subscribe the PollTemperature function to the Elapsed event.
pollTimer.Elapsed += PollTemperature;
// Enable the periodic polling
pollTimer.Enabled = true;
}
private float _Value;
public float Value
{
get { return _Value; }
set
{
if(_value != value)
{
_value = value
}
}
}
public decimal GetTemperature()
{
// get the temperature of the cpu sensor here
float yourTemperature = resultsOfYourLogic;
return yourTemperature;
}
void PollTemperature(Object source, ElapsedEventArgs e)
{
Value = GetTemperature();
this.Invalidate();
this.Update();
}
这设置了一个计时器,每秒钟轮询你的温度和更新你的值属性。