StephaneLenclud@445
|
1 |
/*
|
StephaneLenclud@445
|
2 |
|
StephaneLenclud@445
|
3 |
This Source Code Form is subject to the terms of the Mozilla Public
|
StephaneLenclud@445
|
4 |
License, v. 2.0. If a copy of the MPL was not distributed with this
|
StephaneLenclud@445
|
5 |
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
StephaneLenclud@445
|
6 |
|
StephaneLenclud@445
|
7 |
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
|
StephaneLenclud@445
|
8 |
|
StephaneLenclud@445
|
9 |
*/
|
StephaneLenclud@445
|
10 |
|
StephaneLenclud@445
|
11 |
using System;
|
StephaneLenclud@445
|
12 |
using System.Drawing;
|
StephaneLenclud@445
|
13 |
using System.Drawing.Drawing2D;
|
StephaneLenclud@445
|
14 |
using System.Drawing.Imaging;
|
StephaneLenclud@445
|
15 |
using System.Drawing.Text;
|
StephaneLenclud@445
|
16 |
using System.Runtime.InteropServices;
|
StephaneLenclud@445
|
17 |
using System.Windows.Forms;
|
StephaneLenclud@445
|
18 |
using OpenHardwareMonitor.Hardware;
|
StephaneLenclud@445
|
19 |
using OpenHardwareMonitor.Utilities;
|
StephaneLenclud@445
|
20 |
|
StephaneLenclud@445
|
21 |
namespace OpenHardwareMonitor.GUI
|
StephaneLenclud@445
|
22 |
{
|
StephaneLenclud@445
|
23 |
public class SensorSharpDisplay : IDisposable
|
StephaneLenclud@445
|
24 |
{
|
StephaneLenclud@445
|
25 |
|
StephaneLenclud@445
|
26 |
private UnitManager unitManager;
|
StephaneLenclud@445
|
27 |
|
StephaneLenclud@445
|
28 |
private ISensor sensor;
|
StephaneLenclud@445
|
29 |
private Color color;
|
StephaneLenclud@445
|
30 |
private Color darkColor;
|
StephaneLenclud@445
|
31 |
private Font font;
|
StephaneLenclud@445
|
32 |
private Font smallFont;
|
StephaneLenclud@445
|
33 |
public string iFirstLine;
|
StephaneLenclud@445
|
34 |
public string iSecondLine;
|
StephaneLenclud@445
|
35 |
|
StephaneLenclud@445
|
36 |
|
StephaneLenclud@445
|
37 |
public SensorSharpDisplay(SharpDisplay soundGraphDisplay, ISensor sensor,
|
StephaneLenclud@445
|
38 |
bool balloonTip, PersistentSettings settings, UnitManager unitManager)
|
StephaneLenclud@445
|
39 |
{
|
StephaneLenclud@445
|
40 |
this.unitManager = unitManager;
|
StephaneLenclud@445
|
41 |
this.sensor = sensor;
|
StephaneLenclud@445
|
42 |
|
StephaneLenclud@445
|
43 |
// get the default dpi to create an icon with the correct size
|
StephaneLenclud@445
|
44 |
float dpiX, dpiY;
|
StephaneLenclud@445
|
45 |
using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
|
StephaneLenclud@445
|
46 |
{
|
StephaneLenclud@445
|
47 |
dpiX = b.HorizontalResolution;
|
StephaneLenclud@445
|
48 |
dpiY = b.VerticalResolution;
|
StephaneLenclud@445
|
49 |
}
|
StephaneLenclud@445
|
50 |
|
StephaneLenclud@445
|
51 |
// adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
|
StephaneLenclud@445
|
52 |
int width = (int)Math.Round(16 * dpiX / 96);
|
StephaneLenclud@445
|
53 |
int height = (int)Math.Round(16 * dpiY / 96);
|
StephaneLenclud@445
|
54 |
|
StephaneLenclud@445
|
55 |
// make sure it does never get smaller than 16x16
|
StephaneLenclud@445
|
56 |
width = width < 16 ? 16 : width;
|
StephaneLenclud@445
|
57 |
height = height < 16 ? 16 : height;
|
StephaneLenclud@445
|
58 |
|
StephaneLenclud@445
|
59 |
// adjust the font size to the icon size
|
StephaneLenclud@445
|
60 |
FontFamily family = SystemFonts.MessageBoxFont.FontFamily;
|
StephaneLenclud@445
|
61 |
float baseSize;
|
StephaneLenclud@445
|
62 |
switch (family.Name)
|
StephaneLenclud@445
|
63 |
{
|
StephaneLenclud@445
|
64 |
case "Segoe UI": baseSize = 12; break;
|
StephaneLenclud@445
|
65 |
case "Tahoma": baseSize = 11; break;
|
StephaneLenclud@445
|
66 |
default: baseSize = 12; break;
|
StephaneLenclud@445
|
67 |
}
|
StephaneLenclud@445
|
68 |
|
StephaneLenclud@445
|
69 |
this.font = new Font(family,
|
StephaneLenclud@445
|
70 |
baseSize * width / 16.0f, GraphicsUnit.Pixel);
|
StephaneLenclud@445
|
71 |
this.smallFont = new Font(family,
|
StephaneLenclud@445
|
72 |
0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);
|
StephaneLenclud@445
|
73 |
|
StephaneLenclud@445
|
74 |
}
|
StephaneLenclud@445
|
75 |
|
StephaneLenclud@445
|
76 |
public ISensor Sensor
|
StephaneLenclud@445
|
77 |
{
|
StephaneLenclud@445
|
78 |
get { return sensor; }
|
StephaneLenclud@445
|
79 |
}
|
StephaneLenclud@445
|
80 |
|
StephaneLenclud@445
|
81 |
public Color Color
|
StephaneLenclud@445
|
82 |
{
|
StephaneLenclud@445
|
83 |
get { return color; }
|
StephaneLenclud@445
|
84 |
set
|
StephaneLenclud@445
|
85 |
{
|
StephaneLenclud@445
|
86 |
this.color = value;
|
StephaneLenclud@445
|
87 |
this.darkColor = Color.FromArgb(255,
|
StephaneLenclud@445
|
88 |
this.color.R / 3,
|
StephaneLenclud@445
|
89 |
this.color.G / 3,
|
StephaneLenclud@445
|
90 |
this.color.B / 3);
|
StephaneLenclud@445
|
91 |
}
|
StephaneLenclud@445
|
92 |
}
|
StephaneLenclud@445
|
93 |
|
StephaneLenclud@445
|
94 |
public void Dispose()
|
StephaneLenclud@445
|
95 |
{
|
StephaneLenclud@445
|
96 |
font.Dispose();
|
StephaneLenclud@445
|
97 |
smallFont.Dispose();
|
StephaneLenclud@445
|
98 |
}
|
StephaneLenclud@445
|
99 |
|
StephaneLenclud@445
|
100 |
public string GetString()
|
StephaneLenclud@445
|
101 |
{
|
StephaneLenclud@445
|
102 |
if (!sensor.Value.HasValue)
|
StephaneLenclud@445
|
103 |
return "-";
|
StephaneLenclud@445
|
104 |
|
StephaneLenclud@445
|
105 |
switch (sensor.SensorType)
|
StephaneLenclud@445
|
106 |
{
|
StephaneLenclud@445
|
107 |
case SensorType.Voltage:
|
StephaneLenclud@445
|
108 |
return string.Format("{0:F1}", sensor.Value);
|
StephaneLenclud@445
|
109 |
case SensorType.Clock:
|
StephaneLenclud@445
|
110 |
return string.Format("{0:F1}", 1e-3f * sensor.Value);
|
StephaneLenclud@445
|
111 |
case SensorType.Load:
|
StephaneLenclud@445
|
112 |
return string.Format("{0:F0}", sensor.Value);
|
StephaneLenclud@445
|
113 |
case SensorType.Temperature:
|
StephaneLenclud@445
|
114 |
if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
|
StephaneLenclud@445
|
115 |
return string.Format("{0:F0}",
|
StephaneLenclud@445
|
116 |
UnitManager.CelsiusToFahrenheit(sensor.Value));
|
StephaneLenclud@445
|
117 |
else
|
StephaneLenclud@445
|
118 |
return string.Format("{0:F0}", sensor.Value);
|
StephaneLenclud@445
|
119 |
case SensorType.Fan:
|
StephaneLenclud@445
|
120 |
return string.Format("{0:F1}", 1e-3f * sensor.Value);
|
StephaneLenclud@445
|
121 |
case SensorType.Flow:
|
StephaneLenclud@445
|
122 |
return string.Format("{0:F1}", 1e-3f * sensor.Value);
|
StephaneLenclud@445
|
123 |
case SensorType.Control:
|
StephaneLenclud@445
|
124 |
return string.Format("{0:F0}", sensor.Value);
|
StephaneLenclud@445
|
125 |
case SensorType.Level:
|
StephaneLenclud@445
|
126 |
return string.Format("{0:F0}", sensor.Value);
|
StephaneLenclud@445
|
127 |
case SensorType.Power:
|
StephaneLenclud@445
|
128 |
return string.Format("{0:F0}", sensor.Value);
|
StephaneLenclud@445
|
129 |
case SensorType.Data:
|
StephaneLenclud@445
|
130 |
return string.Format("{0:F0}", sensor.Value);
|
StephaneLenclud@445
|
131 |
case SensorType.Factor:
|
StephaneLenclud@445
|
132 |
return string.Format("{0:F1}", sensor.Value);
|
StephaneLenclud@445
|
133 |
}
|
StephaneLenclud@445
|
134 |
return "-";
|
StephaneLenclud@445
|
135 |
}
|
StephaneLenclud@445
|
136 |
|
StephaneLenclud@445
|
137 |
|
StephaneLenclud@445
|
138 |
public void Update()
|
StephaneLenclud@445
|
139 |
{
|
StephaneLenclud@445
|
140 |
|
StephaneLenclud@445
|
141 |
|
StephaneLenclud@445
|
142 |
switch (sensor.SensorType)
|
StephaneLenclud@445
|
143 |
{
|
StephaneLenclud@445
|
144 |
case SensorType.Load:
|
StephaneLenclud@445
|
145 |
case SensorType.Control:
|
StephaneLenclud@445
|
146 |
case SensorType.Level:
|
StephaneLenclud@445
|
147 |
//notifyIcon.Icon = CreatePercentageIcon();
|
StephaneLenclud@445
|
148 |
break;
|
StephaneLenclud@445
|
149 |
default:
|
StephaneLenclud@445
|
150 |
//notifyIcon.Icon = CreateTransparentIcon();
|
StephaneLenclud@445
|
151 |
break;
|
StephaneLenclud@445
|
152 |
}
|
StephaneLenclud@445
|
153 |
|
StephaneLenclud@445
|
154 |
|
StephaneLenclud@445
|
155 |
string format = "";
|
StephaneLenclud@445
|
156 |
switch (sensor.SensorType)
|
StephaneLenclud@445
|
157 |
{
|
StephaneLenclud@445
|
158 |
case SensorType.Voltage: format = "{0:F2}V"; break;
|
StephaneLenclud@445
|
159 |
case SensorType.Clock: format = "{0:F0}MHz"; break;
|
StephaneLenclud@445
|
160 |
case SensorType.Load: format = "{0:F0}%"; break;
|
StephaneLenclud@445
|
161 |
//iMON VFD escape sequence for Celsius
|
StephaneLenclud@445
|
162 |
case SensorType.Temperature: format = "{0:F0}°C"; break;
|
StephaneLenclud@446
|
163 |
case SensorType.Fan: format = "{0:F0}R"; break; //RPM
|
StephaneLenclud@445
|
164 |
case SensorType.Flow: format = "{0:F0}L/h"; break;
|
StephaneLenclud@445
|
165 |
case SensorType.Control: format = "{0:F0}%"; break;
|
StephaneLenclud@445
|
166 |
case SensorType.Level: format = "{0:F0}%"; break;
|
StephaneLenclud@445
|
167 |
case SensorType.Power: format = "{0:F0}W"; break;
|
StephaneLenclud@445
|
168 |
case SensorType.Data: format = "{0:F0}GB"; break;
|
StephaneLenclud@445
|
169 |
case SensorType.Factor: format = "{0:F3}GB"; break;
|
StephaneLenclud@445
|
170 |
}
|
StephaneLenclud@445
|
171 |
string formattedValue = string.Format(format, sensor.Value);
|
StephaneLenclud@445
|
172 |
|
StephaneLenclud@445
|
173 |
if (sensor.SensorType == SensorType.Temperature &&
|
StephaneLenclud@445
|
174 |
unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
|
StephaneLenclud@445
|
175 |
{
|
StephaneLenclud@445
|
176 |
//iMON VFD escape sequence for Fahrenheit
|
StephaneLenclud@445
|
177 |
format = "{0:F0}°F";
|
StephaneLenclud@445
|
178 |
formattedValue = string.Format(format, UnitManager.CelsiusToFahrenheit(sensor.Value));
|
StephaneLenclud@445
|
179 |
}
|
StephaneLenclud@445
|
180 |
|
StephaneLenclud@445
|
181 |
//iFirstLine = sensor.Hardware.Name;
|
StephaneLenclud@445
|
182 |
//iSecondLine = sensor.Name+ ":" + formattedValue;
|
StephaneLenclud@445
|
183 |
|
StephaneLenclud@445
|
184 |
iFirstLine = sensor.Name;
|
StephaneLenclud@445
|
185 |
iSecondLine = formattedValue;
|
StephaneLenclud@445
|
186 |
|
StephaneLenclud@445
|
187 |
|
StephaneLenclud@445
|
188 |
}
|
StephaneLenclud@445
|
189 |
}
|
StephaneLenclud@445
|
190 |
}
|