GUI/Gadget.cs
author moel.mich
Mon, 16 May 2011 22:04:04 +0000
changeset 289 d4798e7f4388
parent 244 99f16e21cdc8
child 302 44c0e7f76e9e
permissions -rw-r--r--
Fixed Issue 215.
     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     public virtual Size Size {
    80       get {
    81         return window.Size; 
    82       }
    83       set {        
    84         this.window.Size = value;
    85       }
    86     }
    87 
    88     public event EventHandler SizeChanged {
    89       add {
    90         window.SizeChanged += value;
    91       }
    92       remove {
    93         window.SizeChanged -= value;
    94       }
    95     }
    96 
    97     public byte Opacity {
    98       get {
    99         return window.Opacity;
   100       }
   101       set {
   102         window.Opacity = value;
   103       }
   104     }
   105 
   106     public bool LockPositionAndSize {
   107       get {
   108         return window.LockPositionAndSize;
   109       }
   110       set {
   111         window.LockPositionAndSize = value;
   112       }
   113     }
   114 
   115     public bool AlwaysOnTop {
   116       get {
   117         return window.AlwaysOnTop;
   118       }
   119       set {
   120         window.AlwaysOnTop = value;
   121       }
   122     }
   123 
   124     public ContextMenu ContextMenu {
   125       get {
   126         return window.ContextMenu;
   127       }
   128       set {
   129         window.ContextMenu = value;
   130       }
   131     }
   132 
   133     public event HitTestEventHandler HitTest {
   134       add {
   135         window.HitTest += value;
   136       }
   137       remove {
   138         window.HitTest -= value;
   139       }
   140     }
   141 
   142     public event MouseEventHandler MouseDoubleClick {
   143       add {
   144         window.MouseDoubleClick += value;
   145       }
   146       remove {
   147         window.MouseDoubleClick -= value;
   148       }
   149     }
   150 
   151     private void CreateBuffer() {
   152       this.buffer = new Bitmap(window.Size.Width, window.Size.Height, 
   153         PixelFormat.Format32bppArgb);
   154       this.graphics = Graphics.FromImage(this.buffer);
   155       if (Environment.OSVersion.Version.Major > 5) {
   156         this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
   157         this.graphics.SmoothingMode = SmoothingMode.HighQuality;
   158       }
   159     }
   160 
   161     private void DisposeBuffer() {
   162       if (buffer != null) {
   163         this.buffer.Dispose();
   164         this.buffer = null;
   165       }
   166       if (graphics != null) {
   167         this.graphics.Dispose();
   168         this.graphics = null;
   169       }
   170     }
   171 
   172     public bool Visible {
   173       get {
   174         return window.Visible;
   175       }
   176       set {
   177         if (value != window.Visible) {
   178           window.Visible = value;
   179           if (VisibleChanged != null)
   180             VisibleChanged(this, EventArgs.Empty);
   181           if (value)
   182             Redraw();          
   183         }
   184       }
   185     }
   186 
   187     public event EventHandler VisibleChanged;
   188 
   189     public void Redraw() {
   190       if (!window.Visible)
   191         return;
   192       
   193       if (window.Size != buffer.Size) {
   194         DisposeBuffer();
   195         CreateBuffer();
   196       }
   197 
   198       OnPaint(new PaintEventArgs(graphics, 
   199         new Rectangle(Point.Empty, window.Size)));
   200       window.Update(buffer);
   201     }
   202 
   203     protected abstract void OnPaint(PaintEventArgs e);
   204   
   205   }
   206 }