Fix some shit and code format

Also now end is to swap windows
This commit is contained in:
2025-04-27 17:56:43 +02:00
parent 6a766a5441
commit 740d3d8b01
8 changed files with 433 additions and 425 deletions

View File

@@ -12,7 +12,7 @@
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<LangVersion>10</LangVersion> <LangVersion>9.0</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>

View File

@@ -1,7 +1,5 @@
namespace DD2Switcher namespace DD2Switcher {
{ partial class Form1 {
partial class Form1
{
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
/// </summary> /// </summary>
@@ -10,11 +8,10 @@
/// <summary> /// <summary>
/// Clean up any resources being used. /// Clean up any resources being used.
/// </summary> /// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> /// <param name="disposing">true if managed resources should be disposed;
protected override void Dispose(bool disposing) /// otherwise, false.</param>
{ protected override void Dispose(bool disposing) {
if (disposing && (components != null)) if (disposing && (components != null)) {
{
components.Dispose(); components.Dispose();
} }
@@ -27,8 +24,7 @@
/// Required method for Designer support - do not modify /// Required method for Designer support - do not modify
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent() {
{
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);

View File

@@ -1,9 +1,7 @@
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher; namespace DD2Switcher {
public partial class Form1 : Form { public partial class Form1 : Form {
public Form1() { public Form1() { InitializeComponent(); }
InitializeComponent();
} }
} }

View File

@@ -3,8 +3,7 @@ using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher; namespace DD2Switcher {
public static class HotKeyManager { public static class HotKeyManager {
private static volatile MessageWindow _wnd; private static volatile MessageWindow _wnd;
private static volatile IntPtr _hwnd; private static volatile IntPtr _hwnd;
@@ -13,7 +12,8 @@ public static class HotKeyManager {
private static int _id; private static int _id;
static HotKeyManager() { static HotKeyManager() {
var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); }); var messageLoop =
new Thread(delegate() { Application.Run(new MessageWindow()); });
messageLoop.Name = "MessageLoopThread"; messageLoop.Name = "MessageLoopThread";
messageLoop.IsBackground = true; messageLoop.IsBackground = true;
messageLoop.Start(); messageLoop.Start();
@@ -24,15 +24,18 @@ public static class HotKeyManager {
public static int RegisterHotKey(Keys key, KeyModifiers modifiers) { public static int RegisterHotKey(Keys key, KeyModifiers modifiers) {
_windowReadyEvent.WaitOne(); _windowReadyEvent.WaitOne();
var id = Interlocked.Increment(ref _id); var id = Interlocked.Increment(ref _id);
_wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key); _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id,
(uint)modifiers, (uint)key);
return id; return id;
} }
public static void UnregisterHotKey(int id) { public static void UnregisterHotKey(int id) {
_wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id); _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd,
id);
} }
private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key) { private static void RegisterHotKeyInternal(IntPtr hwnd, int id,
uint modifiers, uint key) {
RegisterHotKey(hwnd, id, modifiers, key); RegisterHotKey(hwnd, id, modifiers, key);
} }
@@ -41,16 +44,19 @@ public static class HotKeyManager {
} }
private static void OnHotKeyPressed(HotKeyEventArgs e) { private static void OnHotKeyPressed(HotKeyEventArgs e) {
if (HotKeyPressed != null) HotKeyPressed(null, e); if (HotKeyPressed != null)
HotKeyPressed(null, e);
} }
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); private static extern bool RegisterHotKey(IntPtr hWnd, int id,
uint fsModifiers, uint vk);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id); private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key); private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id,
uint modifiers, uint key);
private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id); private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
@@ -103,3 +109,4 @@ public enum KeyModifiers {
Windows = 8, Windows = 8,
NoRepeat = 0x4000 NoRepeat = 0x4000
} }
}

View File

