Refactor the individual window panel into a separate form

This commit is contained in:
2025-08-31 20:27:45 +02:00
parent f234279135
commit 1f314d5c4b
5 changed files with 320 additions and 73 deletions

View File

@@ -59,6 +59,12 @@
<Compile Include="SettingsForm.cs"> <Compile Include="SettingsForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
<Compile Include="WindowPanelForm.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="WindowPanelForm.Designer.cs">
<DependentUpon>WindowPanelForm.cs</DependentUpon>
</Compile>
<Compile Include="HotKeyManager.cs" /> <Compile Include="HotKeyManager.cs" />
<Compile Include="KeyboardHook.cs" /> <Compile Include="KeyboardHook.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
@@ -75,6 +81,9 @@
<EmbeddedResource Include="SettingsForm.resx"> <EmbeddedResource Include="SettingsForm.resx">
<DependentUpon>SettingsForm.cs</DependentUpon> <DependentUpon>SettingsForm.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Include="WindowPanelForm.resx">
<DependentUpon>WindowPanelForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@@ -76,82 +76,20 @@ namespace DD2Switcher {
if (window == null) if (window == null)
continue; continue;
var windowPanel = CreateWindowPanel(i, window); var windowPanelForm = new WindowPanelForm();
windowsPanel.Controls.Add(windowPanel); windowPanelForm.WindowIndex = i;
windowPanelForm.WindowProcess = window;
windowPanelForm.IsFirst = (i == firstIndex);
windowPanelForm.IsLast = (i == lastIndex);
windowPanelForm.UpdateDisplay();
windowPanelForm.PickClicked += (sender, index) => PickButton_Click(sender, new EventArgs());
windowPanelForm.UntrackClicked += (sender, index) => UntrackButton_Click(sender, new EventArgs());
windowsPanel.Controls.Add(windowPanelForm);
} }
} }
private Panel CreateWindowPanel(int index, Process window) {
var panel = new Panel();
panel.Width = 520;
panel.Height = 80;
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Margin = new Padding(5);
panel.BackColor = Color.LightGray;
// Index label
var indexLabel = new Label();
indexLabel.Text = $"Index: {index}";
indexLabel.Location = new Point(10, 5);
indexLabel.AutoSize = true;
indexLabel.Font = new Font("Segoe UI", 9F, FontStyle.Bold);
panel.Controls.Add(indexLabel);
// Process name
var nameLabel = new Label();
nameLabel.Text = $"Name: {window.ProcessName}";
nameLabel.Location = new Point(10, 25);
nameLabel.AutoSize = true;
nameLabel.Font = new Font("Segoe UI", 9F);
panel.Controls.Add(nameLabel);
// PID
var pidLabel = new Label();
pidLabel.Text = $"PID: {window.Id}";
pidLabel.Location = new Point(10, 45);
pidLabel.AutoSize = true;
pidLabel.Font = new Font("Segoe UI", 9F);
panel.Controls.Add(pidLabel);
// Window title
var titleLabel = new Label();
titleLabel.Text = $"Title: {window.MainWindowTitle}";
titleLabel.Location = new Point(200, 25);
titleLabel.AutoSize = true;
titleLabel.Font = new Font("Segoe UI", 9F);
titleLabel.MaximumSize = new Size(200, 0);
panel.Controls.Add(titleLabel);
// First/Last indicator
var firstLastLabel = new Label();
firstLastLabel.Text = GetFirstLastText(index);
firstLastLabel.Location = new Point(200, 45);
firstLastLabel.AutoSize = true;
firstLastLabel.Font = new Font("Segoe UI", 9F, FontStyle.Bold);
firstLastLabel.ForeColor = Color.DarkBlue;
panel.Controls.Add(firstLastLabel);
// Pick button
var pickButton = new Button();
pickButton.Text = "Pick";
pickButton.Location = new Point(420, 10);
pickButton.Size = new Size(50, 25);
pickButton.Tag = index;
pickButton.Click += PickButton_Click;
panel.Controls.Add(pickButton);
// Untrack button
var untrackButton = new Button();
untrackButton.Text = "Untrack";
untrackButton.Location = new Point(420, 45);
untrackButton.Size = new Size(50, 25);
untrackButton.Tag = index;
untrackButton.Click += UntrackButton_Click;
panel.Controls.Add(untrackButton);
return panel;
}
private string GetFirstLastText(int index) { private string GetFirstLastText(int index) {
if (index == firstIndex && index == lastIndex) { if (index == firstIndex && index == lastIndex) {
return "First & Last"; return "First & Last";

133
DD2Switcher/WindowPanelForm.Designer.cs generated Normal file
View File

@@ -0,0 +1,133 @@
namespace DD2Switcher {
partial class WindowPanelForm {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.indexLabel = new System.Windows.Forms.Label();
this.nameLabel = new System.Windows.Forms.Label();
this.pidLabel = new System.Windows.Forms.Label();
this.titleLabel = new System.Windows.Forms.Label();
this.firstLastLabel = new System.Windows.Forms.Label();
this.pickButton = new System.Windows.Forms.Button();
this.untrackButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// indexLabel
//
this.indexLabel.AutoSize = true;
this.indexLabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.indexLabel.Location = new System.Drawing.Point(10, 5);
this.indexLabel.Name = "indexLabel";
this.indexLabel.Size = new System.Drawing.Size(52, 15);
this.indexLabel.TabIndex = 0;
this.indexLabel.Text = "Index: 0";
//
// nameLabel
//
this.nameLabel.AutoSize = true;
this.nameLabel.Font = new System.Drawing.Font("Segoe UI", 9F);
this.nameLabel.Location = new System.Drawing.Point(12, 35);
this.nameLabel.Name = "nameLabel";
this.nameLabel.Size = new System.Drawing.Size(45, 15);
this.nameLabel.TabIndex = 1;
this.nameLabel.Text = "Name: ";
//
// pidLabel
//
this.pidLabel.AutoSize = true;
this.pidLabel.Font = new System.Drawing.Font("Segoe UI", 9F);
this.pidLabel.Location = new System.Drawing.Point(12, 50);
this.pidLabel.Name = "pidLabel";
this.pidLabel.Size = new System.Drawing.Size(28, 15);
this.pidLabel.TabIndex = 2;
this.pidLabel.Text = "PID:";
//
// titleLabel
//
this.titleLabel.AutoSize = true;
this.titleLabel.Font = new System.Drawing.Font("Segoe UI", 9F);
this.titleLabel.Location = new System.Drawing.Point(12, 20);
this.titleLabel.MaximumSize = new System.Drawing.Size(200, 0);
this.titleLabel.Name = "titleLabel";
this.titleLabel.Size = new System.Drawing.Size(36, 15);
this.titleLabel.TabIndex = 3;
this.titleLabel.Text = "Title: ";
//
// firstLastLabel
//
this.firstLastLabel.AutoSize = true;
this.firstLastLabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.firstLastLabel.ForeColor = System.Drawing.Color.DarkBlue;
this.firstLastLabel.Location = new System.Drawing.Point(200, 45);
this.firstLastLabel.Name = "firstLastLabel";
this.firstLastLabel.Size = new System.Drawing.Size(0, 15);
this.firstLastLabel.TabIndex = 4;
//
// pickButton
//
this.pickButton.Location = new System.Drawing.Point(420, 10);
this.pickButton.Name = "pickButton";
this.pickButton.Size = new System.Drawing.Size(88, 25);
this.pickButton.TabIndex = 5;
this.pickButton.Text = "Pick";
this.pickButton.UseVisualStyleBackColor = true;
this.pickButton.Click += new System.EventHandler(this.pickButton_Click);
//
// untrackButton
//
this.untrackButton.Location = new System.Drawing.Point(420, 45);
this.untrackButton.Name = "untrackButton";
this.untrackButton.Size = new System.Drawing.Size(88, 25);
this.untrackButton.TabIndex = 6;
this.untrackButton.Text = "Untrack";
this.untrackButton.UseVisualStyleBackColor = true;
this.untrackButton.Click += new System.EventHandler(this.untrackButton_Click);
//
// WindowPanelForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.indexLabel);
this.Controls.Add(this.nameLabel);
this.Controls.Add(this.pidLabel);
this.Controls.Add(this.titleLabel);
this.Controls.Add(this.firstLastLabel);
this.Controls.Add(this.pickButton);
this.Controls.Add(this.untrackButton);
this.Name = "WindowPanelForm";
this.Size = new System.Drawing.Size(520, 80);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label indexLabel;
private System.Windows.Forms.Label nameLabel;
private System.Windows.Forms.Label pidLabel;
private System.Windows.Forms.Label titleLabel;
private System.Windows.Forms.Label firstLastLabel;
private System.Windows.Forms.Button pickButton;
private System.Windows.Forms.Button untrackButton;
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace DD2Switcher {
public partial class WindowPanelForm : UserControl {
public event EventHandler<int> PickClicked;
public event EventHandler<int> UntrackClicked;
public int WindowIndex { get; set; }
public Process WindowProcess { get; set; }
public bool IsFirst { get; set; }
public bool IsLast { get; set; }
public WindowPanelForm() {
InitializeComponent();
}
public void UpdateDisplay() {
if (WindowProcess != null) {
indexLabel.Text = $"Index: {WindowIndex}";
nameLabel.Text = $"Name: {WindowProcess.ProcessName}";
pidLabel.Text = $"PID: {WindowProcess.Id}";
titleLabel.Text = $"Title: {WindowProcess.MainWindowTitle}";
if (IsFirst && IsLast) {
firstLastLabel.Text = "First & Last";
} else if (IsFirst) {
firstLastLabel.Text = "First";
} else if (IsLast) {
firstLastLabel.Text = "Last";
} else {
firstLastLabel.Text = "";
}
}
}
private void pickButton_Click(object sender, EventArgs e) {
PickClicked?.Invoke(this, WindowIndex);
}
private void untrackButton_Click(object sender, EventArgs e) {
UntrackClicked?.Invoke(this, WindowIndex);
}
}
}

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>