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