mirror of
https://github.com/openhardwaremonitor/openhardwaremonitor
synced 2025-08-23 10:27:26 +00:00
238 lines
8.3 KiB
C#
238 lines
8.3 KiB
C#
/*
|
|
|
|
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
for the specific language governing rights and limitations under the License.
|
|
|
|
The Original Code is the Open Hardware Monitor code.
|
|
|
|
The Initial Developer of the Original Code is
|
|
Michael Möller <m.moeller@gmx.ch>.
|
|
Portions created by the Initial Developer are Copyright (C) 2009-2010
|
|
the Initial Developer. All Rights Reserved.
|
|
|
|
Contributor(s):
|
|
|
|
Alternatively, the contents of this file may be used under the terms of
|
|
either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
in which case the provisions of the GPL or the LGPL are applicable instead
|
|
of those above. If you wish to allow use of your version of this file only
|
|
under the terms of either the GPL or the LGPL, and not to allow others to
|
|
use your version of this file under the terms of the MPL, indicate your
|
|
decision by deleting the provisions above and replace them with the notice
|
|
and other provisions required by the GPL or the LGPL. If you do not delete
|
|
the provisions above, a recipient may use your version of this file under
|
|
the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing.Text;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Runtime.InteropServices;
|
|
using OpenHardwareMonitor.Hardware;
|
|
using OpenHardwareMonitor.Utilities;
|
|
|
|
namespace OpenHardwareMonitor.GUI {
|
|
public class SensorNotifyIcon : IDisposable {
|
|
|
|
private ISensor sensor;
|
|
private NotifyIcon notifyIcon;
|
|
private Bitmap bitmap;
|
|
private Graphics graphics;
|
|
private Color color;
|
|
private Color darkColor;
|
|
private Brush brush;
|
|
private Brush darkBrush;
|
|
private Pen pen;
|
|
|
|
public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor,
|
|
bool balloonTip)
|
|
{
|
|
this.sensor = sensor;
|
|
this.notifyIcon = new NotifyIcon();
|
|
|
|
Color defaultColor = Color.Black;
|
|
if (sensor.SensorType == SensorType.Load) {
|
|
defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
|
|
}
|
|
Color = Config.Get(sensor.Identifier + "/traycolor", defaultColor);
|
|
|
|
this.pen = new Pen(Color.FromArgb(96, Color.Black));
|
|
|
|
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
|
|
ToolStripMenuItem removeItem = new ToolStripMenuItem("Remove");
|
|
removeItem.Click += delegate(object obj, EventArgs args) {
|
|
sensorSystemTray.Remove(this.sensor);
|
|
};
|
|
contextMenuStrip.Items.Add(removeItem);
|
|
ToolStripMenuItem colorItem = new ToolStripMenuItem("Change Color...");
|
|
colorItem.Click += delegate(object obj, EventArgs args) {
|
|
ColorDialog dialog = new ColorDialog();
|
|
dialog.Color = Color;
|
|
if (dialog.ShowDialog() == DialogResult.OK) {
|
|
Color = dialog.Color;
|
|
Config.Set(sensor.Identifier + "/traycolor", Color);
|
|
}
|
|
};
|
|
contextMenuStrip.Items.Add(colorItem);
|
|
this.notifyIcon.ContextMenuStrip = contextMenuStrip;
|
|
|
|
this.bitmap = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
|
|
this.graphics = Graphics.FromImage(this.bitmap);
|
|
this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
|
|
this.graphics.SmoothingMode = SmoothingMode.HighQuality;
|
|
}
|
|
|
|
public ISensor Sensor {
|
|
get { return sensor; }
|
|
}
|
|
|
|
public Color Color {
|
|
get { return color; }
|
|
set {
|
|
this.color = value;
|
|
this.darkColor = Color.FromArgb(255,
|
|
this.color.R / 3,
|
|
this.color.G / 3,
|
|
this.color.B / 3);
|
|
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();
|
|
}
|
|
}
|
|
|
|
public void Dispose() {
|
|
Icon icon = notifyIcon.Icon;
|
|
notifyIcon.Icon = null;
|
|
if (icon != null)
|
|
icon.Dispose();
|
|
notifyIcon.Dispose();
|
|
notifyIcon = null;
|
|
|
|
if (brush != null)
|
|
brush.Dispose();
|
|
if (darkBrush != null)
|
|
darkBrush.Dispose();
|
|
pen.Dispose();
|
|
graphics.Dispose();
|
|
graphics = null;
|
|
bitmap.Dispose();
|
|
bitmap = null;
|
|
}
|
|
|
|
private string GetString() {
|
|
switch (sensor.SensorType) {
|
|
case SensorType.Voltage:
|
|
return string.Format("{0:F11}", sensor.Value);
|
|
case SensorType.Clock:
|
|
return string.Format("{0:F11}", 1e-3f * sensor.Value);
|
|
case SensorType.Load:
|
|
return string.Format("{0:F0}", sensor.Value < 99 ? sensor.Value : 99);
|
|
case SensorType.Temperature:
|
|
return string.Format("{0:F0}", sensor.Value);
|
|
case SensorType.Fan:
|
|
return string.Format("{0:F11}", 1e-3f * sensor.Value);
|
|
case SensorType.Flow:
|
|
return string.Format("{0:F11}", 1e-3f * sensor.Value);
|
|
}
|
|
return "-";
|
|
}
|
|
|
|
private Icon CreateTransparentIcon() {
|
|
|
|
graphics.Clear(Color.Black);
|
|
TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
|
|
new Point(-2, 0), Color.White, Color.Black);
|
|
|
|
BitmapData data = bitmap.LockBits(
|
|
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
|
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
|
|
int stride = data.Stride;
|
|
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];
|
|
|
|
bytes[i] = color.B;
|
|
bytes[i + 1] = color.G;
|
|
bytes[i + 2] = color.R;
|
|
bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
|
|
}
|
|
|
|
return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
|
|
}
|
|
|
|
private Icon CreateLoadIcon() {
|
|
graphics.Clear(Color.Transparent);
|
|
graphics.FillRectangle(darkBrush, 0.5f, -0.5f, 14, 16);
|
|
float y = 0.16f * (100 - sensor.Value.Value);
|
|
graphics.FillRectangle(brush, 0.5f, -0.5f + y, 14, 16 - y);
|
|
graphics.DrawRectangle(pen, 1, 0, 13, 15);
|
|
|
|
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);
|
|
|
|
return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
|
|
}
|
|
|
|
public void Update() {
|
|
Icon icon = notifyIcon.Icon;
|
|
|
|
if (sensor.SensorType == SensorType.Load) {
|
|
notifyIcon.Icon = CreateLoadIcon();
|
|
} else {
|
|
notifyIcon.Icon = CreateTransparentIcon();
|
|
}
|
|
if (icon != null)
|
|
icon.Dispose();
|
|
|
|
string format = "";
|
|
switch (sensor.SensorType) {
|
|
case SensorType.Voltage: format = "{0}\n{1}: {2:F2} V"; break;
|
|
case SensorType.Clock: format = "{0}\n{1}: {2:F0} MHz"; break;
|
|
case SensorType.Load: format = "{0}\n{1}: {2:F1} %"; break;
|
|
case SensorType.Temperature: format = "{0}\n{1}: {2:F1} °C"; break;
|
|
case SensorType.Fan: format = "{0}\n{1}: {2:F0} RPM"; break;
|
|
case SensorType.Flow: format = "{0}\n{1}: {2:F0} L/h"; break;
|
|
}
|
|
|
|
notifyIcon.Text = string.Format(format, sensor.Hardware.Name, sensor.Name,
|
|
sensor.Value);
|
|
notifyIcon.Visible = true;
|
|
}
|
|
}
|
|
}
|