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