From a21a89f051649b9ec1269ba001ad28c979447ce6 Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Wed, 18 Dec 2019 17:43:40 +0100 Subject: [PATCH] Allow getting a list of windows, added SelectBox --- Misc/Misc.csproj | 9 +++ Misc/SelectBox.Designer.cs | 61 +++++++++++++++++++ Misc/SelectBox.cs | 38 ++++++++++++ Misc/SelectBox.resx | 120 +++++++++++++++++++++++++++++++++++++ W32.Test/Form1.Designer.cs | 39 ++++++++---- W32.Test/Form1.cs | 5 +- W32.Test/W32.Test.csproj | 4 ++ W32/Wnd32.cs | 30 ++++++++++ 8 files changed, 292 insertions(+), 14 deletions(-) create mode 100644 Misc/SelectBox.Designer.cs create mode 100644 Misc/SelectBox.cs create mode 100644 Misc/SelectBox.resx diff --git a/Misc/Misc.csproj b/Misc/Misc.csproj index 624dfe6..608d34b 100644 --- a/Misc/Misc.csproj +++ b/Misc/Misc.csproj @@ -58,9 +58,18 @@ + + Form + + + SelectBox.cs + + + SelectBox.cs + \ No newline at end of file diff --git a/Misc/SelectBox.Designer.cs b/Misc/SelectBox.Designer.cs new file mode 100644 index 0000000..5f0d86e --- /dev/null +++ b/Misc/SelectBox.Designer.cs @@ -0,0 +1,61 @@ +namespace CC_Functions.Misc +{ + partial class SelectBox + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.listBox1 = new System.Windows.Forms.ListBox(); + this.SuspendLayout(); + // + // listBox1 + // + this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.listBox1.FormattingEnabled = true; + this.listBox1.Location = new System.Drawing.Point(0, 0); + this.listBox1.Name = "listBox1"; + this.listBox1.Size = new System.Drawing.Size(433, 145); + this.listBox1.TabIndex = 0; + this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); + // + // SelectBox + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(433, 145); + this.Controls.Add(this.listBox1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Name = "SelectBox"; + this.Text = "SelectBox"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.ListBox listBox1; + } +} \ No newline at end of file diff --git a/Misc/SelectBox.cs b/Misc/SelectBox.cs new file mode 100644 index 0000000..110aa7a --- /dev/null +++ b/Misc/SelectBox.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +namespace CC_Functions.Misc +{ + partial class SelectBox : Form + { + public T result; + public SelectBox(T[] Options, string title = "") + { + InitializeComponent(); + Text = title; + listBox1.Items.AddRange(Options.Select(s => (object)s).ToArray()); + } + + private void listBox1_SelectedIndexChanged(object sender, EventArgs e) + { + result = (T)listBox1.SelectedItem; + DialogResult = DialogResult.OK; + Close(); + } + } + + public static class SelectBox + { + public static T Show(T[] Options, string title = "") + { + SelectBox sb = new SelectBox(Options, title); + return sb.ShowDialog() == DialogResult.OK ? sb.result : default(T); + } + } +} diff --git a/Misc/SelectBox.resx b/Misc/SelectBox.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Misc/SelectBox.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/W32.Test/Form1.Designer.cs b/W32.Test/Form1.Designer.cs index fce9640..afb5964 100644 --- a/W32.Test/Form1.Designer.cs +++ b/W32.Test/Form1.Designer.cs @@ -64,11 +64,12 @@ this.wnd_select_selected = new System.Windows.Forms.Label(); this.wnd_select_self = new System.Windows.Forms.Button(); this.wnd = new System.Windows.Forms.GroupBox(); + this.wnd_action_overlay = new System.Windows.Forms.CheckBox(); this.wnd_action_style = new System.Windows.Forms.ComboBox(); this.wnd_action_visible = new System.Windows.Forms.CheckBox(); this.wnd_action_icon = new System.Windows.Forms.Panel(); this.wnd_select_class_box = new System.Windows.Forms.TextBox(); - this.wnd_action_overlay = new System.Windows.Forms.CheckBox(); + this.wnd_select_list = new System.Windows.Forms.Button(); this.keyboard.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_h_bar)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_w_bar)).BeginInit(); @@ -346,7 +347,7 @@ // wnd_action_enabled // this.wnd_action_enabled.AutoSize = true; - this.wnd_action_enabled.Location = new System.Drawing.Point(6, 110); + this.wnd_action_enabled.Location = new System.Drawing.Point(113, 110); this.wnd_action_enabled.Name = "wnd_action_enabled"; this.wnd_action_enabled.Size = new System.Drawing.Size(65, 17); this.wnd_action_enabled.TabIndex = 9; @@ -422,6 +423,7 @@ // // wnd // + this.wnd.Controls.Add(this.wnd_select_list); this.wnd.Controls.Add(this.wnd_action_overlay); this.wnd.Controls.Add(this.wnd_action_style); this.wnd.Controls.Add(this.wnd_action_visible); @@ -454,6 +456,17 @@ this.wnd.TabStop = false; this.wnd.Text = "CC-Functions.W32.Wnd32"; // + // wnd_action_overlay + // + this.wnd_action_overlay.AutoSize = true; + this.wnd_action_overlay.Location = new System.Drawing.Point(136, 135); + this.wnd_action_overlay.Name = "wnd_action_overlay"; + this.wnd_action_overlay.Size = new System.Drawing.Size(62, 17); + this.wnd_action_overlay.TabIndex = 25; + this.wnd_action_overlay.Text = "Overlay"; + this.wnd_action_overlay.UseVisualStyleBackColor = true; + this.wnd_action_overlay.CheckedChanged += new System.EventHandler(this.wnd_action_overlay_CheckedChanged); + // // wnd_action_style // this.wnd_action_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; @@ -467,7 +480,7 @@ // wnd_action_visible // this.wnd_action_visible.AutoSize = true; - this.wnd_action_visible.Location = new System.Drawing.Point(77, 110); + this.wnd_action_visible.Location = new System.Drawing.Point(184, 110); this.wnd_action_visible.Name = "wnd_action_visible"; this.wnd_action_visible.Size = new System.Drawing.Size(56, 17); this.wnd_action_visible.TabIndex = 23; @@ -479,7 +492,7 @@ // this.wnd_action_icon.BackColor = System.Drawing.SystemColors.ControlLight; this.wnd_action_icon.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.wnd_action_icon.Location = new System.Drawing.Point(217, 106); + this.wnd_action_icon.Location = new System.Drawing.Point(217, 131); this.wnd_action_icon.Name = "wnd_action_icon"; this.wnd_action_icon.Size = new System.Drawing.Size(23, 23); this.wnd_action_icon.TabIndex = 22; @@ -491,16 +504,15 @@ this.wnd_select_class_box.Size = new System.Drawing.Size(104, 20); this.wnd_select_class_box.TabIndex = 5; // - // wnd_action_overlay + // wnd_select_list // - this.wnd_action_overlay.AutoSize = true; - this.wnd_action_overlay.Location = new System.Drawing.Point(139, 110); - this.wnd_action_overlay.Name = "wnd_action_overlay"; - this.wnd_action_overlay.Size = new System.Drawing.Size(62, 17); - this.wnd_action_overlay.TabIndex = 25; - this.wnd_action_overlay.Text = "Overlay"; - this.wnd_action_overlay.UseVisualStyleBackColor = true; - this.wnd_action_overlay.CheckedChanged += new System.EventHandler(this.wnd_action_overlay_CheckedChanged); + this.wnd_select_list.Location = new System.Drawing.Point(6, 106); + this.wnd_select_list.Name = "wnd_select_list"; + this.wnd_select_list.Size = new System.Drawing.Size(81, 23); + this.wnd_select_list.TabIndex = 26; + this.wnd_select_list.Text = "Select (list)"; + this.wnd_select_list.UseVisualStyleBackColor = true; + this.wnd_select_list.Click += new System.EventHandler(this.wnd_select_list_Click); // // Form1 // @@ -575,5 +587,6 @@ private System.Windows.Forms.CheckBox wnd_action_visible; private System.Windows.Forms.ComboBox wnd_action_style; private System.Windows.Forms.CheckBox wnd_action_overlay; + private System.Windows.Forms.Button wnd_select_list; } } \ No newline at end of file diff --git a/W32.Test/Form1.cs b/W32.Test/Form1.cs index 0538990..265de13 100644 --- a/W32.Test/Form1.cs +++ b/W32.Test/Form1.cs @@ -1,4 +1,5 @@ -using System; +using CC_Functions.Misc; +using System; using System.Drawing; using System.Linq; using System.Windows.Forms; @@ -55,6 +56,8 @@ namespace CC_Functions.W32.Test private void Wnd_select_self_Click(object sender, EventArgs e) => tmpWnd = Wnd32.fromForm(this); + private void wnd_select_list_Click(object sender, EventArgs e) => tmpWnd = SelectBox.Show(Wnd32.getVisible(), "Please select a window") ?? tmpWnd; + private void Wnd_select_title_button_Click(object sender, EventArgs e) => tmpWnd = Wnd32.fromMetadata(null, wnd_select_title_box.Text); private void Wnd_selet_class_button_Click(object sender, EventArgs e) => tmpWnd = Wnd32.fromMetadata(wnd_select_class_box.Text, null); diff --git a/W32.Test/W32.Test.csproj b/W32.Test/W32.Test.csproj index d7ae82a..ceef7b1 100644 --- a/W32.Test/W32.Test.csproj +++ b/W32.Test/W32.Test.csproj @@ -82,6 +82,10 @@ + + {B80D5E09-B935-4602-A173-BAF7C1974999} + Misc + {23de4ae0-5075-4ccc-8440-4d131ca0fbba} W32 diff --git a/W32/Wnd32.cs b/W32/Wnd32.cs index ec9fe48..b7bf504 100644 --- a/W32/Wnd32.cs +++ b/W32/Wnd32.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; +using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; @@ -25,6 +27,18 @@ namespace CC_Functions.W32 public static Wnd32 foreground() => fromHandle(GetForegroundWindow()); + public static Wnd32[] getVisible() + { + WindowHandles = new List(); + if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero)) + { + throw new Win32Exception("There was a native error. This should never happen!"); + } + else + { + return WindowHandles.Select(s => fromHandle(s)).ToArray(); + } + } #endregion CreateInstance #region InstanceActions @@ -264,6 +278,22 @@ namespace CC_Functions.W32 [DllImport("user32.dll", SetLastError = true)] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); + [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] + private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam); + + // Define the callback delegate's type. + private delegate bool EnumDelegate(IntPtr hWnd, int lParam); + private static List WindowHandles; + private static bool FilterCallback(IntPtr hWnd, int lParam) + { + StringBuilder sb_title = new StringBuilder(1024); + GetWindowText(hWnd, sb_title, sb_title.Capacity); + if (IsWindowVisible(hWnd) && string.IsNullOrEmpty(sb_title.ToString()) == false) + { + WindowHandles.Add(hWnd); + } + return true; + } #endregion W32 } } \ No newline at end of file