liuyuqi-dellpc 2 years ago
parent
commit
04b679f7fa

+ 4 - 6
GetInputText/GetInputText.csproj

@@ -46,15 +46,15 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="MainForm.cs">
+    <Compile Include="Pages\MainForm.cs">
       <SubType>Form</SubType>
     </Compile>
-    <Compile Include="MainForm.Designer.cs">
+    <Compile Include="Pages\MainForm.Designer.cs">
       <DependentUpon>MainForm.cs</DependentUpon>
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <EmbeddedResource Include="MainForm.resx">
+    <EmbeddedResource Include="Pages\MainForm.resx">
       <DependentUpon>MainForm.cs</DependentUpon>
     </EmbeddedResource>
     <EmbeddedResource Include="Properties\Resources.resx">
@@ -79,8 +79,6 @@
   <ItemGroup>
     <None Include="App.config" />
   </ItemGroup>
-  <ItemGroup>
-    <Folder Include="Pages\" />
-  </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 0 - 48
GetInputText/MainForm.Designer.cs

@@ -1,48 +0,0 @@
-
-namespace GetInputText
-{
-    partial class MainForm
-    {
-        /// <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.SuspendLayout();
-            // 
-            // Form1
-            // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
-            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(800, 450);
-            this.Name = "Form1";
-            this.Text = "P";
-            this.ResumeLayout(false);
-
-        }
-
-        #endregion
-    }
-}
-

+ 0 - 20
GetInputText/MainForm.cs

@@ -1,20 +0,0 @@
-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 GetInputText
-{
-    public partial class MainForm : Form
-    {
-        public MainForm()
-        {
-            InitializeComponent();
-        }
-    }
-}

+ 76 - 0
GetInputText/Pages/MainForm.Designer.cs

@@ -0,0 +1,76 @@
+
+namespace GetInputText
+{
+    partial class MainForm
+    {
+        /// <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.lbTitle = new System.Windows.Forms.Label();
+            this.tbContent = new System.Windows.Forms.TextBox();
+            this.SuspendLayout();
+            // 
+            // lbTitle
+            // 
+            this.lbTitle.AutoSize = true;
+            this.lbTitle.Font = new System.Drawing.Font("SimSun", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.lbTitle.Location = new System.Drawing.Point(28, 19);
+            this.lbTitle.Name = "lbTitle";
+            this.lbTitle.Size = new System.Drawing.Size(136, 21);
+            this.lbTitle.TabIndex = 1;
+            this.lbTitle.Tag = "";
+            this.lbTitle.Text = "输入框内容:";
+            // 
+            // tbContent
+            // 
+            this.tbContent.Location = new System.Drawing.Point(30, 54);
+            this.tbContent.Multiline = true;
+            this.tbContent.Name = "tbContent";
+            this.tbContent.Size = new System.Drawing.Size(518, 202);
+            this.tbContent.TabIndex = 2;
+            // 
+            // MainForm
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(570, 268);
+            this.Controls.Add(this.tbContent);
+            this.Controls.Add(this.lbTitle);
+            this.Name = "MainForm";
+            this.Text = "P";
+            this.TopMost = true;
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.Label lbTitle;
+        private System.Windows.Forms.TextBox tbContent;
+    }
+}
+

+ 54 - 0
GetInputText/Pages/MainForm.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace GetInputText
+{
+    public partial class MainForm : Form
+    {
+        public MainForm()
+        {
+            InitializeComponent();
+        }
+
+        const int WM_GETTEXT = 0x000D;
+        const int WM_GETTEXTLENGTH = 0x000E;
+
+        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
+        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam);
+
+        [DllImport("user32.dll", EntryPoint="SendMessageA")]
+        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
+
+        [DllImport("user32.dll")]
+        public static extern IntPtr GetForegroundWindow();
+
+        /// <summary>
+        /// 全局监听激活的应用输入框的内容
+        /// </summary>
+        /// <param name="hWnd"></param>
+        public void GetTextFromWindow()
+        {
+            IntPtr hWnd = GetForegroundWindow();
+            int len = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
+            if (len > 0)
+            {
+                Byte[] buffer = new Byte[len + 1];
+                SendMessage(hWnd, WM_GETTEXT, len + 1, buffer);
+                string text = Encoding.Default.GetString(buffer);
+                text = text.Substring(0, text.Length - 1);
+                if (text != "")
+                {
+                    this.tbContent.Text = text;
+                }
+            }   
+        }
+    }
+}

+ 0 - 0
GetInputText/MainForm.resx → GetInputText/Pages/MainForm.resx