@@ -4,8 +4,7 @@ using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher; namespace DD2Switcher {
internal static class Program { internal static class Program {
private static int NumProc = 19; private static int NumProc = 19;
private static Process[] windows = new Process[NumProc]; private static Process[] windows = new Process[NumProc];
@@ -27,9 +26,11 @@ internal static class Program {
private static void CleanWindows() { private static void CleanWindows() {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) continue; if (window == null)
continue;
if (window.MainWindowHandle == IntPtr.Zero) { if (window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows"); Console.WriteLine(
$"Window at index {i} has no main window, removing from tracked windows");
windows[i] = null; windows[i] = null;
} }
} }
@@ -39,12 +40,12 @@ internal static class Program {
CleanWindows(); CleanWindows();
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) continue; if (window == null)
continue;
if (i != ActiveIndex) { if (i != ActiveIndex) {
try { try {
window.ProcessorAffinity = defaultAffinity; window.ProcessorAffinity = defaultAffinity;
} } catch (Exception e) {
catch (Exception e) {
windows[i] = null; windows[i] = null;
} }
} }
@@ -54,8 +55,7 @@ internal static class Program {
if (active != null) { if (active != null) {
try { try {
active.ProcessorAffinity = fullAffinity; active.ProcessorAffinity = fullAffinity;
} } catch (Exception e) {
catch (Exception e) {
windows[ActiveIndex] = null; windows[ActiveIndex] = null;
} }
} }
@@ -65,13 +65,13 @@ internal static class Program {
CleanWindows(); CleanWindows();
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) continue; if (window == null)
continue;
if (i != ActiveIndex) { if (i != ActiveIndex) {
try { try {
window.PriorityClass = ProcessPriorityClass.Idle; window.PriorityClass = ProcessPriorityClass.Idle;
} } catch (Exception e) {
catch (Exception e) {
windows[i] = null; windows[i] = null;
} }
} }
@@ -81,8 +81,7 @@ internal static class Program {
if (active != null) { if (active != null) {
try { try {
active.PriorityClass = ProcessPriorityClass.High; active.PriorityClass = ProcessPriorityClass.High;
} } catch (Exception e) {
catch (Exception e) {
windows[ActiveIndex] = null; windows[ActiveIndex] = null;
} }
} }
@@ -98,7 +97,8 @@ internal static class Program {
break; break;
} }
if (foregroundProcess == null) return null; if (foregroundProcess == null)
return null;
return foregroundProcess; return foregroundProcess;
} }
@@ -116,7 +116,8 @@ internal static class Program {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
if (windows[i] == null) { if (windows[i] == null) {
windows[i] = process; windows[i] = process;
Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {i}"); Console.WriteLine(
$"Added {process.ProcessName} to tracked windows at index {i}");
return; return;
} }
} }
@@ -137,12 +138,15 @@ internal static class Program {
private static void Swap(int index) { private static void Swap(int index) {
index = (index - 1) % NumProc; index = (index - 1) % NumProc;
if (index < 0) index = NumProc - 1; if (index < 0)
if (index >= NumProc) return; index = NumProc - 1;
if (index >= NumProc)
return;
CleanWindows(); CleanWindows();
Console.WriteLine($"Swapping window at index {index}"); Console.WriteLine($"Swapping window at index {index}");
var process = GetForegroundProcess(); var process = GetForegroundProcess();
if (process == null) return; if (process == null)
return;
Console.WriteLine($"Foreground process: {process}"); Console.WriteLine($"Foreground process: {process}");
bool found = false; bool found = false;
@@ -158,7 +162,8 @@ internal static class Program {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) { if (window == null) {
Console.WriteLine($"Adding foreground window to tracked at index {i}..."); Console.WriteLine(
$"Adding foreground window to tracked at index {i}...");
windows[i] = process; windows[i] = process;
} }
} }
@@ -177,17 +182,19 @@ internal static class Program {
private static void TabTo(int index) { private static void TabTo(int index) {
index = (index - 1) % NumProc; index = (index - 1) % NumProc;
if (index < 0) index = NumProc - 1; if (index < 0)
if (index >= NumProc) return; index = NumProc - 1;
if (index >= NumProc)
return;
CleanWindows(); CleanWindows();
Console.WriteLine($"Tab to window at index {index}"); Console.WriteLine($"Tab to window at index {index}");
var window = windows[index]; var window = windows[index];
if (window == null || window.MainWindowHandle == IntPtr.Zero) { if (window == null || window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows"); Console.WriteLine(
$"Window at index {index} does not exist, removing from tracked windows");
windows[index] = null; windows[index] = null;
} } else {
else {
SetForegroundWindow(window.MainWindowHandle); SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = index; ActiveIndex = index;
AdjustAffinities(); AdjustAffinities();
@@ -203,13 +210,14 @@ internal static class Program {
var currentProcess = Process.GetCurrentProcess(); var currentProcess = Process.GetCurrentProcess();
foreach (var process in processes) foreach (var process in processes)
if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) { if (process.Id != currentProcess.Id &&
process.ProcessName == currentProcess.ProcessName) {
process.Kill(); process.Kill();
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
} }
long delLast = 0; long delLast = 0;
HotKeyManager.RegisterHotKey(Keys.Decimal, KeyModifiers.NoRepeat); HotKeyManager.RegisterHotKey(Keys.End, KeyModifiers.NoRepeat);
// Register main number keys (0-9) // Register main number keys (0-9)
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
@@ -218,7 +226,6 @@ internal static class Program {
for (int i = 0; i < 9; i++) for (int i = 0; i < 9; i++)
HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
@@ -229,17 +236,15 @@ internal static class Program {
} }
long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (e.Key == Keys.Decimal) if (e.Key == Keys.End)
delLast = now; delLast = now;
int index; int index;
if (e.Key >= Keys.D0 && e.Key <= Keys.D9) { if (e.Key >= Keys.D0 && e.Key <= Keys.D9) {
index = e.Key - Keys.D0; index = e.Key - Keys.D0;
} } else if (e.Key >= Keys.NumPad1 && e.Key <= Keys.NumPad9) {
else if (e.Key >= Keys.NumPad1 && e.Key <= Keys.NumPad9) {
index = 10 + (e.Key - Keys.NumPad1); index = 10 + (e.Key - Keys.NumPad1);
} } else {
else {
return; return;
} }
@@ -255,8 +260,10 @@ internal static class Program {
} }
} }
Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); }; Console.CancelKeyPress +=
(sender, e) => { Process.GetCurrentProcess().Kill(); };
while (true) while (true)
System.Threading.Thread.Sleep(100000); System.Threading.Thread.Sleep(100000);
} }
} }
}

