GUI/SensorNotifyIcon.cs
author moel.mich
Sun, 24 Jun 2012 16:16:46 +0000
changeset 354 e71b968c04f9
parent 340 600962f8a298
child 362 1dfe9dac1651
permissions -rw-r--r--
Fixed Issue 300.
moel@202
     1
/*
moel@40
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@40
     6
 
moel@344
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@40
     9
*/
moel@40
    10
moel@40
    11
using System;
moel@40
    12
using System.Drawing;
moel@40
    13
using System.Drawing.Drawing2D;
moel@40
    14
using System.Drawing.Imaging;
moel@40
    15
using System.Drawing.Text;
moel@176
    16
using System.Runtime.InteropServices;
moel@40
    17
using System.Windows.Forms;
moel@40
    18
using OpenHardwareMonitor.Hardware;
moel@40
    19
using OpenHardwareMonitor.Utilities;
moel@40
    20
moel@40
    21
namespace OpenHardwareMonitor.GUI {
moel@40
    22
  public class SensorNotifyIcon : IDisposable {
moel@40
    23
moel@40
    24
    private ISensor sensor;
moel@40
    25
    private NotifyIcon notifyIcon;
moel@40
    26
    private Bitmap bitmap;
moel@40
    27
    private Graphics graphics;
moel@42
    28
    private Color color;
moel@43
    29
    private Color darkColor;
moel@43
    30
    private Brush brush;
moel@43
    31
    private Brush darkBrush;
moel@43
    32
    private Pen pen;
moel@70
    33
    private Font font;
moel@40
    34
moel@133
    35
    public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
moel@165
    36
      bool balloonTip, PersistentSettings settings) 
moel@42
    37
    {
moel@40
    38
      this.sensor = sensor;
moel@40
    39
      this.notifyIcon = new NotifyIcon();
moel@43
    40
moel@43
    41
      Color defaultColor = Color.Black;
moel@217
    42
      if (sensor.SensorType == SensorType.Load ||
moel@217
    43
          sensor.SensorType == SensorType.Control ||
moel@217
    44
          sensor.SensorType == SensorType.Level) 
moel@217
    45
      {
moel@43
    46
        defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
moel@43
    47
      }
moel@166
    48
      Color = settings.GetValue(new Identifier(sensor.Identifier, 
moel@109
    49
        "traycolor").ToString(), defaultColor);      
moel@40
    50
      
moel@43
    51
      this.pen = new Pen(Color.FromArgb(96, Color.Black));
moel@117
    52
      this.font = SystemFonts.MessageBoxFont;
moel@43
    53
moel@156
    54
      ContextMenu contextMenu = new ContextMenu();
moel@156
    55
      MenuItem hideShowItem = new MenuItem("Hide/Show");
moel@133
    56
      hideShowItem.Click += delegate(object obj, EventArgs args) {
moel@133
    57
        sensorSystemTray.SendHideShowCommand();
moel@133
    58
      };
moel@156
    59
      contextMenu.MenuItems.Add(hideShowItem);
moel@156
    60
      contextMenu.MenuItems.Add(new MenuItem("-"));
moel@156
    61
      MenuItem removeItem = new MenuItem("Remove Sensor");
moel@42
    62
      removeItem.Click += delegate(object obj, EventArgs args) {
moel@42
    63
        sensorSystemTray.Remove(this.sensor);
moel@40
    64
      };
moel@156
    65
      contextMenu.MenuItems.Add(removeItem);
moel@156
    66
      MenuItem colorItem = new MenuItem("Change Color...");
moel@42
    67
      colorItem.Click += delegate(object obj, EventArgs args) {
moel@42
    68
        ColorDialog dialog = new ColorDialog();
moel@43
    69
        dialog.Color = Color;
moel@42
    70
        if (dialog.ShowDialog() == DialogResult.OK) {
moel@43
    71
          Color = dialog.Color;
moel@166
    72
          settings.SetValue(new Identifier(sensor.Identifier,
moel@109
    73
            "traycolor").ToString(), Color);
moel@42
    74
        }
moel@42
    75
      };
moel@156
    76
      contextMenu.MenuItems.Add(colorItem);
moel@156
    77
      contextMenu.MenuItems.Add(new MenuItem("-"));
moel@156
    78
      MenuItem exitItem = new MenuItem("Exit");
moel@133
    79
      exitItem.Click += delegate(object obj, EventArgs args) {
moel@133
    80
        sensorSystemTray.SendExitCommand();
moel@133
    81
      };
moel@156
    82
      contextMenu.MenuItems.Add(exitItem);
moel@156
    83
      this.notifyIcon.ContextMenu = contextMenu;
moel@133
    84
      this.notifyIcon.DoubleClick += delegate(object obj, EventArgs args) {
moel@133
    85
        sensorSystemTray.SendHideShowCommand();
moel@133
    86
      };
moel@40
    87
moel@252
    88
      // get the default dpi to create an icon with the correct size
moel@252
    89
      float dpiX, dpiY;
moel@252
    90
      using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb)) {
moel@252
    91
        dpiX = b.HorizontalResolution;
moel@252
    92
        dpiY = b.VerticalResolution;
moel@252
    93
      }
