简体   繁体   English

获取活动窗口文本(并向其发送更多文本)

[英]Get active window text (and send more text to it)

I'm creating a small utility in C#, that will add some text to an active textbox when a global hotkey is pressed, it's a type of auto complete function. 我在C#中创建一个小实用程序,当按下全局热键时,它会向活动文本框添加一些文本,这是一种自动完成功能。 I have my global hotkey working, but now I don't know how to get the current text in the active textbox (if the active window is a textbox that is.) What I've tried so far is to use 我有我的全局热键工作,但现在我不知道如何在活动文本框中获取当前文本(如果活动窗口是一个文本框)。到目前为止我尝试过的是使用

a. 一种。 GetForegroundWindow and then using that handle calling GetWindowText. GetForegroundWindow然后使用该句柄调用GetWindowText。 This gave me the window title of the active window, not the textbox contents. 这给了我活动窗口的窗口标题,而不是文本框内容。

b. GetActiveWindow and using that handle to call GetWindowText. GetActiveWindow并使用该句柄调用GetWindowText。 That gives me no text at all. 这根本不给我任何文字。

Here's an example of what I've done 这是我所做的一个例子

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();
[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);   
[DllImport("user32.dll")]
static extern int GetActiveWindow();

public static void TestA() {
    int h = GetForegroundWindow();
    StringBuilder b = new StringBuilder();
    GetWindowText(h, b, 256);
    MessageBox.Show(b.ToString());
}

public static void TestB() {
    int h = GetActiveWindow();
    StringBuilder b = new StringBuilder();
    GetWindowText(h, b, 256);
    MessageBox.Show(b.ToString());
}

So, any ideas on how to achieve this? 那么,关于如何实现这一点的任何想法?

Edit 28.01.2009: So, I found out how to do this. 编辑28.01.2009:所以,我发现了如何做到这一点。 This is what I used: 这是我用过的:

using System;
using System.Text;
using System.Runtime.InteropServices;

public class Example
{
[DllImport("user32.dll")]
static extern int GetFocus();

[DllImport("user32.dll")]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);    

[DllImport("user32.dll") ]
static extern int GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); 

const int WM_SETTEXT = 12;
const int WM_GETTEXT = 13;

public static void Main() 
{
    //Wait 5 seconds to give us a chance to give focus to some edit window,
    //notepad for example
    System.Threading.Thread.Sleep(5000);
    StringBuilder builder = new StringBuilder(500);

    int foregroundWindowHandle = GetForegroundWindow();
    uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0);
    uint currentThreadId = GetCurrentThreadId();

    //AttachTrheadInput is needed so we can get the handle of a focused window in another app
    AttachThreadInput(remoteThreadId, currentThreadId, true);
    //Get the handle of a focused window
    int  focused = GetFocus();
    //Now detach since we got the focused handle
    AttachThreadInput(remoteThreadId, currentThreadId, false);

    //Get the text from the active window into the stringbuilder
    SendMessage(focused, WM_GETTEXT, builder.Capacity, builder);
    Console.WriteLine("Text in active window was " + builder);
    builder.Append(" Extra text");
    //Change the text in the active window
    SendMessage(focused, WM_SETTEXT, 0, builder);
    Console.ReadKey();
    }
}

Some notes about this. 关于此的一些注释。 The example waits for 5 seconds before doing anything, giving you the chance to give focus to some edit window. 该示例在执行任何操作之前等待5秒,使您有机会将焦点放在某个编辑窗口上。 In my real app I'm using a hotkey to trigger this, but that would just confuse this example. 在我真正的应用程序中,我使用热键来触发这个,但这只会混淆这个例子。 Also, in production code you should check the return values of the win32 calls to see if they succeeded or not. 此外,在生产代码中,您应该检查win32调用的返回值,以查看它们是否成功。

It's reasonable to send keystrokes if you aware of active window and focused input field. 如果您知道活动窗口和聚焦输入字段,则发送击键是合理的。 See http://www.pinvoke.net/default.aspx/user32/keybd_event.html for API. 有关API,请参见http://www.pinvoke.net/default.aspx/user32/keybd_event.html

Please check, even em_replacesel message may not work across different process, You might need to use WM_COPYDATA or by calling window procedure as given in the url, 请检查,即使em_replacesel消息可能无法在不同的进程中工作,您可能需要使用WM_COPYDATA或通过调用url中给出的窗口过程,

http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.smartphone.developer&tid=4e3a9289-9355-4af7-a5b9-84f1aa601441&cat=&lang=&cr=&sloc=en-us&p=1 http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.smartphone.developer&tid=4e3a9289-9355-4af7-a5b9-84f1aa601441&cat=&lang=&cr=&sloc=en-我们&p = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM