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/Program.cs

107 lines
3.9 KiB
C#
Raw Normal View History

2019-09-06 20:45:42 +02:00
using System;
using System.Collections.Generic;
2019-09-09 15:55:52 +02:00
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
2019-09-06 20:45:42 +02:00
using System.Windows.Forms;
2020-03-08 13:55:21 +01:00
using CC_Functions.W32.Hooks;
2019-09-06 20:45:42 +02:00
using Resizor.Properties;
using Timer = System.Windows.Forms.Timer;
namespace Resizor
{
2020-03-08 13:55:21 +01:00
internal static class Program
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
public static KeyboardHook Kh;
public static NiApplicationContext Ctx;
private static ImmResize _rez;
private static NotifyIcon _notifyIcon;
2019-09-06 20:45:42 +02:00
[STAThread]
2020-03-08 13:55:21 +01:00
private static void Main()
2019-09-06 20:45:42 +02:00
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2020-03-08 13:55:21 +01:00
string appGuid = ((GuidAttribute) Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value;
MutexAccessRule allowEveryoneRule = new MutexAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl,
AccessControlType.Allow);
MutexSecurity securitySettings = new MutexSecurity();
2019-09-09 15:55:52 +02:00
securitySettings.AddAccessRule(allowEveryoneRule);
2020-03-08 13:55:21 +01:00
using Mutex mutex = new Mutex(false, $"Global\\{{{appGuid}}}", out bool _);
bool hasHandle = false;
try
2019-09-09 15:55:52 +02:00
{
try
{
2020-03-08 13:55:21 +01:00
hasHandle = mutex.WaitOne(5000, false);
if (hasHandle == false)
throw new TimeoutException("Timeout waiting for exclusive access");
2019-09-09 15:55:52 +02:00
}
2020-03-08 13:55:21 +01:00
catch (AbandonedMutexException)
2019-09-09 15:55:52 +02:00
{
2020-03-08 13:55:21 +01:00
#if DEBUG
Console.WriteLine("Mutex abandoned");
#endif
hasHandle = true;
2019-09-09 15:55:52 +02:00
}
2020-03-08 13:55:21 +01:00
_notifyIcon = new NotifyIcon();
ContextMenuStrip contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Settings").Click += OpenSettings;
contextMenu.Items.Add("Exit").Click += Exit;
_notifyIcon.Icon = Resources.Resizor;
_notifyIcon.Text = "Resizor";
_notifyIcon.ContextMenuStrip = contextMenu;
_notifyIcon.Visible = true;
Kh = new KeyboardHook();
Kh.OnKeyPress += KeyDown;
Ctx = new NiApplicationContext();
Application.Run(Ctx);
Kh.Dispose();
_notifyIcon.Visible = false;
}
finally
{
if (hasHandle)
mutex.ReleaseMutex();
2019-09-09 15:55:52 +02:00
}
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
private static void KeyDown(KeyboardHookEventArgs e)
2019-09-06 20:45:42 +02:00
{
2020-03-24 21:48:10 +01:00
if (e.Key != Settings.ImmediateResizeKey || (_rez != null && !_rez.IsDisposed)) return;
2020-03-08 13:55:21 +01:00
_rez = new ImmResize();
_rez.Show();
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
private static void OpenSettings(object sender, EventArgs e) => new SettingsForm().Show();
private static void Exit(object sender, EventArgs e) => Application.Exit();
public class NiApplicationContext : ApplicationContext
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
public readonly List<WindowSizeSetter> WindowSizeSetters = new List<WindowSizeSetter>();
public NiApplicationContext()
2019-09-06 20:45:42 +02:00
{
2020-03-08 13:55:21 +01:00
Timer tim = new Timer {Enabled = true, Interval = 100};
tim.Tick += Tick;
2019-09-06 20:45:42 +02:00
}
2020-03-08 13:55:21 +01:00
private void Tick(object sender, EventArgs e)
2019-09-06 20:45:42 +02:00
{
List<int> toRemove = new List<int>();
2020-03-08 13:55:21 +01:00
for (int i = 0; i < WindowSizeSetters.Count; i++)
if (WindowSizeSetters[i].Window.StillExists)
WindowSizeSetters[i].Window.Position = WindowSizeSetters[i].Pos;
2019-09-06 20:45:42 +02:00
else
toRemove.Add(i);
for (int i = 0; i < toRemove.Count; i++)
2020-03-08 13:55:21 +01:00
WindowSizeSetters.RemoveAt(toRemove[i]);
2019-09-06 20:45:42 +02:00
}
}
}
2020-03-08 13:55:21 +01:00
}