moel@252
    94
moel@252
    95
      // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi) 
moel@252
    96
      int width = (int)Math.Round(16 * dpiX / 96);
moel@252
    97
      int height = (int)Math.Round(16 * dpiY / 96);
moel@252
    98
moel@252
    99
      // make sure it does never get smaller than 16x16
moel@252
   100
      width = width < 16 ? 16: width;
moel@252
   101
      height = height < 16 ? 16: height;
moel@252
   102
moel@252
   103
      this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);      
moel@40
   104
      this.graphics = Graphics.FromImage(this.bitmap);
moel@117
   105
moel@117
   106
      if (Environment.OSVersion.Version.Major > 5) {
moel@117
   107
        this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
moel@117
   108
        this.graphics.SmoothingMode = SmoothingMode.HighQuality;
moel@117
   109
      }
moel@40
   110
    }
moel@40
   111
moel@40
   112
    public ISensor Sensor {
moel@40
   113
      get { return sensor; }
moel@40
   114
    }
moel@40
   115
moel@42
   116
    public Color Color {
moel@42
   117
      get { return color; }
moel@43
   118
      set { 
moel@43
   119
        this.color = value;
moel@43
   120
        this.darkColor = Color.FromArgb(255,
moel@44
   121
          this.color.R / 3,
moel@44
   122
          this.color.G / 3,
moel@44
   123
          this.color.B / 3);
moel@43
   124
        Brush brush = this.brush;
moel@43
   125
        this.brush = new SolidBrush(this.color);
moel@43
   126
        if (brush != null)
moel@43
   127
          brush.Dispose();
moel@43
   128
        Brush darkBrush = this.darkBrush;
moel@43
   129
        this.darkBrush = new SolidBrush(this.darkColor);
moel@43
   130
        if (darkBrush != null)
moel@43
   131
          darkBrush.Dispose();
moel@43
   132
      }
moel@42
   133
    }
moel@42
   134
moel@40
   135
    public void Dispose() {      
moel@40
   136
      Icon icon = notifyIcon.Icon;
moel@40
   137
      notifyIcon.Icon = null;
moel@40
   138
      if (icon != null)
moel@40
   139
        icon.Dispose();      
moel@40
   140
      notifyIcon.Dispose();
moel@40
   141
moel@43
   142
      if (brush != null)
moel@43
   143
        brush.Dispose();
moel@43
   144
      if (darkBrush != null)
moel@43
   145
        darkBrush.Dispose();
moel@43
   146
      pen.Dispose();
moel@85
   147
      graphics.Dispose();      
moel@85
   148
      bitmap.Dispose();      
moel@40
   149
    }
moel@40
   150
moel@40
   151
    private string GetString() {
moel@282
   152
      if (!sensor.Value.HasValue)
moel@282
   153
        return "-";
moel@282
   154
moel@40
   155
      switch (sensor.SensorType) {
moel@40
   156
        case SensorType.Voltage:
moel@340
   157
          return string.Format("{0:F1}", sensor.Value);
moel@40
   158
        case SensorType.Clock:
moel@340
   159
          return string.Format("{0:F1}", 1e-3f * sensor.Value);
moel@40
   160
        case SensorType.Load: 
moel@118
   161
          return string.Format("{0:F0}", sensor.Value);
moel@40
   162
        case SensorType.Temperature: 
moel@40
   163
          return string.Format("{0:F0}", sensor.Value);
moel@40
   164
        case SensorType.Fan: 
moel@340
   165
          return string.Format("{0:F1}", 1e-3f * sensor.Value);
moel@57
   166
        case SensorType.Flow:
moel@340
   167
          return string.Format("{0:F1}", 1e-3f * sensor.Value);
moel@118
   168
        case SensorType.Control:
moel@118
   169
          return string.Format("{0:F0}", sensor.Value);
moel@217
   170
        case SensorType.Level:
moel@217
   171
          return string.Format("{0:F0}", sensor.Value);
moel@317
   172
        case SensorType.Power:
moel@317
   173
          return string.Format("{0:F0}", sensor.Value);
moel@324
   174
        case SensorType.Data:
moel@324
   175
          return string.Format("{0:F0}", sensor.Value);
moel@340
   176
        case SensorType.Factor:
moel@340
   177
          return string.Format("{0:F1}", sensor.Value);
moel@40
   178
      }
moel@40
   179
      return "-";
moel@40
   180
    }
moel@40
   181
