Browse Source

切换为剪切板

liuyuqi-dellpc 1 year ago
parent
commit
03bc961593

+ 9 - 1
GetInputText/Pages/MainForm.Designer.cs

@@ -29,8 +29,10 @@ namespace GetInputText
         /// </summary>
         private void InitializeComponent()
         {
+            this.components = new System.ComponentModel.Container();
             this.lbTitle = new System.Windows.Forms.Label();
             this.tbContent = new System.Windows.Forms.TextBox();
+            this.timer1 = new System.Windows.Forms.Timer(this.components);
             this.SuspendLayout();
             // 
             // lbTitle
@@ -52,11 +54,16 @@ namespace GetInputText
             this.tbContent.Size = new System.Drawing.Size(518, 202);
             this.tbContent.TabIndex = 2;
             // 
+            // timer1
+            // 
+            this.timer1.Interval = 1000;
+            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
+            // 
             // 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.ClientSize = new System.Drawing.Size(570, 305);
             this.Controls.Add(this.tbContent);
             this.Controls.Add(this.lbTitle);
             this.Name = "MainForm";
@@ -71,6 +78,7 @@ namespace GetInputText
 
         private System.Windows.Forms.Label lbTitle;
         private System.Windows.Forms.TextBox tbContent;
+        private System.Windows.Forms.Timer timer1;
     }
 }
 

+ 38 - 9
GetInputText/Pages/MainForm.cs

@@ -16,7 +16,8 @@ namespace GetInputText
         public MainForm()
         {
             InitializeComponent();
-            GetTextFromForegroundWindow();
+            timer1.Enabled = true;
+            timer1.Start();
         }
 
         const int WM_GETTEXT = 0x000D;
@@ -31,6 +32,9 @@ namespace GetInputText
         [DllImport("user32.dll")]
         public static extern IntPtr GetForegroundWindow();
 
+        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
+        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
+
         /// <summary>
         /// 全局监听激活的应用输入框的内容
         /// </summary>
@@ -38,18 +42,43 @@ namespace GetInputText
         public void GetTextFromForegroundWindow()
         {
             IntPtr hWnd = GetForegroundWindow();
-            int len = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
-            if (len > 0)
+            // Get the context of being typed
+            IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
+            if (hEdit != IntPtr.Zero)
             {
-                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 != "")
+                // Get the length of the text
+                int length = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
+                StringBuilder sb = new StringBuilder(length + 1);
+                byte[] sbbyte=Encoding.Default.GetBytes(sb.ToString());
+                SendMessage(
+                    hEdit, WM_GETTEXT, sb.Capacity, sbbyte);
+                string text = sbbyte.ToString();
+                if (text.Length > 0 && text.IndexOf("/")==0)
                 {
-                    this.tbContent.Text = text;
+                    tbContent.Text = text;
                 }
             }
+            
+        }
+
+        /// 剪切板获取
+        public String GetTextFromClipboard(){
+            String text = "";
+            if (Clipboard.ContainsText())
+            {
+                text = Clipboard.GetText();
+            }
+            if (text.Length > 0)
+            {
+                tbContent.Text = text;
+            }
+            return text;
+        }
+
+        private void timer1_Tick(object sender, EventArgs e)
+        {
+            // GetTextFromForegroundWindow();
+            GetTextFromClipboard();
         }
     }
 }

+ 3 - 0
GetInputText/Pages/MainForm.resx

@@ -117,4 +117,7 @@
   <resheader name="writer">
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
+  <metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>17, 17</value>
+  </metadata>
 </root>