New graphical system tray display for load sensors.
authormoel.mich
Sat, 13 Feb 2010 17:08:36 +0000
changeset 435b7398d061b7
parent 42 47385d4fc990
child 44 c150de283ca0
New graphical system tray display for load sensors.
GUI/SensorNotifyIcon.cs
     1.1 --- a/GUI/SensorNotifyIcon.cs	Fri Feb 12 22:46:31 2010 +0000
     1.2 +++ b/GUI/SensorNotifyIcon.cs	Sat Feb 13 17:08:36 2010 +0000
     1.3 @@ -54,17 +54,26 @@
     1.4      private NotifyIcon notifyIcon;
     1.5      private Bitmap bitmap;
     1.6      private Graphics graphics;
     1.7 -    private int majorVersion;
     1.8      private Color color;
     1.9 +    private Color darkColor;
    1.10 +    private Brush brush;
    1.11 +    private Brush darkBrush;
    1.12 +    private Pen pen;
    1.13  
    1.14      public SensorNotifyIcon(SensorSystemTray sensorSystemTray, ISensor sensor,
    1.15        bool balloonTip) 
    1.16      {
    1.17        this.sensor = sensor;
    1.18        this.notifyIcon = new NotifyIcon();
    1.19 -      this.majorVersion = Environment.OSVersion.Version.Major;
    1.20 -      this.color = Config.Get(sensor.Identifier + "/traycolor", Color.Black);
    1.21 +
    1.22 +      Color defaultColor = Color.Black;
    1.23 +      if (sensor.SensorType == SensorType.Load) {
    1.24 +        defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
    1.25 +      }
    1.26 +      Color = Config.Get(sensor.Identifier + "/traycolor", defaultColor);      
    1.27        
    1.28 +      this.pen = new Pen(Color.FromArgb(96, Color.Black));
    1.29 +
    1.30        ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
    1.31        ToolStripMenuItem removeItem = new ToolStripMenuItem("Remove");
    1.32        removeItem.Click += delegate(object obj, EventArgs args) {
    1.33 @@ -74,10 +83,10 @@
    1.34        ToolStripMenuItem colorItem = new ToolStripMenuItem("Change Color...");
    1.35        colorItem.Click += delegate(object obj, EventArgs args) {
    1.36          ColorDialog dialog = new ColorDialog();
    1.37 -        dialog.Color = this.color;
    1.38 +        dialog.Color = Color;
    1.39          if (dialog.ShowDialog() == DialogResult.OK) {
    1.40 -          this.color = dialog.Color;
    1.41 -          Config.Set(sensor.Identifier + "/traycolor", this.color);
    1.42 +          Color = dialog.Color;
    1.43 +          Config.Set(sensor.Identifier + "/traycolor", Color);
    1.44          }
    1.45        };
    1.46        contextMenuStrip.Items.Add(colorItem);
    1.47 @@ -95,7 +104,21 @@
    1.48  
    1.49      public Color Color {
    1.50        get { return color; }
    1.51 -      set { color = value; }
    1.52 +      set { 
    1.53 +        this.color = value;
    1.54 +        this.darkColor = Color.FromArgb(255,
    1.55 +          Math.Max(this.color.R - 100, 0),
    1.56 +          Math.Max(this.color.G - 100, 0),
    1.57 +          Math.Max(this.color.B - 100, 0));
    1.58 +        Brush brush = this.brush;
    1.59 +        this.brush = new SolidBrush(this.color);
    1.60 +        if (brush != null)
    1.61 +          brush.Dispose();
    1.62 +        Brush darkBrush = this.darkBrush;
    1.63 +        this.darkBrush = new SolidBrush(this.darkColor);
    1.64 +        if (darkBrush != null)
    1.65 +          darkBrush.Dispose();
    1.66 +      }
    1.67      }
    1.68  
    1.69      public void Dispose() {      
    1.70 @@ -106,6 +129,11 @@
    1.71        notifyIcon.Dispose();
    1.72        notifyIcon = null;
    1.73  
    1.74 +      if (brush != null)
    1.75 +        brush.Dispose();
    1.76 +      if (darkBrush != null)
    1.77 +        darkBrush.Dispose();
    1.78 +      pen.Dispose();
    1.79        graphics.Dispose();
    1.80        graphics = null;
    1.81        bitmap.Dispose();
    1.82 @@ -128,27 +156,6 @@
    1.83        return "-";
    1.84      }
    1.85  
    1.86 -    private Icon CreateSimpleIcon() {
    1.87 -
    1.88 -      graphics.Clear(SystemColors.ButtonFace);
    1.89 -      TextRenderer.DrawText(graphics, GetString(), SystemFonts.StatusFont,
    1.90 -        new Point(-2, 0), color, SystemColors.ButtonFace);
    1.91 -
    1.92 -      BitmapData data = bitmap.LockBits(
    1.93 -        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
    1.94 -        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    1.95 -
    1.96 -      int stride = data.Stride;
    1.97 -      IntPtr Scan0 = data.Scan0;
    1.98 -
    1.99 -      int numBytes = bitmap.Width * bitmap.Height * 4;
   1.100 -      byte[] bytes = new byte[numBytes];
   1.101 -      Marshal.Copy(Scan0, bytes, 0, numBytes);
   1.102 -      bitmap.UnlockBits(data);
   1.103 -
   1.104 -      return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   1.105 -    }
   1.106 -
   1.107      private Icon CreateTransparentIcon() {
   1.108  
   1.109        graphics.Clear(Color.Black);
   1.110 @@ -182,15 +189,31 @@
   1.111        return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   1.112      }
   1.113  
   1.114 +    private Icon CreateLoadIcon() {
   1.115 +      graphics.Clear(Color.Transparent);
   1.116 +      graphics.FillRectangle(darkBrush, 0.5f, -0.5f, 14, 16);
   1.117 +      float y = 0.16f * (100 - sensor.Value.Value);
   1.118 +      graphics.FillRectangle(brush, 0.5f, -0.5f + y, 14, 16 - y);
   1.119 +      graphics.DrawRectangle(pen, 1, 0, 13, 15);
   1.120 +
   1.121 +      BitmapData data = bitmap.LockBits(
   1.122 +        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   1.123 +        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   1.124 +      byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
   1.125 +      Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
   1.126 +      bitmap.UnlockBits(data);
   1.127 +
   1.128 +      return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
   1.129 +    }
   1.130 +
   1.131      public void Update() {
   1.132        Icon icon = notifyIcon.Icon;
   1.133  
   1.134 -      if (majorVersion < 6) {
   1.135 -        notifyIcon.Icon = CreateSimpleIcon();
   1.136 +      if (sensor.SensorType == SensorType.Load) {
   1.137 +        notifyIcon.Icon = CreateLoadIcon();
   1.138        } else {
   1.139          notifyIcon.Icon = CreateTransparentIcon();
   1.140        }
   1.141 -      
   1.142        if (icon != null) 
   1.143          icon.Dispose();
   1.144