moel@40
   182
    private Icon CreateTransparentIcon() {
moel@40
   183
moel@40
   184
      graphics.Clear(Color.Black);
moel@70
   185
      TextRenderer.DrawText(graphics, GetString(), font,
moel@117
   186
        new Point(-2, 0), Color.White, Color.Black);        
moel@40
   187
moel@40
   188
      BitmapData data = bitmap.LockBits(
moel@40
   189
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
moel@40
   190
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
moel@40
   191
moel@40
   192
      IntPtr Scan0 = data.Scan0;
moel@40
   193
moel@40
   194
      int numBytes = bitmap.Width * bitmap.Height * 4;
moel@40
   195
      byte[] bytes = new byte[numBytes];
moel@40
   196
      Marshal.Copy(Scan0, bytes, 0, numBytes);
moel@40
   197
      bitmap.UnlockBits(data);
moel@40
   198
moel@40
   199
      byte red, green, blue;
moel@40
   200
      for (int i = 0; i < bytes.Length; i += 4) {
moel@40
   201
        blue = bytes[i];
moel@40
   202
        green = bytes[i + 1];
moel@40
   203
        red = bytes[i + 2];
moel@40
   204
moel@42
   205
        bytes[i] = color.B;
moel@42
   206
        bytes[i + 1] = color.G;
moel@42
   207
        bytes[i + 2] = color.R;
moel@40
   208
        bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
moel@40
   209
      }
moel@40
   210
moel@252
   211
      return IconFactory.Create(bytes, bitmap.Width, bitmap.Height, 
moel@252
   212
        PixelFormat.Format32bppArgb);
moel@40
   213
    }
moel@40
   214
moel@217
   215
    private Icon CreatePercentageIcon() {      
moel@86
   216
      try {
moel@86
   217
        graphics.Clear(Color.Transparent);
moel@86
   218
      } catch (ArgumentException) {
moel@86
   219
        graphics.Clear(Color.Black);
moel@86
   220
      }
moel@252
   221
      graphics.FillRectangle(darkBrush, 0.5f, -0.5f, bitmap.Width - 2, bitmap.Height);
moel@282
   222
      float value = sensor.Value.GetValueOrDefault();
moel@282
   223
      float y = 0.16f * (100 - value);
moel@252
   224
      graphics.FillRectangle(brush, 0.5f, -0.5f + y, bitmap.Width - 2, bitmap.Height - y);
moel@252
   225
      graphics.DrawRectangle(pen, 1, 0, bitmap.Width - 3, bitmap.Height - 1);
moel@43
   226
moel@43
   227
      BitmapData data = bitmap.LockBits(
moel@43
   228
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
moel@43
   229
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
moel@43
   230
      byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
moel@43
   231
      Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
moel@43
   232
      bitmap.UnlockBits(data);
moel@43
   233
moel@252
   234
      return IconFactory.Create(bytes, bitmap.Width, bitmap.Height, 
moel@252
   235
        PixelFormat.Format32bppArgb);
moel@43
   236
    }
moel@43
   237
moel@40
   238
    public void Update() {
moel@40
   239
      Icon icon = notifyIcon.Icon;
moel@40
   240
moel@217
   241
      switch (sensor.SensorType) {
moel@217
   242
        case SensorType.Load:
moel@217
   243
        case SensorType.Control:
moel@217
   244
        case SensorType.Level:
moel@217
   245
          notifyIcon.Icon = CreatePercentageIcon();
moel@217
   246
          break;
moel@217
   247
        default:
moel@217
   248
          notifyIcon.Icon = CreateTransparentIcon();
moel@217
   249
          break;
moel@40
   250
      }
moel@217
   251
moel@40
   252
      if (icon != null) 
moel@40
   253
        icon.Dispose();
moel@40
   254
moel@40
   255
      string format = "";
moel@40
   256
      switch (sensor.SensorType) {
moel@116
   257
        case SensorType.Voltage: format = "\n{0}: {1:F2} V"; break;
moel@116
   258
        case SensorType.Clock: format = "\n{0}: {1:F0} MHz"; break;
moel@116
   259
        case SensorType.Load: format = "\n{0}: {1:F1} %"; break;
moel@116
   260
        case SensorType.Temperature: format = "\n{0}: {1:F1} °C"; break;
moel@116
   261
        case SensorType.Fan: format = "\n{0}: {1:F0} RPM"; break;
moel@116
   262
        case SensorType.Flow: format = "\n{0}: {1:F0} L/h"; break;
moel@118
   263
        case SensorType.Control: format = "\n{0}: {1:F1} %"; break;
moel@217
   264
        case SensorType.Level: format = "\n{0}: {1:F1} %"; break;
moel@317
   265
        case SensorType.Power: format = "\n{0}: {1:F0} W"; break;
moel@324
   266
        case SensorType.Data: format = "\n{0}: {1:F0} GB"; break;
moel@340
   267
        case SensorType.Factor: format = "\n{0}: {1:F3} GB"; break;
moel@40
   268
      }
moel@116
   269
      string formattedValue = string.Format(format, sensor.Name, sensor.Value);
moel@116
   270
      string hardwareName = sensor.Hardware.Name;
moel@116
   271
      hardwareName = hardwareName.Substring(0, 
moel@116
   272
        Math.Min(63 - formattedValue.Length, hardwareName.Length));
moel@116
   273
      string text = hardwareName + formattedValue;
moel@116
   274
      if (text.Length > 63)
moel@116
   275
        text = null;
moel@40
   276
moel@116
   277
      notifyIcon.Text = text;
moel@42
   278
      notifyIcon.Visible = true;         
moel@40
   279
    }
moel@40
   280
  }
moel@40
   281
}