|
@@ -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();
|
|
|
}
|
|
|
}
|
|
|
}
|