GUI/SensorNotifyIcon.cs
author moel.mich
Fri, 12 Feb 2010 00:36:56 +0000
changeset 40 2392f7402fb6
child 42 47385d4fc990
permissions -rw-r--r--
First system tray sensor monitor support.
     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 
    59     public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor) {
    60       this.sensor = sensor;
    61       this.notifyIcon = new NotifyIcon();
    62       this.majorVersion = Environment.OSVersion.Version.Major;      
    63       
    64       ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
    65       ToolStripMenuItem item = new ToolStripMenuItem("Remove");
    66       item.Click += delegate(object obj, EventArgs args) {
    67         sensorSystemTray.Remove(sensor);
    68       };
    69       contextMenuStrip.Items.Add(item);
    70       this.notifyIcon.ContextMenuStrip = contextMenuStrip;
    71 
    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;
    76     }
    77 
    78     public ISensor Sensor {
    79       get { return sensor; }
    80     }
    81 
    82     public void Dispose() {      
    83       Icon icon = notifyIcon.Icon;
    84       notifyIcon.Icon = null;
    85       if (icon != null)
    86         icon.Dispose();      
    87       notifyIcon.Dispose();
    88       notifyIcon = null;
    89 
    90       graphics.Dispose();
    91       graphics = null;
    92       bitmap.Dispose();
    93       bitmap = null;
    94     }
    95 
    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);
   106         case SensorType.Fan: 
   107           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   108       }
   109       return "-";
   110     }
   111 
   112     private Icon CreateSimpleIcon() {
   113 
   114       graphics.Clear(SystemColors.ButtonFace);
   115       TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
   116         new Point(-2, 0), Color.Blue, SystemColors.ButtonFace);
   117 
   118       BitmapData data = bitmap.LockBits(
   119         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   120         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   121 
   122       int stride = data.Stride;
   123       IntPtr Scan0 = data.Scan0;
   124 
   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);
   129 
   130       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   131     }
   132 
   133     private Icon CreateTransparentIcon() {
   134 
   135       graphics.Clear(Color.Black);
   136       TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
   137         new Point(-2, 0), Color.White, Color.Black);
   138 
   139       BitmapData data = bitmap.LockBits(
   140         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   141         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   142 
   143       int stride = data.Stride;
   144       IntPtr Scan0 = data.Scan0;
   145 
   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);
   150 
   151       byte red, green, blue;
   152       for (int i = 0; i < bytes.Length; i += 4) {
   153         blue = bytes[i];
   154         green = bytes[i + 1];
   155         red = bytes[i + 2];
   156 
   157         bytes[i] = 255;
   158         bytes[i + 1] = 255;
   159         bytes[i + 2] = 255;
   160         bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
   161       }
   162 
   163       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   164     }
   165 
   166     public void Update() {
   167       Icon icon = notifyIcon.Icon;
   168 
   169       if (majorVersion < 6) {
   170         notifyIcon.Icon = CreateSimpleIcon();
   171       } else {
   172         notifyIcon.Icon = CreateTransparentIcon();
   173       }
   174       
   175       if (icon != null) 
   176         icon.Dispose();
   177 
   178       string format = "";
   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;
   185       }
   186 
   187       notifyIcon.Text = string.Format(format, 
   188         sensor.Hardware.Name, sensor.Name, sensor.Value);
   189       notifyIcon.Visible = true;
   190     }
   191   }
   192 }