GUI/Gadget.cs
author moel.mich
Tue, 21 Sep 2010 20:32:36 +0000
changeset 195 0ee888c485d5
parent 181 9901dbb25f18
child 202 551243a66b32
permissions -rw-r--r--
Refactored some of the hardware monitoring code and fixed a few code inspection warnings.
moel@176
     1
/*
moel@176
     2
  
moel@176
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@176
     4
moel@176
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@176
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@176
     7
  the License. You may obtain a copy of the License at
moel@176
     8
 
moel@176
     9
  http://www.mozilla.org/MPL/
moel@176
    10
moel@176
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@176
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@176
    13
  for the specific language governing rights and limitations under the License.
moel@176
    14
moel@176
    15
  The Original Code is the Open Hardware Monitor code.
moel@176
    16
moel@176
    17
  The Initial Developer of the Original Code is 
moel@176
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@176
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@176
    20
  the Initial Developer. All Rights Reserved.
moel@176
    21
moel@176
    22
  Contributor(s):
moel@176
    23
moel@176
    24
  Alternatively, the contents of this file may be used under the terms of
moel@176
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@176
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@176
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@176
    28
  of those above. If you wish to allow use of your version of this file only
moel@176
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@176
    30
  use your version of this file under the terms of the MPL, indicate your
moel@176
    31
  decision by deleting the provisions above and replace them with the notice
moel@176
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@176
    33
  the provisions above, a recipient may use your version of this file under
moel@176
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@176
    35
 
moel@176
    36
*/
moel@176
    37
moel@176
    38
using System;
moel@176
    39
using System.Drawing;
moel@176
    40
using System.Drawing.Drawing2D;
moel@176
    41
using System.Drawing.Imaging;
moel@176
    42
using System.Drawing.Text;
moel@176
    43
using System.Windows.Forms;
moel@176
    44
moel@176
    45
namespace OpenHardwareMonitor.GUI {
moel@176
    46
  public abstract class Gadget : IDisposable {
moel@176
    47
moel@176
    48
    private GadgetWindow window;
moel@176
    49
    private Bitmap buffer;
moel@176
    50
    private Graphics graphics;
moel@176
    51
moel@176
    52
    public Gadget() {
moel@176
    53
      this.window = new GadgetWindow();
moel@176
    54
      CreateBuffer();
moel@176
    55
    }
moel@176
    56
moel@181
    57
    public virtual void Dispose() {
moel@181
    58
      DisposeBuffer();
moel@176
    59
    }
moel@176
    60
moel@176
    61
    public Point Location {
moel@176
    62
      get {
moel@176
    63
        return window.Location;
moel@176
    64
      }
moel@176
    65
      set {
moel@176
    66
        window.Location = value;
moel@176
    67
      }
moel@176
    68
    }
moel@176
    69
moel@176
    70
    public event EventHandler LocationChanged {
moel@176
    71
      add {
moel@176
    72
        window.LocationChanged += value;
moel@176
    73
      }
moel@176
    74
      remove {
moel@176
    75
        window.LocationChanged -= value;
moel@176
    76
      }
moel@176
    77
    }
moel@176
    78
moel@183
    79
    public virtual Size Size {
moel@176
    80
      get {
moel@176
    81
        return window.Size; 
moel@176
    82
      }
moel@183
    83
      set {        
moel@183
    84
        this.window.Size = value;
moel@183
    85
      }
moel@183
    86
    }
moel@183
    87
moel@183
    88
    public event EventHandler SizeChanged {
moel@183
    89
      add {
moel@183
    90
        window.SizeChanged += value;
moel@183
    91
      }
moel@183
    92
      remove {
moel@183
    93
        window.SizeChanged -= value;
moel@176
    94
      }
moel@176
    95
    }
moel@176
    96
moel@176
    97
    public byte Opacity {
moel@176
    98
      get {
moel@176
    99
        return window.Opacity;
moel@176
   100
      }
moel@176
   101
      set {
moel@176
   102
        window.Opacity = value;
moel@176
   103
      }
moel@176
   104
    }
moel@176
   105
moel@183
   106
    public bool LockPositionAndSize {
moel@176
   107
      get {
moel@183
   108
        return window.LockPositionAndSize;
moel@176
   109
      }
moel@176
   110
      set {
moel@183
   111
        window.LockPositionAndSize = value;
moel@176
   112
      }
moel@176
   113
    }
moel@176
   114
moel@176
   115
    public bool AlwaysOnTop {
moel@176
   116
      get {
moel@176
   117
        return window.AlwaysOnTop;
moel@176
   118
      }
moel@176
   119
      set {
moel@176
   120
        window.AlwaysOnTop = value;
moel@176
   121
      }
moel@176
   122
    }
moel@176
   123
moel@176
   124
    public ContextMenu ContextMenu {
moel@176
   125
      get {
moel@176
   126
        return window.ContextMenu;
moel@176
   127
      }
moel@176
   128
      set {
moel@176
   129
        window.ContextMenu = value;
moel@176
   130
      }
moel@176
   131
    }
moel@176
   132
moel@183
   133
    public event HitTestEventHandler HitTest {
moel@183
   134
      add {
moel@183
   135
        window.HitTest += value;
moel@183
   136
      }
moel@183
   137
      remove {
moel@183
   138
        window.HitTest -= value;
moel@183
   139
      }
moel@183
   140
    } 
moel@183
   141
moel@176
   142
    private void CreateBuffer() {
moel@176
   143
      this.buffer = new Bitmap(window.Size.Width, window.Size.Height, 
moel@176
   144
        PixelFormat.Format32bppArgb);
moel@176
   145
      this.graphics = Graphics.FromImage(this.buffer);
moel@176
   146
      if (Environment.OSVersion.Version.Major > 5) {
moel@176
   147
        this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
moel@176
   148
        this.graphics.SmoothingMode = SmoothingMode.HighQuality;
moel@176
   149
      }
moel@176
   150
    }
moel@176
   151
moel@176
   152
    private void DisposeBuffer() {
moel@176
   153
      if (buffer != null) {
moel@176
   154
        this.buffer.Dispose();
moel@176
   155
        this.buffer = null;
moel@176
   156
      }
moel@176
   157
      if (graphics != null) {
moel@176
   158
        this.graphics.Dispose();
moel@176
   159
        this.graphics = null;
moel@176
   160
      }
moel@176
   161
    }
moel@176
   162
moel@176
   163
    public bool Visible {
moel@176
   164
      get {
moel@176
   165
        return window.Visible;
moel@176
   166
      }
moel@176
   167
      set {
moel@176
   168
        if (value != window.Visible) {
moel@176
   169
          if (value)
moel@176
   170
            Redraw();
moel@176
   171
          window.Visible = value;
moel@176
   172
        }
moel@176
   173
      }
moel@176
   174
    }
moel@176
   175
moel@176
   176
    public void Redraw() {
moel@183
   177
      if (window.Size != buffer.Size) {
moel@183
   178
        DisposeBuffer();
moel@183
   179
        CreateBuffer();
moel@183
   180
      }
moel@183
   181
moel@176
   182
      OnPaint(new PaintEventArgs(graphics, 
moel@176
   183
        new Rectangle(Point.Empty, window.Size)));
moel@176
   184
      window.Update(buffer);
moel@176
   185
    }
moel@176
   186
moel@176
   187
    protected abstract void OnPaint(PaintEventArgs e);
moel@176
   188
  
moel@176
   189
  }
moel@176
   190
}