Tray sensor display default color is black and color can be changed now. Fixed CPU load reading for AMD CPUs and added additional misc device for AMD core temperature reading.
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;
60 public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor,
64 this.notifyIcon = new NotifyIcon();
65 this.majorVersion = Environment.OSVersion.Version.Major;
66 this.color = Config.Get(sensor.Identifier + "/traycolor", Color.Black);
68 ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
69 ToolStripMenuItem removeItem = new ToolStripMenuItem("Remove");
70 removeItem.Click += delegate(object obj, EventArgs args) {
71 sensorSystemTray.Remove(this.sensor);
73 contextMenuStrip.Items.Add(removeItem);
74 ToolStripMenuItem colorItem = new ToolStripMenuItem("Change Color...");
75 colorItem.Click += delegate(object obj, EventArgs args) {
76 ColorDialog dialog = new ColorDialog();
77 dialog.Color = this.color;
78 if (dialog.ShowDialog() == DialogResult.OK) {
79 this.color = dialog.Color;
80 Config.Set(sensor.Identifier + "/traycolor", this.color);
83 contextMenuStrip.Items.Add(colorItem);
84 this.notifyIcon.ContextMenuStrip = contextMenuStrip;
86 this.bitmap = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
87 this.graphics = Graphics.FromImage(this.bitmap);
88 this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
89 this.graphics.SmoothingMode = SmoothingMode.HighQuality;
92 public ISensor Sensor {
93 get { return sensor; }
98 set { color = value; }
101 public void Dispose() {
102 Icon icon = notifyIcon.Icon;
103 notifyIcon.Icon = null;
106 notifyIcon.Dispose();
115 private string GetString() {
116 switch (sensor.SensorType) {
117 case SensorType.Voltage:
118 return string.Format("{0:F11}", sensor.Value);
119 case SensorType.Clock:
120 return string.Format("{0:F11}", 1e-3f * sensor.Value);
121 case SensorType.Load:
122 return string.Format("{0:F0}", sensor.Value < 99 ? sensor.Value : 99);
123 case SensorType.Temperature:
124 return string.Format("{0:F0}", sensor.Value);
126 return string.Format("{0:F11}", 1e-3f * sensor.Value);
131 private Icon CreateSimpleIcon() {
133 graphics.Clear(SystemColors.ButtonFace);
134 TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
135 new Point(-2, 0), color, SystemColors.ButtonFace);
137 BitmapData data = bitmap.LockBits(
138 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
139 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
141 int stride = data.Stride;
142 IntPtr Scan0 = data.Scan0;
144 int numBytes = bitmap.Width * bitmap.Height * 4;
145 byte[] bytes = new byte[numBytes];
146 Marshal.Copy(Scan0, bytes, 0, numBytes);
147 bitmap.UnlockBits(data);
149 return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
152 private Icon CreateTransparentIcon() {
154 graphics.Clear(Color.Black);
155 TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
156 new Point(-2, 0), Color.White, Color.Black);
158 BitmapData data = bitmap.LockBits(
159 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
160 ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
162 int stride = data.Stride;
163 IntPtr Scan0 = data.Scan0;
165 int numBytes = bitmap.Width * bitmap.Height * 4;
166 byte[] bytes = new byte[numBytes];
167 Marshal.Copy(Scan0, bytes, 0, numBytes);
168 bitmap.UnlockBits(data);
170 byte red, green, blue;
171 for (int i = 0; i < bytes.Length; i += 4) {
173 green = bytes[i + 1];
177 bytes[i + 1] = color.G;
178 bytes[i + 2] = color.R;
179 bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
182 return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
185 public void Update() {
186 Icon icon = notifyIcon.Icon;
188 if (majorVersion < 6) {
189 notifyIcon.Icon = CreateSimpleIcon();
191 notifyIcon.Icon = CreateTransparentIcon();
198 switch (sensor.SensorType) {
199 case SensorType.Voltage: format = "{0}\n{1}: {2:F2} V"; break;
200 case SensorType.Clock: format = "{0}\n{1}: {2:F0} MHz"; break;
201 case SensorType.Load: format = "{0}\n{1}: {2:F1} %"; break;
202 case SensorType.Temperature: format = "{0}\n{1}: {2:F1} °C"; break;
203 case SensorType.Fan: format = "{0}\n{1}: {2:F0} RPM"; break;
206 notifyIcon.Text = string.Format(format, sensor.Hardware.Name, sensor.Name,
208 notifyIcon.Visible = true;