Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

c# - 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. 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. This gave me the window title of the active window, not the textbox contents.

b. GetActiveWindow and using that handle to call 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. 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. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...