GUI/SensorNotifyIcon.cs
author moel.mich
Sun, 14 Feb 2010 20:16:30 +0000
changeset 44 c150de283ca0
parent 43 5b7398d061b7
child 57 142907c75be4
permissions -rw-r--r--
Added core and bus clock support for Intel CPUs (Core 2).
     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 Color color;
    58     private Color darkColor;
    59     private Brush brush;
    60     private Brush darkBrush;
    61     private Pen pen;
    62 
    63     public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor,
    64       bool balloonTip) 
    65     {
    66       this.sensor = sensor;
    67       this.notifyIcon = new NotifyIcon();
    68 
    69       Color defaultColor = Color.Black;
    70       if (sensor.SensorType == SensorType.Load) {
    71         defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
    72       }
    73       Color = Config.Get(sensor.Identifier + "/traycolor", defaultColor);      
    74       
    75       this.pen = new Pen(Color.FromArgb(96, Color.Black));
    76 
    77       ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
    78       ToolStripMenuItem removeItem = new ToolStripMenuItem("Remove");
    79       removeItem.Click += delegate(object obj, EventArgs args) {
    80         sensorSystemTray.Remove(this.sensor);
    81       };
    82       contextMenuStrip.Items.Add(removeItem);
    83       ToolStripMenuItem colorItem = new ToolStripMenuItem("Change Color...");
    84       colorItem.Click += delegate(object obj, EventArgs args) {
    85         ColorDialog dialog = new ColorDialog();
    86         dialog.Color = Color;
    87         if (dialog.ShowDialog() == DialogResult.OK) {
    88           Color = dialog.Color;
    89           Config.Set(sensor.Identifier + "/traycolor", Color);
    90         }
    91       };
    92       contextMenuStrip.Items.Add(colorItem);
    93       this.notifyIcon.ContextMenuStrip = contextMenuStrip;
    94 
    95       this.bitmap = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
    96       this.graphics = Graphics.FromImage(this.bitmap);
    97       this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    98       this.graphics.SmoothingMode = SmoothingMode.HighQuality;
    99     }
   100 
   101     public ISensor Sensor {
   102       get { return sensor; }
   103     }
   104 
   105     public Color Color {
   106       get { return color; }
   107       set { 
   108         this.color = value;
   109         this.darkColor = Color.FromArgb(255,
   110           this.color.R / 3,
   111           this.color.G / 3,
   112           this.color.B / 3);
   113         Brush brush = this.brush;
   114         this.brush = new SolidBrush(this.color);
   115         if (brush != null)
   116           brush.Dispose();
   117         Brush darkBrush = this.darkBrush;
   118         this.darkBrush = new SolidBrush(this.darkColor);
   119         if (darkBrush != null)
   120           darkBrush.Dispose();
   121       }
   122     }
   123 
   124     public void Dispose() {      
   125       Icon icon = notifyIcon.Icon;
   126       notifyIcon.Icon = null;
   127       if (icon != null)
   128         icon.Dispose();      
   129       notifyIcon.Dispose();
   130       notifyIcon = null;
   131 
   132       if (brush != null)
   133         brush.Dispose();
   134       if (darkBrush != null)
   135         darkBrush.Dispose();
   136       pen.Dispose();
   137       graphics.Dispose();
   138       graphics = null;
   139       bitmap.Dispose();
   140       bitmap = null;
   141     }
   142 
   143     private string GetString() {
   144       switch (sensor.SensorType) {
   145         case SensorType.Voltage:
   146           return string.Format("{0:F11}", sensor.Value);
   147         case SensorType.Clock:
   148           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   149         case SensorType.Load: 
   150           return string.Format("{0:F0}", sensor.Value < 99 ? sensor.Value : 99);
   151         case SensorType.Temperature: 
   152           return string.Format("{0:F0}", sensor.Value);
   153         case SensorType.Fan: 
   154           return string.Format("{0:F11}", 1e-3f * sensor.Value);
   155       }
   156       return "-";
   157     }
   158 
   159     private Icon CreateTransparentIcon() {
   160 
   161       graphics.Clear(Color.Black);
   162       TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
   163         new Point(-2, 0), Color.White, Color.Black);
   164 
   165       BitmapData data = bitmap.LockBits(
   166         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   167         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   168 
   169       int stride = data.Stride;
   170       IntPtr Scan0 = data.Scan0;
   171 
   172       int numBytes = bitmap.Width * bitmap.Height * 4;
   173       byte[] bytes = new byte[numBytes];
   174       Marshal.Copy(Scan0, bytes, 0, numBytes);
   175       bitmap.UnlockBits(data);
   176 
   177       byte red, green, blue;
   178       for (int i = 0; i < bytes.Length; i += 4) {
   179         blue = bytes[i];
   180         green = bytes[i + 1];
   181         red = bytes[i + 2];
   182 
   183         bytes[i] = color.B;
   184         bytes[i + 1] = color.G;
   185         bytes[i + 2] = color.R;
   186         bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
   187       }
   188 
   189       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   190     }
   191 
   192     private Icon CreateLoadIcon() {
   193       graphics.Clear(Color.Transparent);
   194       graphics.FillRectangle(darkBrush, 0.5f, -0.5f, 14, 16);
   195       float y = 0.16f * (100 - sensor.Value.Value);
   196       graphics.FillRectangle(brush, 0.5f, -0.5f + y, 14, 16 - y);
   197       graphics.DrawRectangle(pen, 1, 0, 13, 15);
   198 
   199       BitmapData data = bitmap.LockBits(
   200         new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   201         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   202       byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
   203       Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
   204       bitmap.UnlockBits(data);
   205 
   206       return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   207     }
   208 
   209     public void Update() {
   210       Icon icon = notifyIcon.Icon;
   211 
   212       if (sensor.SensorType == SensorType.Load) {
   213         notifyIcon.Icon = CreateLoadIcon();
   214       } else {
   215         notifyIcon.Icon = CreateTransparentIcon();
   216       }
   217       if (icon != null) 
   218         icon.Dispose();
   219 
   220       string format = "";
   221       switch (sensor.SensorType) {
   222         case SensorType.Voltage: format = "{0}\n{1}: {2:F2} V"; break;
   223         case SensorType.Clock: format = "{0}\n{1}: {2:F0} MHz"; break;
   224         case SensorType.Load: format = "{0}\n{1}: {2:F1} %"; break;
   225         case SensorType.Temperature: format = "{0}\n{1}: {2:F1} °C"; break;
   226         case SensorType.Fan: format = "{0}\n{1}: {2:F0} RPM"; break;
   227       }
   228 
   229       notifyIcon.Text = string.Format(format, sensor.Hardware.Name, sensor.Name,
   230         sensor.Value);    
   231       notifyIcon.Visible = true;         
   232     }
   233   }
   234 }