From 7d7314b889a3f7b49f3afa62e8943ada6a389c8b Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Wed, 15 Jan 2020 17:59:29 +0100 Subject: [PATCH] Experimental changes to (basically) everything --- W32.Test/Form1.Designer.cs | 216 ++++++++++++++++++----------------- W32.Test/Form1.cs | 2 +- W32/DCDrawer/DCBuffered.cs | 29 +++++ W32/DCDrawer/DCUnbuffered.cs | 34 ++++++ W32/DCDrawer/IDCDrawer.cs | 10 ++ W32/DeskMan.cs | 57 +++++++++ W32/GenericExtensions.cs | 3 + W32/Hooks/KeyboardHook.cs | 27 +---- W32/Hooks/MouseHook.cs | 26 +---- W32/KeyboardReader.cs | 9 +- W32/Native/RECT.cs | 13 +++ W32/Native/advapi32.cs | 25 ++++ W32/Native/gdi32.cs | 27 +++++ W32/Native/kernel32.cs | 21 ++++ W32/Native/ntdll.cs | 22 ++++ W32/Native/shell32.cs | 11 ++ W32/Native/user32.cs | 127 ++++++++++++++++++++ W32/Power.cs | 25 +--- W32/Privileges.cs | 80 +++++-------- W32/ScreenMan.cs | 50 ++++++++ W32/W32.csproj | 12 ++ W32/Wnd32.cs | 167 +++++++-------------------- 22 files changed, 638 insertions(+), 355 deletions(-) create mode 100644 W32/DCDrawer/DCBuffered.cs create mode 100644 W32/DCDrawer/DCUnbuffered.cs create mode 100644 W32/DCDrawer/IDCDrawer.cs create mode 100644 W32/DeskMan.cs create mode 100644 W32/Native/RECT.cs create mode 100644 W32/Native/advapi32.cs create mode 100644 W32/Native/gdi32.cs create mode 100644 W32/Native/kernel32.cs create mode 100644 W32/Native/ntdll.cs create mode 100644 W32/Native/shell32.cs create mode 100644 W32/Native/user32.cs create mode 100644 W32/ScreenMan.cs diff --git a/W32.Test/Form1.Designer.cs b/W32.Test/Form1.Designer.cs index afb5964..9194a03 100644 --- a/W32.Test/Form1.Designer.cs +++ b/W32.Test/Form1.Designer.cs @@ -64,17 +64,17 @@ 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_list = new System.Windows.Forms.Button(); 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_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(); - ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_y_bar)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_x_bar)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_h_bar)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_w_bar)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_y_bar)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_x_bar)).BeginInit(); this.mouse.SuspendLayout(); this.power.SuspendLayout(); this.wnd.SuspendLayout(); @@ -83,11 +83,12 @@ // exit // this.exit.BackColor = System.Drawing.Color.Black; - this.exit.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.exit.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Regular, + System.Drawing.GraphicsUnit.Point, ((byte) (0))); this.exit.ForeColor = System.Drawing.Color.White; - this.exit.Location = new System.Drawing.Point(264, 117); + this.exit.Location = new System.Drawing.Point(308, 135); this.exit.Name = "exit"; - this.exit.Size = new System.Drawing.Size(35, 37); + this.exit.Size = new System.Drawing.Size(41, 43); this.exit.TabIndex = 9; this.exit.Text = "°"; this.exit.UseVisualStyleBackColor = false; @@ -98,11 +99,11 @@ // // keyboard_log // - this.keyboard_log.Location = new System.Drawing.Point(6, 42); + this.keyboard_log.Location = new System.Drawing.Point(7, 48); this.keyboard_log.Multiline = true; this.keyboard_log.Name = "keyboard_log"; this.keyboard_log.ReadOnly = true; - this.keyboard_log.Size = new System.Drawing.Size(286, 150); + this.keyboard_log.Size = new System.Drawing.Size(333, 172); this.keyboard_log.TabIndex = 2; this.keyboard_log.TextChanged += new System.EventHandler(this.Keyboard_log_TextChanged); // @@ -110,9 +111,9 @@ // this.keyboard.Controls.Add(this.keyboard_log); this.keyboard.Controls.Add(this.keyboard_enabled); - this.keyboard.Location = new System.Drawing.Point(264, 160); + this.keyboard.Location = new System.Drawing.Point(308, 185); this.keyboard.Name = "keyboard"; - this.keyboard.Size = new System.Drawing.Size(298, 198); + this.keyboard.Size = new System.Drawing.Size(348, 228); this.keyboard.TabIndex = 8; this.keyboard.TabStop = false; this.keyboard.Text = "CC-Functions.W32.KeyboardHook"; @@ -120,9 +121,9 @@ // keyboard_enabled // this.keyboard_enabled.AutoSize = true; - this.keyboard_enabled.Location = new System.Drawing.Point(6, 19); + this.keyboard_enabled.Location = new System.Drawing.Point(7, 22); this.keyboard_enabled.Name = "keyboard_enabled"; - this.keyboard_enabled.Size = new System.Drawing.Size(65, 17); + this.keyboard_enabled.Size = new System.Drawing.Size(68, 19); this.keyboard_enabled.TabIndex = 0; this.keyboard_enabled.Text = "Enabled"; this.keyboard_enabled.UseVisualStyleBackColor = true; @@ -130,20 +131,20 @@ // // mouse_log // - this.mouse_log.Location = new System.Drawing.Point(6, 42); + this.mouse_log.Location = new System.Drawing.Point(7, 48); this.mouse_log.Multiline = true; this.mouse_log.Name = "mouse_log"; this.mouse_log.ReadOnly = true; - this.mouse_log.Size = new System.Drawing.Size(245, 95); + this.mouse_log.Size = new System.Drawing.Size(285, 109); this.mouse_log.TabIndex = 1; this.mouse_log.TextChanged += new System.EventHandler(this.Mouse_log_TextChanged); // // mouse_enabled // this.mouse_enabled.AutoSize = true; - this.mouse_enabled.Location = new System.Drawing.Point(6, 19); + this.mouse_enabled.Location = new System.Drawing.Point(7, 22); this.mouse_enabled.Name = "mouse_enabled"; - this.mouse_enabled.Size = new System.Drawing.Size(65, 17); + this.mouse_enabled.Size = new System.Drawing.Size(68, 19); this.mouse_enabled.TabIndex = 0; this.mouse_enabled.Text = "Enabled"; this.mouse_enabled.UseVisualStyleBackColor = true; @@ -152,72 +153,72 @@ // wnd_action_pos_h_label // this.wnd_action_pos_h_label.AutoSize = true; - this.wnd_action_pos_h_label.Location = new System.Drawing.Point(116, 210); + this.wnd_action_pos_h_label.Location = new System.Drawing.Point(135, 242); this.wnd_action_pos_h_label.Name = "wnd_action_pos_h_label"; - this.wnd_action_pos_h_label.Size = new System.Drawing.Size(18, 13); + this.wnd_action_pos_h_label.Size = new System.Drawing.Size(19, 15); this.wnd_action_pos_h_label.TabIndex = 19; this.wnd_action_pos_h_label.Text = "H:"; // // wnd_action_pos_w_label // this.wnd_action_pos_w_label.AutoSize = true; - this.wnd_action_pos_w_label.Location = new System.Drawing.Point(116, 186); + this.wnd_action_pos_w_label.Location = new System.Drawing.Point(135, 215); this.wnd_action_pos_w_label.Name = "wnd_action_pos_w_label"; - this.wnd_action_pos_w_label.Size = new System.Drawing.Size(21, 13); + this.wnd_action_pos_w_label.Size = new System.Drawing.Size(21, 15); this.wnd_action_pos_w_label.TabIndex = 18; this.wnd_action_pos_w_label.Text = "W:"; // // wnd_action_pos_h_bar // - this.wnd_action_pos_h_bar.Location = new System.Drawing.Point(136, 210); + this.wnd_action_pos_h_bar.Location = new System.Drawing.Point(159, 242); this.wnd_action_pos_h_bar.Name = "wnd_action_pos_h_bar"; - this.wnd_action_pos_h_bar.Size = new System.Drawing.Size(104, 45); + this.wnd_action_pos_h_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_h_bar.TabIndex = 21; // // wnd_action_pos_w_bar // - this.wnd_action_pos_w_bar.Location = new System.Drawing.Point(136, 186); + this.wnd_action_pos_w_bar.Location = new System.Drawing.Point(159, 215); this.wnd_action_pos_w_bar.Name = "wnd_action_pos_w_bar"; - this.wnd_action_pos_w_bar.Size = new System.Drawing.Size(104, 45); + this.wnd_action_pos_w_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_w_bar.TabIndex = 20; // // wnd_action_pos_y_label // this.wnd_action_pos_y_label.AutoSize = true; - this.wnd_action_pos_y_label.Location = new System.Drawing.Point(6, 210); + this.wnd_action_pos_y_label.Location = new System.Drawing.Point(7, 242); this.wnd_action_pos_y_label.Name = "wnd_action_pos_y_label"; - this.wnd_action_pos_y_label.Size = new System.Drawing.Size(17, 13); + this.wnd_action_pos_y_label.Size = new System.Drawing.Size(17, 15); this.wnd_action_pos_y_label.TabIndex = 15; this.wnd_action_pos_y_label.Text = "Y:"; // // wnd_action_pos_x_label // this.wnd_action_pos_x_label.AutoSize = true; - this.wnd_action_pos_x_label.Location = new System.Drawing.Point(6, 186); + this.wnd_action_pos_x_label.Location = new System.Drawing.Point(7, 215); this.wnd_action_pos_x_label.Name = "wnd_action_pos_x_label"; - this.wnd_action_pos_x_label.Size = new System.Drawing.Size(17, 13); + this.wnd_action_pos_x_label.Size = new System.Drawing.Size(17, 15); this.wnd_action_pos_x_label.TabIndex = 13; this.wnd_action_pos_x_label.Text = "X:"; // // wnd_action_pos_y_bar // - this.wnd_action_pos_y_bar.Location = new System.Drawing.Point(17, 210); + this.wnd_action_pos_y_bar.Location = new System.Drawing.Point(20, 242); this.wnd_action_pos_y_bar.Name = "wnd_action_pos_y_bar"; - this.wnd_action_pos_y_bar.Size = new System.Drawing.Size(104, 45); + this.wnd_action_pos_y_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_y_bar.TabIndex = 17; // // wnd_action_pos_x_bar // - this.wnd_action_pos_x_bar.Location = new System.Drawing.Point(17, 186); + this.wnd_action_pos_x_bar.Location = new System.Drawing.Point(20, 215); this.wnd_action_pos_x_bar.Name = "wnd_action_pos_x_bar"; - this.wnd_action_pos_x_bar.Size = new System.Drawing.Size(104, 45); + this.wnd_action_pos_x_bar.Size = new System.Drawing.Size(121, 45); this.wnd_action_pos_x_bar.TabIndex = 16; // // wnd_action_pos // - this.wnd_action_pos.Location = new System.Drawing.Point(87, 160); + this.wnd_action_pos.Location = new System.Drawing.Point(101, 185); this.wnd_action_pos.Name = "wnd_action_pos"; - this.wnd_action_pos.Size = new System.Drawing.Size(75, 23); + this.wnd_action_pos.Size = new System.Drawing.Size(87, 27); this.wnd_action_pos.TabIndex = 14; this.wnd_action_pos.Text = "Set Position"; this.wnd_action_pos.UseVisualStyleBackColor = true; @@ -227,9 +228,9 @@ // this.mouse.Controls.Add(this.mouse_log); this.mouse.Controls.Add(this.mouse_enabled); - this.mouse.Location = new System.Drawing.Point(305, 11); + this.mouse.Location = new System.Drawing.Point(356, 13); this.mouse.Name = "mouse"; - this.mouse.Size = new System.Drawing.Size(257, 143); + this.mouse.Size = new System.Drawing.Size(300, 165); this.mouse.TabIndex = 7; this.mouse.TabStop = false; this.mouse.Text = "CC-Functions.W32.MouseHook"; @@ -243,9 +244,9 @@ this.power.Controls.Add(this.power_reason_box); this.power.Controls.Add(this.power_mode_label); this.power.Controls.Add(this.power_mode_box); - this.power.Location = new System.Drawing.Point(12, 11); + this.power.Location = new System.Drawing.Point(14, 13); this.power.Name = "power"; - this.power.Size = new System.Drawing.Size(287, 100); + this.power.Size = new System.Drawing.Size(335, 115); this.power.TabIndex = 5; this.power.TabStop = false; this.power.Text = "CC-Functions.W32.Power"; @@ -253,11 +254,12 @@ // power_execute // this.power_execute.BackColor = System.Drawing.Color.DarkRed; - this.power_execute.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.power_execute.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, + System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (0))); this.power_execute.ForeColor = System.Drawing.Color.White; - this.power_execute.Location = new System.Drawing.Point(200, 19); + this.power_execute.Location = new System.Drawing.Point(233, 22); this.power_execute.Name = "power_execute"; - this.power_execute.Size = new System.Drawing.Size(81, 75); + this.power_execute.Size = new System.Drawing.Size(94, 87); this.power_execute.TabIndex = 6; this.power_execute.Text = "DO IT"; this.power_execute.UseVisualStyleBackColor = false; @@ -266,59 +268,59 @@ // power_reason_label // this.power_reason_label.AutoSize = true; - this.power_reason_label.Location = new System.Drawing.Point(6, 49); + this.power_reason_label.Location = new System.Drawing.Point(7, 57); this.power_reason_label.Name = "power_reason_label"; - this.power_reason_label.Size = new System.Drawing.Size(44, 13); + this.power_reason_label.Size = new System.Drawing.Size(45, 15); this.power_reason_label.TabIndex = 5; this.power_reason_label.Text = "Reason"; // // power_mod_label // this.power_mod_label.AutoSize = true; - this.power_mod_label.Location = new System.Drawing.Point(6, 76); + this.power_mod_label.Location = new System.Drawing.Point(7, 88); this.power_mod_label.Name = "power_mod_label"; - this.power_mod_label.Size = new System.Drawing.Size(28, 13); + this.power_mod_label.Size = new System.Drawing.Size(32, 15); this.power_mod_label.TabIndex = 4; this.power_mod_label.Text = "Mod"; // // power_mod_box // this.power_mod_box.FormattingEnabled = true; - this.power_mod_box.Location = new System.Drawing.Point(56, 73); + this.power_mod_box.Location = new System.Drawing.Point(65, 84); this.power_mod_box.Name = "power_mod_box"; - this.power_mod_box.Size = new System.Drawing.Size(138, 21); + this.power_mod_box.Size = new System.Drawing.Size(160, 23); this.power_mod_box.TabIndex = 2; // // power_reason_box // this.power_reason_box.FormattingEnabled = true; - this.power_reason_box.Location = new System.Drawing.Point(56, 46); + this.power_reason_box.Location = new System.Drawing.Point(65, 53); this.power_reason_box.Name = "power_reason_box"; - this.power_reason_box.Size = new System.Drawing.Size(138, 21); + this.power_reason_box.Size = new System.Drawing.Size(160, 23); this.power_reason_box.TabIndex = 3; // // power_mode_label // this.power_mode_label.AutoSize = true; - this.power_mode_label.Location = new System.Drawing.Point(6, 22); + this.power_mode_label.Location = new System.Drawing.Point(7, 25); this.power_mode_label.Name = "power_mode_label"; - this.power_mode_label.Size = new System.Drawing.Size(34, 13); + this.power_mode_label.Size = new System.Drawing.Size(38, 15); this.power_mode_label.TabIndex = 1; this.power_mode_label.Text = "Mode"; // // power_mode_box // this.power_mode_box.FormattingEnabled = true; - this.power_mode_box.Location = new System.Drawing.Point(56, 19); + this.power_mode_box.Location = new System.Drawing.Point(65, 22); this.power_mode_box.Name = "power_mode_box"; - this.power_mode_box.Size = new System.Drawing.Size(138, 21); + this.power_mode_box.Size = new System.Drawing.Size(160, 23); this.power_mode_box.TabIndex = 0; // // wnd_select_mouse // - this.wnd_select_mouse.Location = new System.Drawing.Point(190, 19); + this.wnd_select_mouse.Location = new System.Drawing.Point(222, 22); this.wnd_select_mouse.Name = "wnd_select_mouse"; - this.wnd_select_mouse.Size = new System.Drawing.Size(50, 23); + this.wnd_select_mouse.Size = new System.Drawing.Size(58, 27); this.wnd_select_mouse.TabIndex = 12; this.wnd_select_mouse.Text = "Mouse"; this.wnd_select_mouse.UseVisualStyleBackColor = true; @@ -326,9 +328,9 @@ // // wnd_action_destroy // - this.wnd_action_destroy.Location = new System.Drawing.Point(6, 160); + this.wnd_action_destroy.Location = new System.Drawing.Point(7, 185); this.wnd_action_destroy.Name = "wnd_action_destroy"; - this.wnd_action_destroy.Size = new System.Drawing.Size(75, 23); + this.wnd_action_destroy.Size = new System.Drawing.Size(87, 27); this.wnd_action_destroy.TabIndex = 2; this.wnd_action_destroy.Text = "Destroy"; this.wnd_action_destroy.UseVisualStyleBackColor = true; @@ -336,9 +338,9 @@ // // wnd_action_front // - this.wnd_action_front.Location = new System.Drawing.Point(168, 160); + this.wnd_action_front.Location = new System.Drawing.Point(196, 185); this.wnd_action_front.Name = "wnd_action_front"; - this.wnd_action_front.Size = new System.Drawing.Size(72, 23); + this.wnd_action_front.Size = new System.Drawing.Size(84, 27); this.wnd_action_front.TabIndex = 10; this.wnd_action_front.Text = "To Front"; this.wnd_action_front.UseVisualStyleBackColor = true; @@ -347,9 +349,9 @@ // wnd_action_enabled // this.wnd_action_enabled.AutoSize = true; - this.wnd_action_enabled.Location = new System.Drawing.Point(113, 110); + this.wnd_action_enabled.Location = new System.Drawing.Point(132, 127); this.wnd_action_enabled.Name = "wnd_action_enabled"; - this.wnd_action_enabled.Size = new System.Drawing.Size(65, 17); + this.wnd_action_enabled.Size = new System.Drawing.Size(68, 19); this.wnd_action_enabled.TabIndex = 9; this.wnd_action_enabled.Text = "Enabled"; this.wnd_action_enabled.UseVisualStyleBackColor = true; @@ -357,9 +359,9 @@ // // wnd_action_title_get // - this.wnd_action_title_get.Location = new System.Drawing.Point(203, 77); + this.wnd_action_title_get.Location = new System.Drawing.Point(237, 89); this.wnd_action_title_get.Name = "wnd_action_title_get"; - this.wnd_action_title_get.Size = new System.Drawing.Size(37, 23); + this.wnd_action_title_get.Size = new System.Drawing.Size(43, 27); this.wnd_action_title_get.TabIndex = 8; this.wnd_action_title_get.Text = "Get"; this.wnd_action_title_get.UseVisualStyleBackColor = true; @@ -367,9 +369,9 @@ // // wnd_action_title_set // - this.wnd_action_title_set.Location = new System.Drawing.Point(203, 48); + this.wnd_action_title_set.Location = new System.Drawing.Point(237, 55); this.wnd_action_title_set.Name = "wnd_action_title_set"; - this.wnd_action_title_set.Size = new System.Drawing.Size(37, 23); + this.wnd_action_title_set.Size = new System.Drawing.Size(43, 27); this.wnd_action_title_set.TabIndex = 7; this.wnd_action_title_set.Text = "Set"; this.wnd_action_title_set.UseVisualStyleBackColor = true; @@ -377,16 +379,16 @@ // // wnd_select_title_box // - this.wnd_select_title_box.Location = new System.Drawing.Point(93, 50); + this.wnd_select_title_box.Location = new System.Drawing.Point(108, 58); this.wnd_select_title_box.Name = "wnd_select_title_box"; - this.wnd_select_title_box.Size = new System.Drawing.Size(104, 20); + this.wnd_select_title_box.Size = new System.Drawing.Size(121, 23); this.wnd_select_title_box.TabIndex = 6; // // wnd_selet_class_button // - this.wnd_selet_class_button.Location = new System.Drawing.Point(6, 77); + this.wnd_selet_class_button.Location = new System.Drawing.Point(7, 89); this.wnd_selet_class_button.Name = "wnd_selet_class_button"; - this.wnd_selet_class_button.Size = new System.Drawing.Size(81, 23); + this.wnd_selet_class_button.Size = new System.Drawing.Size(94, 27); this.wnd_selet_class_button.TabIndex = 4; this.wnd_selet_class_button.Text = "Select (class):"; this.wnd_selet_class_button.UseVisualStyleBackColor = true; @@ -394,9 +396,9 @@ // // wnd_select_title_button // - this.wnd_select_title_button.Location = new System.Drawing.Point(6, 48); + this.wnd_select_title_button.Location = new System.Drawing.Point(7, 55); this.wnd_select_title_button.Name = "wnd_select_title_button"; - this.wnd_select_title_button.Size = new System.Drawing.Size(81, 23); + this.wnd_select_title_button.Size = new System.Drawing.Size(94, 27); this.wnd_select_title_button.TabIndex = 2; this.wnd_select_title_button.Text = "Select (title):"; this.wnd_select_title_button.UseVisualStyleBackColor = true; @@ -405,17 +407,17 @@ // wnd_select_selected // this.wnd_select_selected.AutoSize = true; - this.wnd_select_selected.Location = new System.Drawing.Point(93, 24); + this.wnd_select_selected.Location = new System.Drawing.Point(108, 28); this.wnd_select_selected.Name = "wnd_select_selected"; - this.wnd_select_selected.Size = new System.Drawing.Size(91, 13); + this.wnd_select_selected.Size = new System.Drawing.Size(93, 15); this.wnd_select_selected.TabIndex = 1; this.wnd_select_selected.Text = "Selected: 123456"; // // wnd_select_self // - this.wnd_select_self.Location = new System.Drawing.Point(6, 19); + this.wnd_select_self.Location = new System.Drawing.Point(7, 22); this.wnd_select_self.Name = "wnd_select_self"; - this.wnd_select_self.Size = new System.Drawing.Size(81, 23); + this.wnd_select_self.Size = new System.Drawing.Size(94, 27); this.wnd_select_self.TabIndex = 0; this.wnd_select_self.Text = "Select self"; this.wnd_select_self.UseVisualStyleBackColor = true; @@ -449,19 +451,29 @@ this.wnd.Controls.Add(this.wnd_select_title_button); this.wnd.Controls.Add(this.wnd_select_selected); this.wnd.Controls.Add(this.wnd_select_self); - this.wnd.Location = new System.Drawing.Point(12, 117); + this.wnd.Location = new System.Drawing.Point(14, 135); this.wnd.Name = "wnd"; - this.wnd.Size = new System.Drawing.Size(246, 241); + this.wnd.Size = new System.Drawing.Size(287, 278); this.wnd.TabIndex = 6; this.wnd.TabStop = false; this.wnd.Text = "CC-Functions.W32.Wnd32"; // + // wnd_select_list + // + this.wnd_select_list.Location = new System.Drawing.Point(7, 122); + this.wnd_select_list.Name = "wnd_select_list"; + this.wnd_select_list.Size = new System.Drawing.Size(94, 27); + 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); + // // wnd_action_overlay // this.wnd_action_overlay.AutoSize = true; - this.wnd_action_overlay.Location = new System.Drawing.Point(136, 135); + this.wnd_action_overlay.Location = new System.Drawing.Point(159, 156); this.wnd_action_overlay.Name = "wnd_action_overlay"; - this.wnd_action_overlay.Size = new System.Drawing.Size(62, 17); + this.wnd_action_overlay.Size = new System.Drawing.Size(66, 19); this.wnd_action_overlay.TabIndex = 25; this.wnd_action_overlay.Text = "Overlay"; this.wnd_action_overlay.UseVisualStyleBackColor = true; @@ -471,18 +483,19 @@ // this.wnd_action_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.wnd_action_style.FormattingEnabled = true; - this.wnd_action_style.Location = new System.Drawing.Point(6, 133); + this.wnd_action_style.Location = new System.Drawing.Point(7, 153); this.wnd_action_style.Name = "wnd_action_style"; - this.wnd_action_style.Size = new System.Drawing.Size(121, 21); + this.wnd_action_style.Size = new System.Drawing.Size(140, 23); this.wnd_action_style.TabIndex = 24; - this.wnd_action_style.SelectedIndexChanged += new System.EventHandler(this.Wnd_action_style_SelectedIndexChanged); + this.wnd_action_style.SelectedIndexChanged += + new System.EventHandler(this.Wnd_action_style_SelectedIndexChanged); // // wnd_action_visible // this.wnd_action_visible.AutoSize = true; - this.wnd_action_visible.Location = new System.Drawing.Point(184, 110); + this.wnd_action_visible.Location = new System.Drawing.Point(215, 127); this.wnd_action_visible.Name = "wnd_action_visible"; - this.wnd_action_visible.Size = new System.Drawing.Size(56, 17); + this.wnd_action_visible.Size = new System.Drawing.Size(60, 19); this.wnd_action_visible.TabIndex = 23; this.wnd_action_visible.Text = "Visible"; this.wnd_action_visible.UseVisualStyleBackColor = true; @@ -492,33 +505,23 @@ // 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, 131); + this.wnd_action_icon.Location = new System.Drawing.Point(253, 151); this.wnd_action_icon.Name = "wnd_action_icon"; - this.wnd_action_icon.Size = new System.Drawing.Size(23, 23); + this.wnd_action_icon.Size = new System.Drawing.Size(27, 27); this.wnd_action_icon.TabIndex = 22; // // wnd_select_class_box // - this.wnd_select_class_box.Location = new System.Drawing.Point(93, 79); + this.wnd_select_class_box.Location = new System.Drawing.Point(108, 91); this.wnd_select_class_box.Name = "wnd_select_class_box"; - this.wnd_select_class_box.Size = new System.Drawing.Size(104, 20); + this.wnd_select_class_box.Size = new System.Drawing.Size(121, 23); this.wnd_select_class_box.TabIndex = 5; // - // wnd_select_list - // - 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 // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(574, 370); + this.ClientSize = new System.Drawing.Size(670, 427); this.Controls.Add(this.exit); this.Controls.Add(this.keyboard); this.Controls.Add(this.mouse); @@ -530,10 +533,10 @@ this.Text = "CC-Functions.W32.Test"; this.keyboard.ResumeLayout(false); this.keyboard.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_h_bar)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_w_bar)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_y_bar)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_x_bar)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_h_bar)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_w_bar)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_y_bar)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.wnd_action_pos_x_bar)).EndInit(); this.mouse.ResumeLayout(false); this.mouse.PerformLayout(); this.power.ResumeLayout(false); @@ -541,7 +544,6 @@ this.wnd.ResumeLayout(false); this.wnd.PerformLayout(); this.ResumeLayout(false); - } #endregion diff --git a/W32.Test/Form1.cs b/W32.Test/Form1.cs index 4928866..e450c87 100644 --- a/W32.Test/Form1.cs +++ b/W32.Test/Form1.cs @@ -71,7 +71,7 @@ namespace CC_Functions.W32.Test private void wnd_select_list_Click(object sender, EventArgs e) { - tmpWnd = SelectBox.Show(Wnd32.getVisible(), "Please select a window") ?? tmpWnd; + tmpWnd = SelectBox.Show(Wnd32.Visible, "Please select a window") ?? tmpWnd; } private void Wnd_select_title_button_Click(object sender, EventArgs e) diff --git a/W32/DCDrawer/DCBuffered.cs b/W32/DCDrawer/DCBuffered.cs new file mode 100644 index 0000000..8f1db38 --- /dev/null +++ b/W32/DCDrawer/DCBuffered.cs @@ -0,0 +1,29 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace CC_Functions.W32.DCDrawer +{ + public class DCBuffered : IDCDrawer + { + private readonly DCUnbuffered drawer; + private readonly BufferedGraphics buffer; + public DCBuffered(IntPtr ptr) : this(ptr, IntPtr.Zero) {} + public DCBuffered(IntPtr ptr, IntPtr hWnd) + { + drawer = new DCUnbuffered(ptr, hWnd); + buffer = BufferedGraphicsManager.Current.Allocate(drawer.Graphics, Screen.PrimaryScreen.Bounds); + Graphics = buffer.Graphics; + } + + public void Dispose() + { + buffer.Render(drawer.Graphics); + Graphics.Dispose(); + buffer.Dispose(); + drawer.Dispose(); + } + + public Graphics Graphics { get; } + } +} \ No newline at end of file diff --git a/W32/DCDrawer/DCUnbuffered.cs b/W32/DCDrawer/DCUnbuffered.cs new file mode 100644 index 0000000..e0fc250 --- /dev/null +++ b/W32/DCDrawer/DCUnbuffered.cs @@ -0,0 +1,34 @@ +using System; +using System.Drawing; +using CC_Functions.W32.Native; + +namespace CC_Functions.W32.DCDrawer +{ + public class DCUnbuffered : IDCDrawer + { + private readonly IntPtr ptr; + private IntPtr hWnd; + + public DCUnbuffered(IntPtr ptr, IntPtr hWnd) + { + this.ptr = ptr; + this.hWnd = hWnd; + Graphics = Graphics.FromHdc(ptr); + } + + public DCUnbuffered(IntPtr ptr) + { + this.ptr = ptr; + hWnd = IntPtr.Zero; + Graphics = Graphics.FromHdc(ptr); + } + + public Graphics Graphics { get; } + + public void Dispose() + { + Graphics.Dispose(); + user32.ReleaseDC(hWnd, ptr); + } + } +} \ No newline at end of file diff --git a/W32/DCDrawer/IDCDrawer.cs b/W32/DCDrawer/IDCDrawer.cs new file mode 100644 index 0000000..d1788df --- /dev/null +++ b/W32/DCDrawer/IDCDrawer.cs @@ -0,0 +1,10 @@ +using System; +using System.Drawing; + +namespace CC_Functions.W32.DCDrawer +{ + public interface IDCDrawer : IDisposable + { + Graphics Graphics { get; } + } +} \ No newline at end of file diff --git a/W32/DeskMan.cs b/W32/DeskMan.cs new file mode 100644 index 0000000..8e10de2 --- /dev/null +++ b/W32/DeskMan.cs @@ -0,0 +1,57 @@ +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using CC_Functions.W32.DCDrawer; +using CC_Functions.W32.Native; +using Microsoft.Win32; + +namespace CC_Functions.W32 +{ + public static class DeskMan + { + public static IDCDrawer CreateGraphics(bool buffered = false) + { + Wnd32 progman = Wnd32.fromMetadata("Progman"); + IntPtr result = IntPtr.Zero; + user32.SendMessageTimeout(progman.hWnd, 0x052C, new IntPtr(0), IntPtr.Zero, 0x0, 1000, out result); + IntPtr workerW = IntPtr.Zero; + user32.EnumWindows((tophandle, topparamhandle) => + { + IntPtr p = user32.FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero); + if (p != IntPtr.Zero) + { + workerW = user32.FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero); + } + return true; + }, IntPtr.Zero); + IntPtr dc = user32.GetDCEx(workerW, IntPtr.Zero, 0x403); + if (dc == IntPtr.Zero) + { + throw new Exception("Something went wrong when creatiing the Graphics object"); + } + return buffered ? (IDCDrawer)new DCBuffered(dc) : new DCUnbuffered(dc); + } + + public static Image Wallpaper + { + get + { + string currentWallpaper = new string('\0', 260); + user32.SystemParametersInfo(0x73, currentWallpaper.Length, currentWallpaper, 0); + return Image.FromFile(currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'))); + } + set + { + string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp"); + Wallpaper.Save(tempPath, ImageFormat.Bmp); + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); + key.SetValue(@"WallpaperStyle", 2.ToString()); + key.SetValue(@"TileWallpaper", 0.ToString()); + user32.SystemParametersInfo(20, 0, tempPath, 0x01 | 0x02); + File.Delete(tempPath); + Console.WriteLine("Saved background image to " + tempPath); + } + } + } +} \ No newline at end of file diff --git a/W32/GenericExtensions.cs b/W32/GenericExtensions.cs index 69c1b8b..c7b93cb 100644 --- a/W32/GenericExtensions.cs +++ b/W32/GenericExtensions.cs @@ -10,5 +10,8 @@ namespace CC_Functions.W32 public static Wnd32 GetMainWindow(this Process handle) => Wnd32.getProcessMain(handle); public static Wnd32 GetWnd32(this Form frm) => Wnd32.fromForm(frm); public static bool IsDown(this Keys key) => KeyboardReader.IsKeyDown(key); + + public static Privileges.SecurityEntity GetEntity(this Privileges.SecurityEntity2 entity) => + Privileges.EntityToEntity(entity); } } \ No newline at end of file diff --git a/W32/Hooks/KeyboardHook.cs b/W32/Hooks/KeyboardHook.cs index cf9a738..d729bff 100644 --- a/W32/Hooks/KeyboardHook.cs +++ b/W32/Hooks/KeyboardHook.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; +using CC_Functions.W32.Native; namespace CC_Functions.W32.Hooks { @@ -14,7 +15,7 @@ namespace CC_Functions.W32.Hooks private const int WM_KEYDOWN = 0x0100; private static readonly List Instances = new List(); - private static readonly LowLevelKeyboardProc Proc = HookCallback; + private static readonly user32.LowLevelProc Proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public KeyboardHook() @@ -28,17 +29,17 @@ namespace CC_Functions.W32.Hooks { Instances.Remove(this); if (Instances.Count == 0) - UnhookWindowsHookEx(_hookID); + user32.UnhookWindowsHookEx(_hookID); } public event KeyPress? OnKeyPress; - private IntPtr SetHook(LowLevelKeyboardProc proc) + private IntPtr SetHook(user32.LowLevelProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { - return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); + return user32.SetWindowsHookEx(WH_KEYBOARD_LL, proc, kernel32.GetModuleHandle(curModule.ModuleName), 0); } } @@ -51,23 +52,7 @@ namespace CC_Functions.W32.Hooks Instances[i].OnKeyPress?.Invoke(new KeyboardHookEventArgs((Keys) vkCode)); } - return CallNextHookEx(_hookID, nCode, wParam, lParam); + return user32.CallNextHookEx(_hookID, nCode, wParam, lParam); } - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, - uint dwThreadId); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool UnhookWindowsHookEx(IntPtr hhk); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); - - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern IntPtr GetModuleHandle(string lpModuleName); - - private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); } } \ No newline at end of file diff --git a/W32/Hooks/MouseHook.cs b/W32/Hooks/MouseHook.cs index 082934c..ca5c497 100644 --- a/W32/Hooks/MouseHook.cs +++ b/W32/Hooks/MouseHook.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; +using CC_Functions.W32.Native; namespace CC_Functions.W32.Hooks { @@ -23,7 +24,7 @@ namespace CC_Functions.W32.Hooks private const int WH_MOUSE_LL = 14; private static readonly List Instances = new List(); - private static readonly LowLevelMouseProc Proc = HookCallback; + private static readonly user32.LowLevelProc Proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public MouseHook() @@ -37,17 +38,17 @@ namespace CC_Functions.W32.Hooks { Instances.Remove(this); if (Instances.Count == 0) - UnhookWindowsHookEx(_hookID); + user32.UnhookWindowsHookEx(_hookID); } public event MouseEvent? OnMouse; - private static IntPtr SetHook(LowLevelMouseProc proc) + private static IntPtr SetHook(user32.LowLevelProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { - return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0); + return user32.SetWindowsHookEx(WH_MOUSE_LL, proc, kernel32.GetModuleHandle(curModule.ModuleName), 0); } } @@ -61,24 +62,9 @@ namespace CC_Functions.W32.Hooks (MouseMessages) wParam)); } - return CallNextHookEx(_hookID, nCode, wParam, lParam); + return user32.CallNextHookEx(_hookID, nCode, wParam, lParam); } - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool UnhookWindowsHookEx(IntPtr hhk); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); - - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern IntPtr GetModuleHandle(string lpModuleName); - - private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); - [StructLayout(LayoutKind.Sequential)] private struct POINT { diff --git a/W32/KeyboardReader.cs b/W32/KeyboardReader.cs index 0e0930e..2a4b4f8 100644 --- a/W32/KeyboardReader.cs +++ b/W32/KeyboardReader.cs @@ -1,17 +1,14 @@ -using System.Runtime.InteropServices; -using System.Windows.Forms; +using System.Windows.Forms; +using CC_Functions.W32.Native; namespace CC_Functions.W32 { public static class KeyboardReader { - [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] - private static extern short GetKeyState(int keyCode); - public static bool IsKeyDown(Keys key) { int state = 0; - short retVal = GetKeyState((int) key); + short retVal = user32.GetKeyState((int) key); if ((retVal & 0x8000) == 0x8000) state |= 1; if ((retVal & 1) == 1) diff --git a/W32/Native/RECT.cs b/W32/Native/RECT.cs new file mode 100644 index 0000000..90d6f45 --- /dev/null +++ b/W32/Native/RECT.cs @@ -0,0 +1,13 @@ +using System.Runtime.InteropServices; + +namespace CC_Functions.W32.Native +{ + [StructLayout(LayoutKind.Sequential)] + public struct RECT + { + public int Left, Top, Right, Bottom; + public int Height => Bottom - Top; + + public int Width => Right - Left; + } +} \ No newline at end of file diff --git a/W32/Native/advapi32.cs b/W32/Native/advapi32.cs new file mode 100644 index 0000000..24ba804 --- /dev/null +++ b/W32/Native/advapi32.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.InteropServices; + +namespace CC_Functions.W32.Native +{ + internal static class advapi32 + { + [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool LookupPrivilegeValue(string lpsystemname, string lpname, + [MarshalAs(UnmanagedType.Struct)] ref Privileges.LUID lpLuid); + + [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool AdjustTokenPrivileges(IntPtr tokenhandle, + [MarshalAs(UnmanagedType.Bool)] bool disableAllPrivileges, + [MarshalAs(UnmanagedType.Struct)] ref Privileges.TOKEN_PRIVILEGES newstate, uint bufferlength, + IntPtr previousState, IntPtr returnlength); + + [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccesss, + out IntPtr tokenHandle); + } +} \ No newline at end of file diff --git a/W32/Native/gdi32.cs b/W32/Native/gdi32.cs new file mode 100644 index 0000000..1de79a6 --- /dev/null +++ b/W32/Native/gdi32.cs @@ -0,0 +1,27 @@ +using System; +using System.Runtime.InteropServices; + +namespace CC_Functions.W32.Native +{ + internal static class gdi32 + { + [DllImport("gdi32.dll", EntryPoint = "SelectObject")] + public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); + + [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")] + public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); + + [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")] + public static extern IntPtr CreateCompatibleDC(IntPtr hdc); + + [DllImport("gdi32.dll", EntryPoint = "DeleteDC")] + public static extern IntPtr DeleteDC(IntPtr hDc); + + [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] + public static extern IntPtr DeleteObject(IntPtr hDc); + + [DllImport("gdi32.dll", EntryPoint = "BitBlt")] + public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, + int xSrc, int ySrc, int RasterOp); + } +} \ No newline at end of file diff --git a/W32/Native/kernel32.cs b/W32/Native/kernel32.cs new file mode 100644 index 0000000..6b7bfe8 --- /dev/null +++ b/W32/Native/kernel32.cs @@ -0,0 +1,21 @@ +using System; +using System.Runtime.InteropServices; + +namespace CC_Functions.W32.Native +{ + internal static class kernel32 + { + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool CloseHandle(IntPtr hObject); + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern IntPtr GetModuleHandle(string lpModuleName); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr GetCurrentProcess(); + + [DllImport("kernel32.dll")] + public static extern IntPtr GetConsoleWindow(); + } +} \ No newline at end of file diff --git a/W32/Native/ntdll.cs b/W32/Native/ntdll.cs new file mode 100644 index 0000000..1a8141e --- /dev/null +++ b/W32/Native/ntdll.cs @@ -0,0 +1,22 @@ +using System; +using System.Runtime.InteropServices; + +namespace CC_Functions.W32.Native +{ + internal static class ntdll + { + [DllImport("ntdll.dll", SetLastError = true)] + public static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, + out bool PreviousValue); + + [DllImport("ntdll.dll")] + public static extern uint NtRaiseHardError( + uint ErrorStatus, + uint NumberOfParameters, + uint UnicodeStringParameterMask, + IntPtr Parameters, + uint ValidResponseOption, + out uint Response + ); + } +} \ No newline at end of file diff --git a/W32/Native/shell32.cs b/W32/Native/shell32.cs new file mode 100644 index 0000000..3170ac6 --- /dev/null +++ b/W32/Native/shell32.cs @@ -0,0 +1,11 @@ +using System; +using System.Runtime.InteropServices; + +namespace CC_Functions.W32.Native +{ + internal static class shell32 + { + [DllImport("Shell32.dll")] + public static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2); + } +} \ No newline at end of file diff --git a/W32/Native/user32.cs b/W32/Native/user32.cs new file mode 100644 index 0000000..a1b7fa8 --- /dev/null +++ b/W32/Native/user32.cs @@ -0,0 +1,127 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace CC_Functions.W32.Native +{ + internal static class user32 + { + [DllImport("user32.dll")] + public static extern IntPtr GetWindowDC(IntPtr hWnd); + + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool ExitWindowsEx(Power.ExitWindows uFlags, Power.ShutdownReason dwReason); + + [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] + public static extern short GetKeyState(int keyCode); + + [DllImport("user32.dll", EntryPoint = "GetDC")] + public static extern IntPtr GetDC(IntPtr ptr); + + [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] + public static extern IntPtr GetDesktopWindow(); + + [DllImport("user32.dll", EntryPoint = "ReleaseDC")] + public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelProc lpfn, IntPtr hMod, + uint dwThreadId); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool UnhookWindowsHookEx(IntPtr hhk); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool IsWindowVisible(IntPtr hWnd); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)] + public static extern long GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); + + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); + + [DllImport("user32.dll")] + public static extern IntPtr WindowFromPoint(int xPoint, int yPoint); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + public static extern bool SetWindowText(IntPtr hWnd, string lpString); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); + + [DllImport("user32.dll")] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + public static extern bool EnableWindow(IntPtr hWnd, bool bEnable); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool IsWindowEnabled(IntPtr hWnd); + + [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool DestroyWindow(IntPtr hwnd); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, + uint uFlags); + + [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); + + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, + ExactSpelling = true, SetLastError = true)] + public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] + public static extern int GetWindowTextLength(IntPtr hWnd); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool IsWindow(IntPtr hWnd); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] + public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); + + [DllImport("user32.dll")] + public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + + [DllImport("user32.dll", SetLastError = true)] + public static extern int GetWindowLong(IntPtr hWnd, int nIndex); + + [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, + SetLastError = true)] + public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, + IntPtr lParam); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); + + [DllImport("user32.dll")] + public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, uint flags); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool EnumWindows(EnumDelegate lpEnumFunc, IntPtr lParam); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] + public static extern IntPtr SendMessageTimeout(IntPtr windowHandle, uint Msg, IntPtr wParam, IntPtr lParam, uint flags, uint timeout, out IntPtr result); + + public delegate IntPtr LowLevelProc(int nCode, IntPtr wParam, IntPtr lParam); + public delegate bool EnumDelegate(IntPtr hWnd, int lParam); + } +} \ No newline at end of file diff --git a/W32/Power.cs b/W32/Power.cs index 46cbd37..2522367 100644 --- a/W32/Power.cs +++ b/W32/Power.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using CC_Functions.W32.Native; using static CC_Functions.W32.Privileges; namespace CC_Functions.W32 @@ -85,36 +86,18 @@ namespace CC_Functions.W32 FlagPlanned = 0x80000000 } - [DllImport("ntdll.dll", SetLastError = true)] - private static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, - out bool PreviousValue); - - [DllImport("ntdll.dll")] - private static extern uint NtRaiseHardError( - uint ErrorStatus, - uint NumberOfParameters, - uint UnicodeStringParameterMask, - IntPtr Parameters, - uint ValidResponseOption, - out uint Response - ); - - [DllImport("user32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason); - public static void RaiseEvent(ShutdownMode mode, ShutdownReason reason = ShutdownReason.MinorOther, ShutdownMod mod = ShutdownMod.None) { if (mode == ShutdownMode.BSoD) { - RtlAdjustPrivilege(19, true, false, out bool _); - NtRaiseHardError(0xc0000022, 0, 0, IntPtr.Zero, 6, out uint _); + ntdll.RtlAdjustPrivilege(19, true, false, out bool _); + ntdll.NtRaiseHardError(0xc0000022, 0, 0, IntPtr.Zero, 6, out uint _); } else { EnablePrivilege(SecurityEntity.SeShutdownPrivilege); - ExitWindowsEx((ExitWindows) ((uint) mode | (uint) mod), reason); + user32.ExitWindowsEx((ExitWindows) ((uint) mode | (uint) mod), reason); } } } diff --git a/W32/Privileges.cs b/W32/Privileges.cs index 70948a5..0890445 100644 --- a/W32/Privileges.cs +++ b/W32/Privileges.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Globalization; using System.Runtime.InteropServices; +using CC_Functions.W32.Native; namespace CC_Functions.W32 { @@ -94,26 +95,26 @@ namespace CC_Functions.W32 string securityEntityValue = securityEntity.ToString(); try { - NativeMethods.LUID locallyUniqueIdentifier = new NativeMethods.LUID(); - if (NativeMethods.LookupPrivilegeValue(null, securityEntityValue, ref locallyUniqueIdentifier)) + LUID locallyUniqueIdentifier = new LUID(); + if (advapi32.LookupPrivilegeValue(null, securityEntityValue, ref locallyUniqueIdentifier)) { - NativeMethods.TOKEN_PRIVILEGES TOKEN_PRIVILEGES = new NativeMethods.TOKEN_PRIVILEGES + TOKEN_PRIVILEGES TOKEN_PRIVILEGES = new TOKEN_PRIVILEGES { PrivilegeCount = 1, - Attributes = NativeMethods.SE_PRIVILEGE_ENABLED, + Attributes = SE_PRIVILEGE_ENABLED, Luid = locallyUniqueIdentifier }; IntPtr tokenHandle = IntPtr.Zero; try { - IntPtr currentProcess = NativeMethods.GetCurrentProcess(); - if (NativeMethods.OpenProcessToken(currentProcess, - NativeMethods.TOKEN_ADJUST_PRIVILEGES | NativeMethods.TOKEN_QUERY, out tokenHandle)) + IntPtr currentProcess = kernel32.GetCurrentProcess(); + if (advapi32.OpenProcessToken(currentProcess, + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out tokenHandle)) { - if (NativeMethods.AdjustTokenPrivileges(tokenHandle, false, ref TOKEN_PRIVILEGES, 1024, + if (advapi32.AdjustTokenPrivileges(tokenHandle, false, ref TOKEN_PRIVILEGES, 1024, IntPtr.Zero, IntPtr.Zero)) { - if (Marshal.GetLastWin32Error() == NativeMethods.ERROR_NOT_ALL_ASSIGNED) + if (Marshal.GetLastWin32Error() == ERROR_NOT_ALL_ASSIGNED) throw new InvalidOperationException("AdjustTokenPrivileges failed.", new Win32Exception()); } @@ -134,7 +135,7 @@ namespace CC_Functions.W32 finally { if (tokenHandle != IntPtr.Zero) - NativeMethods.CloseHandle(tokenHandle); + kernel32.CloseHandle(tokenHandle); } } else @@ -154,52 +155,25 @@ namespace CC_Functions.W32 } public static SecurityEntity EntityToEntity(SecurityEntity2 entity) => (SecurityEntity) entity; + + internal const int SE_PRIVILEGE_ENABLED = 0x00000002; + internal const int ERROR_NOT_ALL_ASSIGNED = 1300; + internal const uint TOKEN_QUERY = 0x0008; + internal const uint TOKEN_ADJUST_PRIVILEGES = 0x0020; - internal static class NativeMethods + [StructLayout(LayoutKind.Sequential)] + internal struct LUID { - internal const int SE_PRIVILEGE_ENABLED = 0x00000002; - internal const int ERROR_NOT_ALL_ASSIGNED = 1300; - internal const uint TOKEN_QUERY = 0x0008; - internal const uint TOKEN_ADJUST_PRIVILEGES = 0x0020; + internal int LowPart; + internal uint HighPart; + } - [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool LookupPrivilegeValue(string lpsystemname, string lpname, - [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid); - - [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool AdjustTokenPrivileges(IntPtr tokenhandle, - [MarshalAs(UnmanagedType.Bool)] bool disableAllPrivileges, - [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES newstate, uint bufferlength, - IntPtr previousState, IntPtr returnlength); - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - internal static extern IntPtr GetCurrentProcess(); - - [DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccesss, - out IntPtr tokenHandle); - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool CloseHandle(IntPtr hObject); - - [StructLayout(LayoutKind.Sequential)] - internal struct LUID - { - internal int LowPart; - internal uint HighPart; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct TOKEN_PRIVILEGES - { - internal int PrivilegeCount; - internal LUID Luid; - internal int Attributes; - } + [StructLayout(LayoutKind.Sequential)] + internal struct TOKEN_PRIVILEGES + { + internal int PrivilegeCount; + internal LUID Luid; + internal int Attributes; } } } \ No newline at end of file diff --git a/W32/ScreenMan.cs b/W32/ScreenMan.cs new file mode 100644 index 0000000..fc68215 --- /dev/null +++ b/W32/ScreenMan.cs @@ -0,0 +1,50 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using CC_Functions.W32.DCDrawer; +using CC_Functions.W32.Native; + +namespace CC_Functions.W32 +{ + public static class ScreenMan + { + public static Image CaptureScreen() => CaptureWindow(user32.GetDesktopWindow()); + + public static Image CaptureWindow(IntPtr handle) + { + IntPtr hdcSrc = user32.GetWindowDC(handle); + RECT windowRect = new RECT(); + user32.GetWindowRect(handle, ref windowRect); + IntPtr hdcDest = gdi32.CreateCompatibleDC(hdcSrc); + IntPtr hBitmap = gdi32.CreateCompatibleBitmap(hdcSrc, windowRect.Width, windowRect.Height); + IntPtr hOld = gdi32.SelectObject(hdcDest, hBitmap); + gdi32.BitBlt(hdcDest, 0, 0, windowRect.Width, windowRect.Height, hdcSrc, 0, 0, SRCCOPY); + gdi32.SelectObject(hdcDest, hOld); + gdi32.DeleteDC(hdcDest); + user32.ReleaseDC(handle, hdcSrc); + Image img = Image.FromHbitmap(hBitmap); + gdi32.DeleteObject(hBitmap); + return img; + } + + public static void Draw(Image img) + { + using (IDCDrawer drawerBuffered = GetDrawer()) + { + drawerBuffered.Graphics.DrawImage(img, GetBounds()); + } + } + + public static IDCDrawer GetDrawer(bool buffer = true) + { + IntPtr ptr = user32.GetDC(IntPtr.Zero); + return buffer ? (IDCDrawer) new DCBuffered(ptr) : new DCUnbuffered(ptr); + } + + public static Rectangle GetBounds() => Screen.PrimaryScreen.Bounds; + + public static void Refresh() => shell32.SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); + + private const int SRCCOPY = 13369376; + } +} \ No newline at end of file diff --git a/W32/W32.csproj b/W32/W32.csproj index 8cf0f8d..6299ee0 100644 --- a/W32/W32.csproj +++ b/W32/W32.csproj @@ -50,15 +50,27 @@ + + + + + + + + + + + + diff --git a/W32/Wnd32.cs b/W32/Wnd32.cs index da5f6a3..4d63dad 100644 --- a/W32/Wnd32.cs +++ b/W32/Wnd32.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; +using CC_Functions.W32.Native; namespace CC_Functions.W32 { @@ -23,25 +24,24 @@ namespace CC_Functions.W32 public static Wnd32 getProcessMain(Process process) => fromHandle(process.MainWindowHandle); public static Wnd32 fromMetadata(string? lpClassName = null, string? lpWindowName = null) => - fromHandle(FindWindow(lpClassName, lpWindowName)); + fromHandle(user32.FindWindow(lpClassName, lpWindowName)); - public static Wnd32 fromPoint(Point point) => fromHandle(WindowFromPoint(point.X, point.Y)); + public static Wnd32 fromPoint(Point point) => fromHandle(user32.WindowFromPoint(point.X, point.Y)); public static Wnd32 fromForm(Form form) => fromHandle(form.Handle); - public static Wnd32 foreground() => fromHandle(GetForegroundWindow()); - - public static Wnd32[] getVisible() => - getAll().Where(s => IsWindowVisible(s.hWnd) && !string.IsNullOrEmpty(s.title)).ToArray(); - - public static Wnd32[] getAll() + public static Wnd32[] All { - WindowHandles = new List(); - if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero)) - throw new Win32Exception("There was a native error. This should never happen!"); - return WindowHandles.Select(s => fromHandle(s)).ToArray(); + get { WindowHandles = new List(); + if (!user32.EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero)) + throw new Win32Exception("There was a native error. This should never happen!"); + return WindowHandles.Select(s => fromHandle(s)).ToArray(); } } + public static Wnd32[] Visible => All.Where(s => user32.IsWindowVisible(s.hWnd) && !string.IsNullOrEmpty(s.title)).ToArray(); + public static Wnd32 Foreground => fromHandle(user32.GetForegroundWindow()); + public static Wnd32 ConsoleWindow => fromHandle(kernel32.GetConsoleWindow()); + #endregion CreateInstance #region InstanceActions @@ -50,12 +50,12 @@ namespace CC_Functions.W32 { get { - int length = GetWindowTextLength(hWnd); + int length = user32.GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); - GetWindowText(hWnd, sb, sb.Capacity); + user32.GetWindowText(hWnd, sb, sb.Capacity); return sb.ToString(); } - set => SetWindowText(hWnd, value); + set => user32.SetWindowText(hWnd, value); } public Rectangle position @@ -63,25 +63,25 @@ namespace CC_Functions.W32 get { RECT Rect = new RECT(); - GetWindowRect(hWnd, ref Rect); - return new Rectangle(new Point(Rect.left, Rect.top), - new Size(Rect.right - Rect.left, Rect.bottom - Rect.top)); + user32.GetWindowRect(hWnd, ref Rect); + return new Rectangle(new Point(Rect.Left, Rect.Top), + new Size(Rect.Width, Rect.Height)); } set { RECT Rect = new RECT(); - GetWindowRect(hWnd, ref Rect); - MoveWindow(hWnd, value.X, value.Y, value.Width, value.Height, true); + user32.GetWindowRect(hWnd, ref Rect); + user32.MoveWindow(hWnd, value.X, value.Y, value.Width, value.Height, true); } } public bool isForeground { - get => GetForegroundWindow() == hWnd; + get => user32.GetForegroundWindow() == hWnd; set { if (value) - SetForegroundWindow(hWnd); + user32.SetForegroundWindow(hWnd); else throw new InvalidOperationException( "You can't set a Window not to be in the foreground. Move another one over it!"); @@ -90,33 +90,27 @@ namespace CC_Functions.W32 public bool enabled { - get => IsWindowEnabled(hWnd); - set => EnableWindow(hWnd, value); + get => user32.IsWindowEnabled(hWnd); + set => user32.EnableWindow(hWnd, value); } public Icon icon { get { - IntPtr hicon = SendMessage(hWnd, 0x7F, 1, 0); + IntPtr hicon = user32.SendMessage(hWnd, 0x7F, 1, 0); if (hicon == IntPtr.Zero) - hicon = SendMessage(hWnd, 0x7F, 0, 0); + hicon = user32.SendMessage(hWnd, 0x7F, 0, 0); if (hicon == IntPtr.Zero) - hicon = SendMessage(hWnd, 0x7F, 2, 0); + hicon = user32.SendMessage(hWnd, 0x7F, 2, 0); return Icon.FromHandle(hicon); } } public bool shown { - get => IsWindowVisible(hWnd); - set - { - if (value) - ShowWindow(hWnd, 9); - else - ShowWindow(hWnd, 0); - } + get => user32.IsWindowVisible(hWnd); + set => user32.ShowWindow(hWnd, value ? 9 : 0); } public string className @@ -124,7 +118,7 @@ namespace CC_Functions.W32 get { StringBuilder ClassName = new StringBuilder(256); - _ = GetClassName(hWnd, ClassName, ClassName.Capacity); + user32.GetClassName(hWnd, ClassName, ClassName.Capacity); return ClassName.ToString(); } } @@ -133,7 +127,7 @@ namespace CC_Functions.W32 { get { - int style = GetWindowLong(hWnd, -16); + int style = user32.GetWindowLong(hWnd, -16); if ((style & 0x01000000) == 0x01000000) return FormWindowState.Maximized; if ((style & 0x20000000) == 0x20000000) @@ -145,15 +139,15 @@ namespace CC_Functions.W32 switch (value) { case FormWindowState.Minimized: - ShowWindow(hWnd, 11); + user32.ShowWindow(hWnd, 11); break; case FormWindowState.Normal: - ShowWindow(hWnd, 1); + user32.ShowWindow(hWnd, 1); break; case FormWindowState.Maximized: - ShowWindow(hWnd, 3); + user32.ShowWindow(hWnd, 3); break; } } @@ -169,19 +163,19 @@ namespace CC_Functions.W32 set { Rectangle tmp = position; - _ = SetWindowPos(hWnd, value ? HWND_TOPMOST : HWND_NOTOPMOST, tmp.X, tmp.Y, tmp.Width, tmp.Height, + user32.SetWindowPos(hWnd, value ? HWND_TOPMOST : HWND_NOTOPMOST, tmp.X, tmp.Y, tmp.Width, tmp.Height, value ? SWP_NOMOVE | SWP_NOSIZE : 0); } } public bool Destroy() { - if (DestroyWindow(hWnd)) + if (user32.DestroyWindow(hWnd)) return true; throw new Exception("Failed."); } - public bool stillExists => IsWindow(hWnd); + public bool stillExists => user32.IsWindow(hWnd); public override string ToString() => hWnd + "; " + title + "; " + position; @@ -199,106 +193,27 @@ namespace CC_Functions.W32 #endregion Exposed - #region W32 + #region Internal public IntPtr hWnd; - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool IsWindowVisible(IntPtr hWnd); - - [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)] - private static extern long GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); - - [DllImport("user32.dll")] - private static extern IntPtr GetForegroundWindow(); - - [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); - - [DllImport("user32.dll")] - private static extern IntPtr WindowFromPoint(int xPoint, int yPoint); - - [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - private static extern bool SetWindowText(IntPtr hWnd, string lpString); - - [DllImport("user32.dll", SetLastError = true)] - private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); - - [DllImport("user32.dll")] - private static extern bool SetForegroundWindow(IntPtr hWnd); - - [DllImport("user32.dll")] - private static extern bool EnableWindow(IntPtr hWnd, bool bEnable); - - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool IsWindowEnabled(IntPtr hWnd); - - [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool DestroyWindow(IntPtr hwnd); - private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); - private static readonly IntPtr HWND_TOP = new IntPtr(0); private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); private const uint SWP_NOSIZE = 0x0001; private const uint SWP_NOMOVE = 0x0002; - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, - uint uFlags); - - [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); - - [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, - ExactSpelling = true, SetLastError = true)] - private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect); - - private struct RECT - { - public int left; - public int top; - public int right; - public int bottom; - } - - [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] - private static extern int GetWindowTextLength(IntPtr hWnd); - - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool IsWindow(IntPtr hWnd); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] - private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); - - [DllImport("user32.dll")] - private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); - - [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, 1024); + StringBuilder sbTitle = new StringBuilder(1024); + user32.GetWindowText(hWnd, sbTitle, 1024); WindowHandles.Add(hWnd); return true; } - #endregion W32 + #endregion Internal } } \ No newline at end of file