GUI/Gadget.cs
author moel.mich
Tue, 07 Sep 2010 22:15:02 +0000
changeset 181 9901dbb25f18
parent 176 c16fd81b520a
child 183 3096735e99b2
permissions -rw-r--r--
Improved the gadget formatting and added an option to remove the hardware names in the gadget.
     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) 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.Drawing;
    40 using System.Drawing.Drawing2D;
    41 using System.Drawing.Imaging;
    42 using System.Drawing.Text;
    43 using System.Windows.Forms;
    44 
    45 namespace OpenHardwareMonitor.GUI {
    46   public abstract class Gadget : IDisposable {
    47 
    48     private GadgetWindow window;
    49     private Bitmap buffer;
    50     private Graphics graphics;
    51 
    52     public Gadget() {
    53       this.window = new GadgetWindow();
    54       CreateBuffer();
    55     }
    56 
    57     public virtual void Dispose() {
    58       DisposeBuffer();
    59     }
    60 
    61     public Point Location {
    62       get {
    63         return window.Location;
    64       }
    65       set {
    66         window.Location = value;
    67       }
    68     }
    69 
    70     public event EventHandler LocationChanged {
    71       add {
    72         window.LocationChanged += value;
    73       }
    74       remove {
    75         window.LocationChanged -= value;
    76       }
    77     }
    78 
    79     protected virtual Size Size {
    80       get {
    81         return window.Size; 
    82       }
    83       set {
    84         if (window.Size != value) {
    85           DisposeBuffer();
    86           this.window.Size = value;          
    87           CreateBuffer();
    88         }
    89       }
    90     }
    91 
    92     public byte Opacity {
    93       get {
    94         return window.Opacity;
    95       }
    96       set {
    97         window.Opacity = value;
    98       }
    99     }
   100 
   101     public bool LockPosition {
   102       get {
   103         return window.LockPosition;
   104       }
   105       set {
   106         window.LockPosition = value;
   107       }
   108     }
   109 
   110     public bool AlwaysOnTop {
   111       get {
   112         return window.AlwaysOnTop;
   113       }
   114       set {
   115         window.AlwaysOnTop = value;
   116       }
   117     }
   118 
   119     public ContextMenu ContextMenu {
   120       get {
   121         return window.ContextMenu;
   122       }
   123       set {
   124         window.ContextMenu = value;
   125       }
   126     }
   127 
   128     private void CreateBuffer() {
   129       this.buffer = new Bitmap(window.Size.Width, window.Size.Height, 
   130         PixelFormat.Format32bppArgb);
   131       this.graphics = Graphics.FromImage(this.buffer);
   132       if (Environment.OSVersion.Version.Major > 5) {
   133         this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
   134         this.graphics.SmoothingMode = SmoothingMode.HighQuality;
   135       }
   136     }
   137 
   138     private void DisposeBuffer() {
   139       if (buffer != null) {
   140         this.buffer.Dispose();
   141         this.buffer = null;
   142       }
   143       if (graphics != null) {
   144         this.graphics.Dispose();
   145         this.graphics = null;
   146       }
   147     }
   148 
   149     public bool Visible {
   150       get {
   151         return window.Visible;
   152       }
   153       set {
   154         if (value != window.Visible) {
   155           if (value)
   156             Redraw();
   157           window.Visible = value;
   158         }
   159       }
   160     }
   161 
   162     public void Redraw() {
   163       OnPaint(new PaintEventArgs(graphics, 
   164         new Rectangle(Point.Empty, window.Size)));
   165       window.Update(buffer);
   166     }
   167 
   168     protected abstract void OnPaint(PaintEventArgs e);
   169   
   170   }
   171 }