This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
Resizor/Resizor/Immediate Resize.cs

134 lines
5.0 KiB
C#
Raw Normal View History

2019-09-06 20:45:42 +02:00
using System;
using System.Drawing;
2019-11-10 13:25:27 +01:00
using System.Drawing.Drawing2D;
2019-09-06 20:45:42 +02:00
using System.Linq;
using System.Windows.Forms;
using CC_Functions.W32;
2020-03-08 13:55:21 +01:00
using CC_Functions.W32.Hooks;
2019-09-06 20:45:42 +02:00
namespace Resizor
{
2020-03-08 13:55:21 +01:00
public partial class ImmResize : Form
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
private readonly Wnd32 _window;
private bool _down;
private Rectangle _prevR;
private Rectangle _screen = Screen.PrimaryScreen.WorkingArea;
private Point _startP;
public ImmResize()
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
_prevR = new Rectangle();
_window = Wnd32.Foreground;
2019-09-06 20:45:42 +02:00
InitializeComponent();
2020-03-08 13:55:21 +01:00
Program.Kh.OnKeyPress += OnKeyDown;
Rectangle tmp = _window.Position;
2019-11-10 13:25:27 +01:00
forcePos.Location = new Point((tmp.X + (tmp.Width / 2)) - (forcePos.Width / 2), tmp.Y);
2020-03-08 13:55:21 +01:00
forcePos.Checked = Program.Ctx.WindowSizeSetters.Where(s => s.Window == _window).ToArray().Length > 0;
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
private void OnKeyDown(KeyboardHookEventArgs args)
2019-09-11 11:30:49 +02:00
{
2020-03-08 13:55:21 +01:00
if (args.Key == Keys.Escape)
2019-09-11 11:30:49 +02:00
Close();
}
2020-03-08 13:55:21 +01:00
private void ImmResize_FormClosed(object sender, FormClosedEventArgs e) => Program.Kh.OnKeyPress -= OnKeyDown;
2019-09-11 11:30:49 +02:00
2019-09-06 20:45:42 +02:00
private void Form1_Load(object sender, EventArgs e)
{
2020-03-08 13:55:21 +01:00
Wnd32 self = this.GetWnd32();
self.Overlay = true;
if (self != _window)
self.IsForeground = true;
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
2019-09-06 20:45:42 +02:00
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
2019-11-10 13:25:27 +01:00
g.SmoothingMode = SmoothingMode.None;
g.InterpolationMode = InterpolationMode.Low;
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.HighSpeed;
g.PixelOffsetMode = PixelOffsetMode.None;
2020-03-24 21:48:10 +01:00
PointF divisor = Settings.ResizeDividor;
2020-03-08 13:55:21 +01:00
Rectangle rect = _down ? FRect() : CRect();
2019-09-06 20:45:42 +02:00
g.FillRectangle(new SolidBrush(Color.LightBlue), rect);
Pen gridPen = new Pen(Color.Black, 2);
2020-03-08 13:55:21 +01:00
PointF div = GetDiv();
for (int x = 0; x < divisor.X; x++) g.DrawLine(gridPen, x * div.X, 0, x * div.X, _screen.Height);
for (int y = 0; y < divisor.Y; y++) g.DrawLine(gridPen, 0, y * div.Y, _screen.Width, y * div.Y);
2019-09-06 20:45:42 +02:00
g.DrawRectangle(new Pen(Color.Blue, 2), rect);
2020-03-08 13:55:21 +01:00
g.DrawRectangle(new Pen(Color.Red, 2), _window.Position);
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
2020-03-24 21:48:10 +01:00
private PointF GetDiv() => new PointF(_screen.Width / (float) Settings.ResizeDividor.X,
_screen.Height / (float) Settings.ResizeDividor.Y);
2020-03-08 13:55:21 +01:00
private Rectangle CRect() => P2R(F2S(MousePosition, GetDiv()), C2S(MousePosition, GetDiv()));
private Rectangle FRect()
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
Point min = F2S(new Point(Math.Min(MousePosition.X, _startP.X), Math.Min(MousePosition.Y, _startP.Y)),
GetDiv());
Point max = C2S(new Point(Math.Max(MousePosition.X, _startP.X), Math.Max(MousePosition.Y, _startP.Y)),
GetDiv());
return P2R(min, max);
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
private static Point F2S(Point p, PointF step) => new Point(F2S(p.X, step.X), F2S(p.Y, step.Y));
private static Point C2S(Point p, PointF step) => new Point(C2S(p.X, step.X), C2S(p.Y, step.Y));
private static Rectangle P2R(Point p1, Point p2) => new Rectangle(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y),
Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
private static int F2S(int f, double step) => (int) D2F(Math.Floor(f / step) * step);
private static int C2S(int f, double step) => (int) D2F(Math.Ceiling(f / step) * step);
private static float D2F(double f)
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
float result = (float) f;
return float.IsPositiveInfinity(result) ? float.MaxValue :
float.IsNegativeInfinity(result) ? float.MinValue : result;
2019-09-06 20:45:42 +02:00
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
2020-03-08 13:55:21 +01:00
_down = true;
_startP = MousePosition;
2019-09-06 20:45:42 +02:00
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
2020-03-08 13:55:21 +01:00
Rectangle rect = _down ? FRect() : CRect();
if (_prevR != rect)
2019-09-06 20:45:42 +02:00
Invalidate();
2020-03-08 13:55:21 +01:00
_prevR = rect;
2019-09-06 20:45:42 +02:00
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
2020-03-08 14:01:21 +01:00
Rectangle tmp = FRect();
_window.Position = tmp;
if (forcePos.Checked)
WindowSizeSetter.Make(_window, tmp);
2019-09-06 20:45:42 +02:00
Close();
}
private void ForcePos_CheckedChanged(object sender, EventArgs e)
{
2019-11-10 13:25:27 +01:00
if (forcePos.Checked)
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
if (Program.Ctx.WindowSizeSetters.Any(s => s.Window == _window)) return;
WindowSizeSetter.Make(_window, _window.Position);
Close();
2019-11-10 13:25:27 +01:00
}
else
{
2020-03-08 13:55:21 +01:00
if (Program.Ctx.WindowSizeSetters.All(s => s.Window != _window)) return;
WindowSizeSetter.TryRemove(_window);
Close();
2019-09-06 20:45:42 +02:00
}
}
}
2020-03-08 13:55:21 +01:00
}