using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using VDash.Utilities; namespace VDash.Overlay.Actions { [OverlayActionInfo("Bring Window to Front")] public class BringWindowToFrontAction : IWindowAction { /// /// Unminimize the window if it's minimized. /// public bool UnminimizeIfMinimized { get; set; } public bool Enabled { get; set; } = true; public Type Editor { get; } = null; public BringWindowToFrontAction(bool unminimizeIfMinimized) { UnminimizeIfMinimized = unminimizeIfMinimized; } public BringWindowToFrontAction() { UnminimizeIfMinimized = true; } public void PreformAction(IOverlay intOverlay) { if (!Enabled) { return; } if (!intOverlay.GetType().IsAssignableFrom(typeof(WindowOverlay))) { Console.WriteLine("This action cannot be performed on a non-WindowOverlay overlay."); return; } WindowOverlay overlay = (WindowOverlay)intOverlay; IntPtr handle = overlay.Config.Selector.GetWindowHandle(); PreformAction(handle); } public void PreformAction(IntPtr handle) { //Make sure window isn't minimized User32.WINDOWPLACEMENT placement = new User32.WINDOWPLACEMENT(); placement.Length = Marshal.SizeOf(placement); User32.GetWindowPlacement(handle, ref placement); if (placement.ShowCmd == User32.ShowWindowCommands.ShowMinimized) { User32.ShowWindow(handle, User32.ShowWindowCommands.Normal); } User32.SetForegroundWindow(handle); } public override string ToString() { var attribute = (OverlayActionInfo)this.GetType().GetCustomAttributes(typeof(OverlayActionInfo), false).Single(); return attribute.Description; } } }