GUI/SensorNotifyIcon.cs
author paulwerelds
Tue, 05 Oct 2010 19:34:59 +0000
changeset 213 d93c927e4fd6
parent 176 c16fd81b520a
child 217 d93ddd6ca0af
permissions -rw-r--r--
Fixed Issue 123.
     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.Drawing;
    40 using System.Drawing.Drawing2D;
    41 using System.Drawing.Imaging;
    42 using System.Drawing.Text;
    43 using System.Runtime.InteropServices;
    44 using System.Windows.Forms;
    45 using OpenHardwareMonitor.Hardware;
    46 using OpenHardwareMonitor.Utilities;
    47 
    48 namespace OpenHardwareMonitor.GUI {
    49   public class SensorNotifyIcon : IDisposable {
    50 
    51     private ISensor sensor;
    52     private NotifyIcon notifyIcon;
    53     private Bitmap bitmap;
    54     private Graphics graphics;
    55     private Color color;
    56     private Color darkColor;
    57     private Brush brush;
    58     private Brush darkBrush;
    59     private Pen pen;
    60     private Font font;
    61 
    62     public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
    63       bool balloonTip, PersistentSettings settings) 
    64     {
    65       this.sensor = sensor;
    66       this.notifyIcon = new NotifyIcon();
    67 
    68       Color defaultColor = Color.Black;
    69       if (sensor.SensorType == SensorType.Load) {
    70         defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
    71       }
    72       Color = settings.GetValue(new Identifier(sensor.Identifier, 
    73         "traycolor").ToString(), defaultColor);      
    74       
    75       this.pen = new Pen(Color.FromArgb(96, Color.Black));
    76       this.font = SystemFonts.MessageBoxFont;
    77 
    78       ContextMenu contextMenu = new ContextMenu();
    79       MenuItem hideShowItem = new MenuItem("Hide/Show");
    80       hideShowItem.Click += delegate(object obj, EventArgs args) {
    81         sensorSystemTray.SendHideShowCommand();
    82       };
    83       contextMenu.MenuItems.Add(hideShowItem);
    84       contextMenu.MenuItems.Add(new MenuItem("-"));
    85       MenuItem removeItem = new MenuItem("Remove Sensor");
    86       removeItem.Click += delegate(object obj, EventArgs args) {
    87         sensorSystemTray.Remove(this.sensor);
    88       };
    89       contextMenu.MenuItems.Add(removeItem);
    90       MenuItem colorItem = new MenuItem("Change Color...");
    91       colorItem.Click += delegate(object obj, EventArgs args) {
    92         ColorDialog dialog = new ColorDialog();
    93         dialog.Color = Color;
    94         if (dialog.ShowDialog() == DialogResult.OK) {
    95           Color = dialog.Color;
    96           settings.SetValue(new Identifier(sensor.Identifier,
    97             "traycolor").ToString(), Color);
    98         }
    99       };
   100       contextMenu.MenuItems.Add(colorItem);
   101       contextMenu.MenuItems.Add(new MenuItem("-"));
   102       MenuItem exitItem = new MenuItem("Exit");
   103       exitItem.Click += delegate(object obj, EventArgs args) {
   104         sensorSystemTray.SendExitCommand();
   105       };
   106       contextMenu.MenuItems.Add(exitItem);
   107       this.notifyIcon.ContextMenu = contextMenu;
   108       this.notifyIcon.DoubleClick += delegate(object obj, EventArgs args) {
   109         sensorSystemTray.SendHideShowCommand();
   110       };
   111 
   112       this.bitmap = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
   113       this.graphics = Graphics.FromImage(this.bitmap);
   114 
   115       if (Environment.OSVersion.Version.Major > 5) {
   116         this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
   117         this.graphics.SmoothingMode = SmoothingMode.HighQuality;
   118       }
   119     }
   120 
   121     public ISensor Sensor {
   122       get { return sensor; }
   123     }
   124 
   125     public Color Color {
   126       get { return color; }
   127       set { 
   128         this.color = value;
   129         this.darkColor = Color.FromArgb(255,
   130           this.color.R / 3,
   131           this.color.G / 3,
   132           this.color.B / 3);
   133         Brush brush = this.brush;
   134         this.brush = new SolidBrush(this.color);
   135         if (brush != null)
   136           brush.Dispose();
   137         Brush darkBrush = this.darkBrush;
   138         this.darkBrush = new SolidBrush(this.darkColor);
   139         if (darkBrush != null)
   140           darkBrush.Dispose();
   141       }
   142     }
   143 
   144     public void Dispose() {      
   145       Icon icon = notifyIcon.Icon;
   146       notifyIcon.Icon = null;
   147       if (icon != null)
   148         icon.Dispose();      
   149       notifyIcon.Dispose();
   150 
   151       if (brush != null)
   152         brush.Dispose();
   153       if (darkBrush != null)
   154         darkBrush.Dispose();
   155       pen.Dispose();
   156       graphics.Dispose();      
   157       bitmap.Dispose();      
   158     }
   159 
   160     private string GetString() {
   161       switch (sensor.SensorType) {
   162         case SensorType.Voltage:
   163           return string.Format("{0:F11}", sensor.Value);
   164         case SensorType.Clock:
   165           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   166         case SensorType.Load: 
   167           return string.Format("{0:F0}", sensor.Value);
   168         case SensorType.Temperature: 
   169           return string.Format("{0:F0}", sensor.Value);
   170         case SensorType.Fan: 
   171           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   172         case SensorType.Flow:
   173           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   174         case SensorType.Control:
   175           return string.Format("{0:F0}", sensor.Value);
   176       }
   177       return "-";
   178     }
   179 
   180     private Icon CreateTransparentIcon() {
   181 
   182       graphics.Clear(Color.Black);
   183       TextRenderer.DrawText(graphics, GetString(), font,
   184         new Point(-2, 0), Color.White, Color.Black);        
   185 
   186       BitmapData data = bitmap.LockBits(
   187         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   188         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   189 
   190       IntPtr Scan0 = data.Scan0;
   191 
   192       int numBytes = bitmap.Width * bitmap.Height * 4;
   193       byte[] bytes = new byte[numBytes];
   194       Marshal.Copy(Scan0, bytes, 0, numBytes);
   195       bitmap.UnlockBits(data);
   196 
   197       byte red, green, blue;
   198       for (int i = 0; i < bytes.Length; i += 4) {
   199         blue = bytes[i];
   200         green = bytes[i + 1];
   201         red = bytes[i + 2];
   202 
   203         bytes[i] = color.B;
   204         bytes[i + 1] = color.G;
   205         bytes[i + 2] = color.R;
   206         bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
   207       }
   208 
   209       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   210     }
   211 
   212     private Icon CreateLoadIcon() {      
   213       try {
   214         graphics.Clear(Color.Transparent);
   215       } catch (ArgumentException) {
   216         graphics.Clear(Color.Black);
   217       }
   218       graphics.FillRectangle(darkBrush, 0.5f, -0.5f, 14, 16);
   219       float y = 0.16f * (100 - sensor.Value.Value);
   220       graphics.FillRectangle(brush, 0.5f, -0.5f + y, 14, 16 - y);
   221       graphics.DrawRectangle(pen, 1, 0, 13, 15);
   222 
   223       BitmapData data = bitmap.LockBits(
   224         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   225         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   226       byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
   227       Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
   228       bitmap.UnlockBits(data);
   229 
   230       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   231     }
   232 
   233     public void Update() {
   234       Icon icon = notifyIcon.Icon;
   235 
   236       if (sensor.SensorType == SensorType.Load) {
   237         notifyIcon.Icon = CreateLoadIcon();
   238       } else {
   239         notifyIcon.Icon = CreateTransparentIcon();
   240       }
   241       if (icon != null) 
   242         icon.Dispose();
   243 
   244       string format = "";
   245       switch (sensor.SensorType) {
   246         case SensorType.Voltage: format = "\n{0}: {1:F2} V"; break;
   247         case SensorType.Clock: format = "\n{0}: {1:F0} MHz"; break;
   248         case SensorType.Load: format = "\n{0}: {1:F1} %"; break;
   249         case SensorType.Temperature: format = "\n{0}: {1:F1} °C"; break;
   250         case SensorType.Fan: format = "\n{0}: {1:F0} RPM"; break;
   251         case SensorType.Flow: format = "\n{0}: {1:F0} L/h"; break;
   252         case SensorType.Control: format = "\n{0}: {1:F1} %"; break;
   253       }
   254       string formattedValue = string.Format(format, sensor.Name, sensor.Value);
   255       string hardwareName = sensor.Hardware.Name;
   256       hardwareName = hardwareName.Substring(0, 
   257         Math.Min(63 - formattedValue.Length, hardwareName.Length));
   258       string text = hardwareName + formattedValue;
   259       if (text.Length > 63)
   260         text = null;
   261 
   262       notifyIcon.Text = text;
   263       notifyIcon.Visible = true;         
   264     }
   265   }
   266 }