2010-09-30 20:59:21 +00:00
|
|
|
/*
|
2010-02-12 00:36:56 +00:00
|
|
|
|
2012-05-27 14:23:31 +00:00
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-02-12 00:36:56 +00:00
|
|
|
|
2012-05-27 14:23:31 +00:00
|
|
|
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
using System.Drawing.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
2010-09-06 19:53:13 +00:00
|
|
|
using System.Windows.Forms;
|
2010-02-12 00:36:56 +00:00
|
|
|
using OpenHardwareMonitor.Hardware;
|
|
|
|
using OpenHardwareMonitor.Utilities;
|
|
|
|
|
|
|
|
namespace OpenHardwareMonitor.GUI {
|
|
|
|
public class SensorNotifyIcon : IDisposable {
|
|
|
|
|
2012-07-12 10:17:18 +00:00
|
|
|
private UnitManager unitManager;
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
private ISensor sensor;
|
2012-07-14 19:24:04 +00:00
|
|
|
private NotifyIconAdv notifyIcon;
|
2010-02-12 00:36:56 +00:00
|
|
|
private Bitmap bitmap;
|
|
|
|
private Graphics graphics;
|
2010-02-12 22:46:31 +00:00
|
|
|
private Color color;
|
2010-02-13 17:08:36 +00:00
|
|
|
private Color darkColor;
|
|
|
|
private Brush brush;
|
|
|
|
private Brush darkBrush;
|
|
|
|
private Pen pen;
|
2010-03-02 22:52:37 +00:00
|
|
|
private Font font;
|
2012-07-12 10:17:18 +00:00
|
|
|
private Font smallFont;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
2010-06-05 18:59:54 +00:00
|
|
|
public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
|
2012-07-12 10:17:18 +00:00
|
|
|
bool balloonTip, PersistentSettings settings, UnitManager unitManager)
|
2010-02-12 22:46:31 +00:00
|
|
|
{
|
2012-07-12 10:17:18 +00:00
|
|
|
this.unitManager = unitManager;
|
2010-02-12 00:36:56 +00:00
|
|
|
this.sensor = sensor;
|
2012-07-14 19:24:04 +00:00
|
|
|
this.notifyIcon = new NotifyIconAdv();
|
2010-02-13 17:08:36 +00:00
|
|
|
|
|
|
|
Color defaultColor = Color.Black;
|
2010-10-07 19:34:36 +00:00
|
|
|
if (sensor.SensorType == SensorType.Load ||
|
|
|
|
sensor.SensorType == SensorType.Control ||
|
|
|
|
sensor.SensorType == SensorType.Level)
|
|
|
|
{
|
2010-02-13 17:08:36 +00:00
|
|
|
defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
|
|
|
|
}
|
2010-08-12 20:53:27 +00:00
|
|
|
Color = settings.GetValue(new Identifier(sensor.Identifier,
|
2010-05-06 19:20:38 +00:00
|
|
|
"traycolor").ToString(), defaultColor);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
2010-02-13 17:08:36 +00:00
|
|
|
this.pen = new Pen(Color.FromArgb(96, Color.Black));
|
|
|
|
|
2010-07-18 12:38:01 +00:00
|
|
|
ContextMenu contextMenu = new ContextMenu();
|
|
|
|
MenuItem hideShowItem = new MenuItem("Hide/Show");
|
2010-06-05 18:59:54 +00:00
|
|
|
hideShowItem.Click += delegate(object obj, EventArgs args) {
|
|
|
|
sensorSystemTray.SendHideShowCommand();
|
|
|
|
};
|
2010-07-18 12:38:01 +00:00
|
|
|
contextMenu.MenuItems.Add(hideShowItem);
|
|
|
|
contextMenu.MenuItems.Add(new MenuItem("-"));
|
|
|
|
MenuItem removeItem = new MenuItem("Remove Sensor");
|
2010-02-12 22:46:31 +00:00
|
|
|
removeItem.Click += delegate(object obj, EventArgs args) {
|
|
|
|
sensorSystemTray.Remove(this.sensor);
|
2010-02-12 00:36:56 +00:00
|
|
|
};
|
2010-07-18 12:38:01 +00:00
|
|
|
contextMenu.MenuItems.Add(removeItem);
|
|
|
|
MenuItem colorItem = new MenuItem("Change Color...");
|
2010-02-12 22:46:31 +00:00
|
|
|
colorItem.Click += delegate(object obj, EventArgs args) {
|
|
|
|
ColorDialog dialog = new ColorDialog();
|
2010-02-13 17:08:36 +00:00
|
|
|
dialog.Color = Color;
|
2010-02-12 22:46:31 +00:00
|
|
|
if (dialog.ShowDialog() == DialogResult.OK) {
|
2010-02-13 17:08:36 +00:00
|
|
|
Color = dialog.Color;
|
2010-08-12 20:53:27 +00:00
|
|
|
settings.SetValue(new Identifier(sensor.Identifier,
|
2010-05-06 19:20:38 +00:00
|
|
|
"traycolor").ToString(), Color);
|
2010-02-12 22:46:31 +00:00
|
|
|
}
|
|
|
|
};
|
2010-07-18 12:38:01 +00:00
|
|
|
contextMenu.MenuItems.Add(colorItem);
|
|
|
|
contextMenu.MenuItems.Add(new MenuItem("-"));
|
|
|
|
MenuItem exitItem = new MenuItem("Exit");
|
2010-06-05 18:59:54 +00:00
|
|
|
exitItem.Click += delegate(object obj, EventArgs args) {
|
|
|
|
sensorSystemTray.SendExitCommand();
|
|
|
|
};
|
2010-07-18 12:38:01 +00:00
|
|
|
contextMenu.MenuItems.Add(exitItem);
|
|
|
|
this.notifyIcon.ContextMenu = contextMenu;
|
2010-06-05 18:59:54 +00:00
|
|
|
this.notifyIcon.DoubleClick += delegate(object obj, EventArgs args) {
|
|
|
|
sensorSystemTray.SendHideShowCommand();
|
2012-07-18 19:45:59 +00:00
|
|
|
};
|
2010-02-12 00:36:56 +00:00
|
|
|
|
2011-01-30 23:45:26 +00:00
|
|
|
// get the default dpi to create an icon with the correct size
|
|
|
|
float dpiX, dpiY;
|
|
|
|
using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb)) {
|
|
|
|
dpiX = b.HorizontalResolution;
|
|
|
|
dpiY = b.VerticalResolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
|
|
|
|
int width = (int)Math.Round(16 * dpiX / 96);
|
|
|
|
int height = (int)Math.Round(16 * dpiY / 96);
|
|
|
|
|
|
|
|
// make sure it does never get smaller than 16x16
|
2012-07-18 19:45:59 +00:00
|
|
|
width = width < 16 ? 16 : width;
|
|
|
|
height = height < 16 ? 16 : height;
|
|
|
|
|
|
|
|
// adjust the font size to the icon size
|
|
|
|
FontFamily family = SystemFonts.MessageBoxFont.FontFamily;
|
|
|
|
float baseSize;
|
|
|
|
switch (family.Name) {
|
|
|
|
case "Segoe UI": baseSize = 12; break;
|
|
|
|
case "Tahoma": baseSize = 11; break;
|
|
|
|
default: baseSize = 12; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.font = new Font(family,
|
|
|
|
baseSize * width / 16.0f, GraphicsUnit.Pixel);
|
|
|
|
this.smallFont = new Font(family,
|
|
|
|
0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);
|
2011-01-30 23:45:26 +00:00
|
|
|
|
|
|
|
this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
2010-02-12 00:36:56 +00:00
|
|
|
this.graphics = Graphics.FromImage(this.bitmap);
|
2010-05-15 12:50:28 +00:00
|
|
|
|
|
|
|
if (Environment.OSVersion.Version.Major > 5) {
|
|
|
|
this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
|
|
|
|
this.graphics.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
|
}
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ISensor Sensor {
|
|
|
|
get { return sensor; }
|
|
|
|
}
|
|
|
|
|
2010-02-12 22:46:31 +00:00
|
|
|
public Color Color {
|
|
|
|
get { return color; }
|
2010-02-13 17:08:36 +00:00
|
|
|
set {
|
|
|
|
this.color = value;
|
|
|
|
this.darkColor = Color.FromArgb(255,
|
2010-02-14 20:16:30 +00:00
|
|
|
this.color.R / 3,
|
|
|
|
this.color.G / 3,
|
|
|
|
this.color.B / 3);
|
2010-02-13 17:08:36 +00:00
|
|
|
Brush brush = this.brush;
|
|
|
|
this.brush = new SolidBrush(this.color);
|
|
|
|
if (brush != null)
|
|
|
|
brush.Dispose();
|
|
|
|
Brush darkBrush = this.darkBrush;
|
|
|
|
this.darkBrush = new SolidBrush(this.darkColor);
|
|
|
|
if (darkBrush != null)
|
|
|
|
darkBrush.Dispose();
|
|
|
|
}
|
2010-02-12 22:46:31 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
public void Dispose() {
|
|
|
|
Icon icon = notifyIcon.Icon;
|
|
|
|
notifyIcon.Icon = null;
|
|
|
|
if (icon != null)
|
|
|
|
icon.Dispose();
|
|
|
|
notifyIcon.Dispose();
|
|
|
|
|
2010-02-13 17:08:36 +00:00
|
|
|
if (brush != null)
|
|
|
|
brush.Dispose();
|
|
|
|
if (darkBrush != null)
|
|
|
|
darkBrush.Dispose();
|
|
|
|
pen.Dispose();
|
2010-03-27 19:55:09 +00:00
|
|
|
graphics.Dispose();
|
2012-07-12 10:17:18 +00:00
|
|
|
bitmap.Dispose();
|
2012-07-18 19:45:59 +00:00
|
|
|
font.Dispose();
|
2012-07-12 10:17:18 +00:00
|
|
|
smallFont.Dispose();
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetString() {
|
2011-05-15 16:00:22 +00:00
|
|
|
if (!sensor.Value.HasValue)
|
|
|
|
return "-";
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
switch (sensor.SensorType) {
|
|
|
|
case SensorType.Voltage:
|
2012-02-14 23:07:55 +00:00
|
|
|
return string.Format("{0:F1}", sensor.Value);
|
2010-02-12 00:36:56 +00:00
|
|
|
case SensorType.Clock:
|
2012-02-14 23:07:55 +00:00
|
|
|
return string.Format("{0:F1}", 1e-3f * sensor.Value);
|
2010-02-12 00:36:56 +00:00
|
|
|
case SensorType.Load:
|
2010-05-15 14:16:00 +00:00
|
|
|
return string.Format("{0:F0}", sensor.Value);
|
2012-07-12 10:17:18 +00:00
|
|
|
case SensorType.Temperature:
|
|
|
|
if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
|
|
|
|
return string.Format("{0:F0}",
|
|
|
|
UnitManager.CelsiusToFahrenheit(sensor.Value));
|
2012-07-18 19:45:59 +00:00
|
|
|
else
|
2012-07-12 10:17:18 +00:00
|
|
|
return string.Format("{0:F0}", sensor.Value);
|
2010-02-12 00:36:56 +00:00
|
|
|
case SensorType.Fan:
|
2012-02-14 23:07:55 +00:00
|
|
|
return string.Format("{0:F1}", 1e-3f * sensor.Value);
|
2010-02-21 18:10:30 +00:00
|
|
|
case SensorType.Flow:
|
2012-02-14 23:07:55 +00:00
|
|
|
return string.Format("{0:F1}", 1e-3f * sensor.Value);
|
2010-05-15 14:16:00 +00:00
|
|
|
case SensorType.Control:
|
|
|
|
return string.Format("{0:F0}", sensor.Value);
|
2010-10-07 19:34:36 +00:00
|
|
|
case SensorType.Level:
|
|
|
|
return string.Format("{0:F0}", sensor.Value);
|
2011-07-27 18:27:16 +00:00
|
|
|
case SensorType.Power:
|
|
|
|
return string.Format("{0:F0}", sensor.Value);
|
2011-12-31 17:31:04 +00:00
|
|
|
case SensorType.Data:
|
|
|
|
return string.Format("{0:F0}", sensor.Value);
|
2012-02-14 23:07:55 +00:00
|
|
|
case SensorType.Factor:
|
|
|
|
return string.Format("{0:F1}", sensor.Value);
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
|
|
|
return "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
private Icon CreateTransparentIcon() {
|
2012-07-12 10:17:18 +00:00
|
|
|
string text = GetString();
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < text.Length; i++)
|
|
|
|
if ((text[i] >= '0' && text[i] <= '9') || text[i] == '-')
|
|
|
|
count++;
|
|
|
|
bool small = count > 2;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
|
|
|
graphics.Clear(Color.Black);
|
2012-07-12 10:17:18 +00:00
|
|
|
TextRenderer.DrawText(graphics, text, small ? smallFont : font,
|
|
|
|
new Point(-2, small ? 1 : 0), Color.White, Color.Black);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
|
|
|
BitmapData data = bitmap.LockBits(
|
|
|
|
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
|
|
|
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
|
|
|
|
|
|
IntPtr Scan0 = data.Scan0;
|
|
|
|
|
|
|
|
int numBytes = bitmap.Width * bitmap.Height * 4;
|
|
|
|
byte[] bytes = new byte[numBytes];
|
|
|
|
Marshal.Copy(Scan0, bytes, 0, numBytes);
|
|
|
|
bitmap.UnlockBits(data);
|
|
|
|
|
|
|
|
byte red, green, blue;
|
|
|
|
for (int i = 0; i < bytes.Length; i += 4) {
|
|
|
|
blue = bytes[i];
|
|
|
|
green = bytes[i + 1];
|
|
|
|
red = bytes[i + 2];
|
|
|
|
|
2010-02-12 22:46:31 +00:00
|
|
|
bytes[i] = color.B;
|
|
|
|
bytes[i + 1] = color.G;
|
|
|
|
bytes[i + 2] = color.R;
|
2010-02-12 00:36:56 +00:00
|
|
|
bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
|
|
|
|
}
|
|
|
|
|
2011-01-30 23:45:26 +00:00
|
|
|
return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
|
|
|
|
PixelFormat.Format32bppArgb);
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-07 19:34:36 +00:00
|
|
|
private Icon CreatePercentageIcon() {
|
2010-04-02 16:05:07 +00:00
|
|
|
try {
|
|
|
|
graphics.Clear(Color.Transparent);
|
|
|
|
} catch (ArgumentException) {
|
|
|
|
graphics.Clear(Color.Black);
|
|
|
|
}
|
2011-01-30 23:45:26 +00:00
|
|
|
graphics.FillRectangle(darkBrush, 0.5f, -0.5f, bitmap.Width - 2, bitmap.Height);
|
2011-05-15 16:00:22 +00:00
|
|
|
float value = sensor.Value.GetValueOrDefault();
|
|
|
|
float y = 0.16f * (100 - value);
|
2011-01-30 23:45:26 +00:00
|
|
|
graphics.FillRectangle(brush, 0.5f, -0.5f + y, bitmap.Width - 2, bitmap.Height - y);
|
|
|
|
graphics.DrawRectangle(pen, 1, 0, bitmap.Width - 3, bitmap.Height - 1);
|
2010-02-13 17:08:36 +00:00
|
|
|
|
|
|
|
BitmapData data = bitmap.LockBits(
|
|
|
|
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
|
|
|
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
|
|
byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
|
|
|
|
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
|
|
|
|
bitmap.UnlockBits(data);
|
|
|
|
|
2011-01-30 23:45:26 +00:00
|
|
|
return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
|
|
|
|
PixelFormat.Format32bppArgb);
|
2010-02-13 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
public void Update() {
|
|
|
|
Icon icon = notifyIcon.Icon;
|
|
|
|
|
2010-10-07 19:34:36 +00:00
|
|
|
switch (sensor.SensorType) {
|
|
|
|
case SensorType.Load:
|
|
|
|
case SensorType.Control:
|
|
|
|
case SensorType.Level:
|
|
|
|
notifyIcon.Icon = CreatePercentageIcon();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
notifyIcon.Icon = CreateTransparentIcon();
|
|
|
|
break;
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
2010-10-07 19:34:36 +00:00
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
if (icon != null)
|
|
|
|
icon.Dispose();
|
|
|
|
|
|
|
|
string format = "";
|
|
|
|
switch (sensor.SensorType) {
|
2010-05-15 11:34:36 +00:00
|
|
|
case SensorType.Voltage: format = "\n{0}: {1:F2} V"; break;
|
|
|
|
case SensorType.Clock: format = "\n{0}: {1:F0} MHz"; break;
|
|
|
|
case SensorType.Load: format = "\n{0}: {1:F1} %"; break;
|
|
|
|
case SensorType.Temperature: format = "\n{0}: {1:F1} °C"; break;
|
|
|
|
case SensorType.Fan: format = "\n{0}: {1:F0} RPM"; break;
|
|
|
|
case SensorType.Flow: format = "\n{0}: {1:F0} L/h"; break;
|
2010-05-15 14:16:00 +00:00
|
|
|
case SensorType.Control: format = "\n{0}: {1:F1} %"; break;
|
2010-10-07 19:34:36 +00:00
|
|
|
case SensorType.Level: format = "\n{0}: {1:F1} %"; break;
|
2011-07-27 18:27:16 +00:00
|
|
|
case SensorType.Power: format = "\n{0}: {1:F0} W"; break;
|
2011-12-31 17:31:04 +00:00
|
|
|
case SensorType.Data: format = "\n{0}: {1:F0} GB"; break;
|
2012-02-14 23:07:55 +00:00
|
|
|
case SensorType.Factor: format = "\n{0}: {1:F3} GB"; break;
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
2010-05-15 11:34:36 +00:00
|
|
|
string formattedValue = string.Format(format, sensor.Name, sensor.Value);
|
2012-07-12 10:17:18 +00:00
|
|
|
|
|
|
|
if (sensor.SensorType == SensorType.Temperature &&
|
|
|
|
unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
|
|
|
|
{
|
|
|
|
format = "\n{0}: {1:F1} °F";
|
|
|
|
formattedValue = string.Format(format, sensor.Name,
|
|
|
|
UnitManager.CelsiusToFahrenheit(sensor.Value));
|
|
|
|
}
|
|
|
|
|
2010-05-15 11:34:36 +00:00
|
|
|
string hardwareName = sensor.Hardware.Name;
|
|
|
|
hardwareName = hardwareName.Substring(0,
|
|
|
|
Math.Min(63 - formattedValue.Length, hardwareName.Length));
|
|
|
|
string text = hardwareName + formattedValue;
|
|
|
|
if (text.Length > 63)
|
|
|
|
text = null;
|
|
|
|
|
|
|
|
notifyIcon.Text = text;
|
2010-02-12 22:46:31 +00:00
|
|
|
notifyIcon.Visible = true;
|
2010-02-12 00:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|