moel@202: /* moel@176: moel@176: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@176: moel@176: The contents of this file are subject to the Mozilla Public License Version moel@176: 1.1 (the "License"); you may not use this file except in compliance with moel@176: the License. You may obtain a copy of the License at moel@176: moel@176: http://www.mozilla.org/MPL/ moel@176: moel@176: Software distributed under the License is distributed on an "AS IS" basis, moel@176: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@176: for the specific language governing rights and limitations under the License. moel@176: moel@176: The Original Code is the Open Hardware Monitor code. moel@176: moel@176: The Initial Developer of the Original Code is moel@176: Michael Möller . moel@176: Portions created by the Initial Developer are Copyright (C) 2010 moel@176: the Initial Developer. All Rights Reserved. moel@176: moel@176: Contributor(s): moel@176: moel@176: Alternatively, the contents of this file may be used under the terms of moel@176: either the GNU General Public License Version 2 or later (the "GPL"), or moel@176: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@176: in which case the provisions of the GPL or the LGPL are applicable instead moel@176: of those above. If you wish to allow use of your version of this file only moel@176: under the terms of either the GPL or the LGPL, and not to allow others to moel@176: use your version of this file under the terms of the MPL, indicate your moel@176: decision by deleting the provisions above and replace them with the notice moel@176: and other provisions required by the GPL or the LGPL. If you do not delete moel@176: the provisions above, a recipient may use your version of this file under moel@176: the terms of any one of the MPL, the GPL or the LGPL. moel@176: moel@176: */ moel@176: moel@176: using System; moel@176: using System.Drawing; moel@176: using System.Drawing.Drawing2D; moel@176: using System.Drawing.Imaging; moel@176: using System.Drawing.Text; moel@176: using System.Windows.Forms; moel@176: moel@176: namespace OpenHardwareMonitor.GUI { moel@176: public abstract class Gadget : IDisposable { moel@176: moel@176: private GadgetWindow window; moel@176: private Bitmap buffer; moel@176: private Graphics graphics; moel@176: moel@176: public Gadget() { moel@176: this.window = new GadgetWindow(); moel@176: CreateBuffer(); moel@176: } moel@176: moel@181: public virtual void Dispose() { moel@181: DisposeBuffer(); moel@176: } moel@176: moel@176: public Point Location { moel@176: get { moel@176: return window.Location; moel@176: } moel@176: set { moel@176: window.Location = value; moel@176: } moel@176: } moel@176: moel@176: public event EventHandler LocationChanged { moel@176: add { moel@176: window.LocationChanged += value; moel@176: } moel@176: remove { moel@176: window.LocationChanged -= value; moel@176: } moel@176: } moel@176: moel@183: public virtual Size Size { moel@176: get { moel@176: return window.Size; moel@176: } moel@183: set { moel@183: this.window.Size = value; moel@183: } moel@183: } moel@183: moel@183: public event EventHandler SizeChanged { moel@183: add { moel@183: window.SizeChanged += value; moel@183: } moel@183: remove { moel@183: window.SizeChanged -= value; moel@176: } moel@176: } moel@176: moel@176: public byte Opacity { moel@176: get { moel@176: return window.Opacity; moel@176: } moel@176: set { moel@176: window.Opacity = value; moel@176: } moel@176: } moel@176: moel@183: public bool LockPositionAndSize { moel@176: get { moel@183: return window.LockPositionAndSize; moel@176: } moel@176: set { moel@183: window.LockPositionAndSize = value; moel@176: } moel@176: } moel@176: moel@176: public bool AlwaysOnTop { moel@176: get { moel@176: return window.AlwaysOnTop; moel@176: } moel@176: set { moel@176: window.AlwaysOnTop = value; moel@176: } moel@176: } moel@176: moel@176: public ContextMenu ContextMenu { moel@176: get { moel@176: return window.ContextMenu; moel@176: } moel@176: set { moel@176: window.ContextMenu = value; moel@176: } moel@176: } moel@176: moel@183: public event HitTestEventHandler HitTest { moel@183: add { moel@183: window.HitTest += value; moel@183: } moel@183: remove { moel@183: window.HitTest -= value; moel@183: } moel@183: } moel@183: moel@176: private void CreateBuffer() { moel@176: this.buffer = new Bitmap(window.Size.Width, window.Size.Height, moel@176: PixelFormat.Format32bppArgb); moel@176: this.graphics = Graphics.FromImage(this.buffer); moel@176: if (Environment.OSVersion.Version.Major > 5) { moel@176: this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault; moel@176: this.graphics.SmoothingMode = SmoothingMode.HighQuality; moel@176: } moel@176: } moel@176: moel@176: private void DisposeBuffer() { moel@176: if (buffer != null) { moel@176: this.buffer.Dispose(); moel@176: this.buffer = null; moel@176: } moel@176: if (graphics != null) { moel@176: this.graphics.Dispose(); moel@176: this.graphics = null; moel@176: } moel@176: } moel@176: moel@176: public bool Visible { moel@176: get { moel@176: return window.Visible; moel@176: } moel@176: set { moel@176: if (value != window.Visible) { moel@202: window.Visible = value; moel@176: if (value) moel@202: Redraw(); moel@176: } moel@176: } moel@176: } moel@176: moel@176: public void Redraw() { moel@202: if (!window.Visible) moel@202: return; moel@202: moel@183: if (window.Size != buffer.Size) { moel@183: DisposeBuffer(); moel@183: CreateBuffer(); moel@183: } moel@183: moel@176: OnPaint(new PaintEventArgs(graphics, moel@176: new Rectangle(Point.Empty, window.Size))); moel@176: window.Update(buffer); moel@176: } moel@176: moel@176: protected abstract void OnPaint(PaintEventArgs e); moel@176: moel@176: } moel@176: }