/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Windows.Forms; using GreenshotPlugin.Core; using GreenshotPlugin.UnmanagedHelpers; using Greenshot.IniFile; namespace GreenshotPlugin.Controls { /// /// Extend this Form to have the possibility for animations on your form /// public abstract class AnimatingForm : Form { protected static CoreConfiguration coreConfiguration = IniConfig.GetIniSection(); private int vRefresh = 0; private Timer timer = null; /// /// Vertical Refresh Rate /// protected int VRefresh { get { if (vRefresh == 0) { // get te hDC of the desktop to get the VREFRESH IntPtr hDCDesktop = User32.GetWindowDC(User32.GetDesktopWindow()); vRefresh = GDI32.GetDeviceCaps(hDCDesktop, DeviceCaps.VREFRESH); User32.ReleaseDC(hDCDesktop); } return vRefresh; } } /// /// Check if we need to optimize for RDP / Terminal Server sessions /// protected bool OptimizeForTerminalServer { get { return coreConfiguration.OptimizeForRDP || SystemInformation.TerminalServerSession; } } /// /// Calculate the amount of frames that an animation takes /// /// /// Number of frames, 1 if in Terminal Server Session protected int CalculateFrames(int milliseconds) { // If we are in a Terminal Server Session we return 1 if (OptimizeForTerminalServer) { return 1; } return milliseconds / VRefresh; } /// /// Initialize the animation /// protected AnimatingForm() { timer = new Timer(); timer.Interval = 1000 / VRefresh; timer.Tick += new EventHandler(timer_Tick); this.Load += delegate { timer.Start(); }; // Unregister at close this.FormClosing += delegate { if (timer != null) { timer.Stop(); } }; } /// /// The tick handler initiates the animation. /// /// /// void timer_Tick(object sender, EventArgs e) { Animate(); } /// /// This method will be called every frame, so implement your animation/redraw logic here. /// protected abstract void Animate(); } }