From 1d20e114398eb65698379e16a10c2f04cf24f867 Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Sat, 4 Apr 2020 12:22:34 +0200 Subject: [PATCH] Allow finding window children --- W32.Test/MainForm.Designer.cs | 31 ++++++++++++++++++++++--------- W32.Test/MainForm.cs | 2 ++ W32/Native/user32.cs | 5 +++++ W32/Wnd32.cs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/W32.Test/MainForm.Designer.cs b/W32.Test/MainForm.Designer.cs index 73b368c..63a6df2 100644 --- a/W32.Test/MainForm.Designer.cs +++ b/W32.Test/MainForm.Designer.cs @@ -64,6 +64,7 @@ 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_select_child = new System.Windows.Forms.Button(); this.wnd_select_list = new System.Windows.Forms.Button(); this.wnd_action_overlay = new System.Windows.Forms.CheckBox(); this.wnd_action_style = new System.Windows.Forms.ComboBox(); @@ -83,8 +84,8 @@ this.desk_back = new System.Windows.Forms.Panel(); this.readerUpdate = new System.Windows.Forms.Timer(this.components); this.time = new System.Windows.Forms.GroupBox(); - this.time_select = new System.Windows.Forms.DateTimePicker(); this.time_set = new System.Windows.Forms.Button(); + this.time_select = new System.Windows.Forms.DateTimePicker(); this.keyboard.SuspendLayout(); ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_h_bar)).BeginInit(); ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_w_bar)).BeginInit(); @@ -442,7 +443,7 @@ this.wnd_select_title_box.Size = new System.Drawing.Size(121, 23); this.wnd_select_title_box.TabIndex = 6; // - // wnd_selet_class_button + // wnd_select_class_button // this.wnd_select_class_button.Location = new System.Drawing.Point(7, 89); this.wnd_select_class_button.Name = "wnd_select_class_button"; @@ -483,6 +484,7 @@ // // wnd // + this.wnd.Controls.Add(this.wnd_select_child); this.wnd.Controls.Add(this.wnd_select_list); this.wnd.Controls.Add(this.wnd_action_overlay); this.wnd.Controls.Add(this.wnd_action_style); @@ -516,6 +518,16 @@ this.wnd.TabStop = false; this.wnd.Text = "CC-Functions.W32.Wnd32"; // + // wnd_select_child + // + this.wnd_select_child.Location = new System.Drawing.Point(107, 122); + this.wnd_select_child.Name = "wnd_select_child"; + this.wnd_select_child.Size = new System.Drawing.Size(94, 27); + this.wnd_select_child.TabIndex = 27; + this.wnd_select_child.Text = "Select (childs)"; + this.wnd_select_child.UseVisualStyleBackColor = true; + this.wnd_select_child.Click += new System.EventHandler(this.wnd_select_child_Click); + // // wnd_select_list // this.wnd_select_list.Location = new System.Drawing.Point(7, 122); @@ -740,13 +752,6 @@ this.time.TabStop = false; this.time.Text = "CC-Functions.W32.Time"; // - // time_select - // - this.time_select.Location = new System.Drawing.Point(6, 26); - this.time_select.Name = "time_select"; - this.time_select.Size = new System.Drawing.Size(205, 23); - this.time_select.TabIndex = 0; - // // time_set // this.time_set.Location = new System.Drawing.Point(217, 22); @@ -757,6 +762,13 @@ this.time_set.UseVisualStyleBackColor = true; this.time_set.Click += new System.EventHandler(this.time_set_Click); // + // time_select + // + this.time_select.Location = new System.Drawing.Point(6, 27); + this.time_select.Name = "time_select"; + this.time_select.Size = new System.Drawing.Size(205, 23); + this.time_select.TabIndex = 0; + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -851,5 +863,6 @@ private System.Windows.Forms.GroupBox time; private System.Windows.Forms.DateTimePicker time_select; private System.Windows.Forms.Button time_set; + private System.Windows.Forms.Button wnd_select_child; } } \ No newline at end of file diff --git a/W32.Test/MainForm.cs b/W32.Test/MainForm.cs index 516e0f0..49b98c2 100644 --- a/W32.Test/MainForm.cs +++ b/W32.Test/MainForm.cs @@ -83,6 +83,8 @@ namespace CC_Functions.W32.Test private void wnd_select_list_Click(object sender, EventArgs e) => TmpWnd = SelectBox.Show(Wnd32.Visible, "Please select a window") ?? TmpWnd; + private void wnd_select_child_Click(object sender, EventArgs e) => TmpWnd = SelectBox.Show(TmpWnd.Children, "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); diff --git a/W32/Native/user32.cs b/W32/Native/user32.cs index f0d0629..dcbfdee 100644 --- a/W32/Native/user32.cs +++ b/W32/Native/user32.cs @@ -6,6 +6,7 @@ namespace CC_Functions.W32.Native { internal static class user32 { + public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr lParam); public delegate bool EnumDelegate(IntPtr hWnd, int lParam); public delegate IntPtr LowLevelProc(int nCode, IntPtr wParam, IntPtr lParam); @@ -129,5 +130,9 @@ namespace CC_Functions.W32.Native [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); + + [DllImport("user32")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); } } \ No newline at end of file diff --git a/W32/Wnd32.cs b/W32/Wnd32.cs index 801316c..5ec8bad 100644 --- a/W32/Wnd32.cs +++ b/W32/Wnd32.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using CC_Functions.W32.Native; @@ -104,6 +105,28 @@ namespace CC_Functions.W32 #region InstanceActions + public Wnd32[] Children + { + get + { + List childHandles = new List(); + + GCHandle gcChildHandlesList = GCHandle.Alloc(childHandles); + IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildHandlesList); + + try + { + user32.EnumChildWindows(HWnd, EnumWindow, pointerChildHandlesList); + } + finally + { + gcChildHandlesList.Free(); + } + + return childHandles.Select(FromHandle).ToArray(); + } + } + /// /// The windows title /// @@ -344,6 +367,18 @@ namespace CC_Functions.W32 _windowHandles.Add(hWnd); return true; } + + private bool EnumWindow(IntPtr hWnd, IntPtr lParam) + { + GCHandle gcChildhandlesList = GCHandle.FromIntPtr(lParam); + + if (gcChildhandlesList.Target == null) return false; + + List childHandles = gcChildhandlesList.Target as List; + childHandles?.Add(hWnd); + + return true; + } #endregion Internal }