gpt4 book ai didi

c# - 获取事件窗口文本(并向其发送更多文本)

转载 作者:可可西里 更新时间:2023-11-01 08:29:17 26 4
gpt4 key购买 nike

我正在用 C# 创建一个小实用程序,它会在按下全局热键时向事件文本框添加一些文本,这是一种自动完成功能。我有我的全局热键工作,但现在我不知道如何在事件文本框中获取当前文本(如果事件窗口是一个文本框。)到目前为止我尝试过的是使用

一个。 GetForegroundWindow,然后使用该句柄调用 GetWindowText。这给了我事件窗口的窗口标题,而不是文本框内容。

GetActiveWindow 并使用该句柄调用 GetWindowText。这根本没有给我任何文字。

这是我做过的一个例子

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

那么,关于如何实现这一点有什么想法吗?

编辑 28.01.2009:所以,我找到了如何做到这一点。这是我使用的:

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

关于此的一些注意事项。该示例在执行任何操作之前等待 5 秒,让您有机会将焦点放在某个编辑窗口上。在我的真实应用程序中,我使用热键来触发它,但这只会混淆这个例子。此外,在生产代码中,您应该检查 win32 调用的返回值以查看它们是否成功。

最佳答案

如果您知道事件窗口和焦点输入字段,发送击键是合理的。参见 http://www.pinvoke.net/default.aspx/user32/keybd_event.html用于 API。

关于c# - 获取事件窗口文本(并向其发送更多文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/479548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com