VRDash/VDash/Overlay/Actions/ResizeWindowAction.cs

58 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VDash.UI.ActionEditors;
using VDash.Utilities;
namespace VDash.Overlay.Actions
{
[OverlayActionInfo("Resize Window", typeof(ResizeWindowActionEditor))]
public class ResizeWindowAction : IWindowAction
{
public int NewWidth { get; set; }
public int NewHeight { get; set; }
public bool Enabled { get; set; } = true;
public ResizeWindowAction(int newWidth, int newHeight)
{
NewWidth = newWidth;
NewHeight = newHeight;
}
public ResizeWindowAction() { }
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)
{
//Retain old rect
User32.Rect rect = new User32.Rect();
User32.GetWindowRect(handle, ref rect);
User32.SetWindowPos(handle, IntPtr.Zero, rect.left, rect.bottom, NewWidth, NewHeight, User32.SetWindowPosFlags.DoNotActivate |
User32.SetWindowPosFlags.IgnoreMove | User32.SetWindowPosFlags.IgnoreZOrder |
User32.SetWindowPosFlags.DoNotChangeOwnerZOrder);
}
public override string ToString()
{
var attribute = (OverlayActionInfo)this.GetType().GetCustomAttributes(typeof(OverlayActionInfo), false).Single();
return $"{attribute.Description} ({NewWidth}x{NewHeight})";
}
}
}