First system tray sensor monitor support.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
41 using System.Drawing.Drawing2D;
42 using System.Drawing.Imaging;
43 using System.Drawing.Text;
45 using System.Windows.Forms;
46 using System.Runtime.InteropServices;
47 using OpenHardwareMonitor.Hardware;
48 using OpenHardwareMonitor.Utilities;
50 namespace OpenHardwareMonitor.GUI {
51 public class SensorNotifyIcon : IDisposable {
53 private ISensor sensor;
54 private NotifyIcon notifyIcon;
55 private Bitmap bitmap;
56 private Graphics graphics;
57 private int majorVersion;
59 public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor) {
61 this.notifyIcon = new NotifyIcon();
62 this.majorVersion = Environment.OSVersion.Version.Major;
64 ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
65 ToolStripMenuItem item = new ToolStripMenuItem("Remove");
66 item.Click += delegate(object obj, EventArgs args) {
67 sensorSystemTray.Remove(sensor);
69 contextMenuStrip.Items.Add(item);
70 this.notifyIcon.ContextMenuStrip = contextMenuStrip;
72 this.bitmap = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
73 this.graphics = Graphics.FromImage(this.bitmap);
74 this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
75 this.graphics.SmoothingMode = SmoothingMode.HighQuality;
78 public ISensor Sensor {
79 get { return sensor; }
82 public void Dispose() {
83 Icon icon = notifyIcon.Icon;
84 notifyIcon.Icon = null;
96 private string GetString() {
97 switch (sensor.SensorType) {
98 case SensorType.Voltage:
99 return string.Format("{0:F11}", sensor.Value);
100 case SensorType.Clock:
101 return string.Format("{0:F11}", 1e-3f * sensor.Value);
102 case SensorType.Load:
103 return string.Format("{0:F0}", sensor.Value < 99 ? sensor.Value : 99);
104 case SensorType.Temperature:
105 return string.Format("{0:F0}", sensor.Value);
107 return string.Format("{0:F11}", 1e-3f * sensor.Value);
112 private Icon CreateSimpleIcon() {
114 graphics.Clear(SystemColors.ButtonFace);
115 TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
116 new Point(-2, 0), Color.Blue, SystemColors.ButtonFace);
118 BitmapData data = bitmap.LockBits(
119 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
120 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
122 int stride = data.Stride;
123 IntPtr Scan0 = data.Scan0;
125 int numBytes = bitmap.Width * bitmap.Height * 4;
126 byte[] bytes = new byte[numBytes];
127 Marshal.Copy(Scan0, bytes, 0, numBytes);
128 bitmap.UnlockBits(data);
130 return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
133 private Icon CreateTransparentIcon() {
135 graphics.Clear(Color.Black);
136 TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
137 new Point(-2, 0), Color.White, Color.Black);
139 BitmapData data = bitmap.LockBits(
140 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
141 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
143 int stride = data.Stride;
144 IntPtr Scan0 = data.Scan0;
146 int numBytes = bitmap.Width * bitmap.Height * 4;
147 byte[] bytes = new byte[numBytes];
148 Marshal.Copy(Scan0, bytes, 0, numBytes);
149 bitmap.UnlockBits(data);
151 byte red, green, blue;
152 for (int i = 0; i < bytes.Length; i += 4) {
154 green = bytes[i + 1];
160 bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
163 return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
166 public void Update() {
167 Icon icon = notifyIcon.Icon;
169 if (majorVersion < 6) {
170 notifyIcon.Icon = CreateSimpleIcon();
172 notifyIcon.Icon = CreateTransparentIcon();
179 switch (sensor.SensorType) {
180 case SensorType.Voltage: format = "{0}\n{1}: {2:F2} V"; break;
181 case SensorType.Clock: format = "{0}\n{1}: {2:F0} MHz"; break;
182 case SensorType.Load: format = "{0}\n{1}: {2:F1} %"; break;
183 case SensorType.Temperature: format = "{0}\n{1}: {2:F1} °C"; break;
184 case SensorType.Fan: format = "{0}\n{1}: {2:F0} RPM"; break;
187 notifyIcon.Text = string.Format(format,
188 sensor.Hardware.Name, sensor.Name, sensor.Value);
189 notifyIcon.Visible = true;