View File

@@ -18,7 +18,8 @@ using System.Runtime.InteropServices;
// COM, set the ComVisible attribute to true on that type. // COM, set the ComVisible attribute to true on that type.
[assembly:ComVisible(false)] [assembly:ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM // The following GUID is for the ID of the typelib if this project is exposed to
// COM
[assembly:Guid("2AC26899-8E27-4B96-85A9-C387186EAD27")] [assembly:Guid("2AC26899-8E27-4B96-85A9-C387186EAD27")]
// Version information for an assembly consists of the following four values: // Version information for an assembly consists of the following four values:
@@ -28,8 +29,7 @@ using System.Runtime.InteropServices;
// Build Number // Build Number
// Revision // Revision
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision
// by using the '*' as shown below: // Numbers by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")]
// [assembly: AssemblyVersion("1.0.*")]
[assembly:AssemblyVersion("1.0.0.0")] [assembly:AssemblyVersion("1.0.0.0")]
[assembly:AssemblyFileVersion("1.0.0.0")] [assembly:AssemblyFileVersion("1.0.0.0")]

View File

@@ -11,7 +11,6 @@
namespace DD2Switcher.Properties { namespace DD2Switcher.Properties {
using System; using System;
/// <summary> /// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc. /// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary> /// </summary>
@@ -19,7 +18,8 @@ namespace DD2Switcher.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources { internal class Resources {
@@ -28,18 +28,21 @@ namespace DD2Switcher.Properties {
private static global::System.Globalization.CultureInfo resourceCulture; private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
internal Resources() { "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
} internal Resources() {}
/// <summary> /// <summary>
/// Returns the cached ResourceManager instance used by this class. /// Returns the cached ResourceManager instance used by this class.
/// </summary> /// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] [global::System.ComponentModel.EditorBrowsableAttribute(
global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager { internal static global::System.Resources.ResourceManager ResourceManager {
get { get {
if (object.ReferenceEquals(resourceMan, null)) { if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DD2Switcher.Properties.Resources", typeof(Resources).Assembly); global::System.Resources.ResourceManager temp =
new global::System.Resources.ResourceManager(
"DD2Switcher.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp; resourceMan = temp;
} }
return resourceMan; return resourceMan;
@@ -50,14 +53,11 @@ namespace DD2Switcher.Properties {
/// Overrides the current thread's CurrentUICulture property for all /// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class. /// resource lookups using this strongly typed resource class.
/// </summary> /// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] [global::System.ComponentModel.EditorBrowsableAttribute(
global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture { internal static global::System.Globalization.CultureInfo Culture {
get { get { return resourceCulture; }
return resourceCulture; set { resourceCulture = value; }
}
set {
resourceCulture = value;
}
} }
} }
} }

View File

@@ -8,18 +8,18 @@
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
namespace DD2Switcher.Properties namespace DD2Switcher.Properties {
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute( [global::System.CodeDom.Compiler.GeneratedCodeAttribute(
"Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] "Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator",
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase "11.0.0.0")]
{ internal sealed partial class Settings
: global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = private static Settings defaultInstance =
((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); ((Settings)(global::System.Configuration.ApplicationSettingsBase
.Synchronized(new Settings())));
public static Settings Default public static Settings Default {
{
get { return defaultInstance; } get { return defaultInstance; }
} }
} }