GUI/SensorNotifyIcon.cs
author moel.mich
Fri, 12 Feb 2010 22:46:31 +0000
changeset 42 47385d4fc990
parent 40 2392f7402fb6
child 43 5b7398d061b7
permissions -rw-r--r--
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.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.Drawing.Drawing2D;
    42 using System.Drawing.Imaging;
    43 using System.Drawing.Text;
    44 using System.Text;
    45 using System.Windows.Forms;
    46 using System.Runtime.InteropServices;
    47 using OpenHardwareMonitor.Hardware;
    48 using OpenHardwareMonitor.Utilities;
    49 
    50 namespace OpenHardwareMonitor.GUI {
    51   public class SensorNotifyIcon : IDisposable {
    52 
    53     private ISensor sensor;
    54     private NotifyIcon notifyIcon;
    55     private Bitmap bitmap;
    56     private Graphics graphics;
    57     private int majorVersion;
    58     private Color color;
    59 
    60     public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor,
    61       bool balloonTip) 
    62     {
    63       this.sensor = sensor;
    64       this.notifyIcon = new NotifyIcon();
    65       this.majorVersion = Environment.OSVersion.Version.Major;
    66       this.color = Config.Get(sensor.Identifier + "/traycolor", Color.Black);
    67       
    68       ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
    69       ToolStripMenuItem removeItem = new ToolStripMenuItem("Remove");
    70       removeItem.Click += delegate(object obj, EventArgs args) {
    71         sensorSystemTray.Remove(this.sensor);
    72       };
    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);
    81         }
    82       };
    83       contextMenuStrip.Items.Add(colorItem);
    84       this.notifyIcon.ContextMenuStrip = contextMenuStrip;
    85 
    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;
    90     }
    91 
    92     public ISensor Sensor {
    93       get { return sensor; }
    94     }
    95 
    96     public Color Color {
    97       get { return color; }
    98       set { color = value; }
    99     }
   100 
   101     public void Dispose() {      
   102       Icon icon = notifyIcon.Icon;
   103       notifyIcon.Icon = null;
   104       if (icon != null)
   105         icon.Dispose();      
   106       notifyIcon.Dispose();
   107       notifyIcon = null;
   108 
   109       graphics.Dispose();
   110       graphics = null;
   111       bitmap.Dispose();
   112       bitmap = null;
   113     }
   114 
   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);
   125         case SensorType.Fan: 
   126           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   127       }
   128       return "-";
   129     }
   130 
   131     private Icon CreateSimpleIcon() {
   132 
   133       graphics.Clear(SystemColors.ButtonFace);
   134       TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
   135         new Point(-2, 0), color, SystemColors.ButtonFace);
   136 
   137       BitmapData data = bitmap.LockBits(
   138         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   139         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   140 
   141       int stride = data.Stride;
   142       IntPtr Scan0 = data.Scan0;
   143 
   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);
   148 
   149       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   150     }
   151 
   152     private Icon CreateTransparentIcon() {
   153 
   154       graphics.Clear(Color.Black);
   155       TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
   156         new Point(-2, 0), Color.White, Color.Black);
   157 
   158       BitmapData data = bitmap.LockBits(
   159         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   160         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   161 
   162       int stride = data.Stride;
   163       IntPtr Scan0 = data.Scan0;
   164 
   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);
   169 
   170       byte red, green, blue;
   171       for (int i = 0; i < bytes.Length; i += 4) {
   172         blue = bytes[i];
   173         green = bytes[i + 1];
   174         red = bytes[i + 2];
   175 
   176         bytes[i] = color.B;
   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);
   180       }
   181 
   182       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   183     }
   184 
   185     public void Update() {
   186       Icon icon = notifyIcon.Icon;
   187 
   188       if (majorVersion < 6) {
   189         notifyIcon.Icon = CreateSimpleIcon();
   190       } else {
   191         notifyIcon.Icon = CreateTransparentIcon();
   192       }
   193       
   194       if (icon != null) 
   195         icon.Dispose();
   196 
   197       string format = "";
   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;
   204       }
   205 
   206       notifyIcon.Text = string.Format(format, sensor.Hardware.Name, sensor.Name,
   207         sensor.Value);    
   208       notifyIcon.Visible = true;         
   209     }
   210   }
   211 }