gpt4 book ai didi

c# - GetFocus - Win32api 帮助

转载 作者:可可西里 更新时间:2023-11-01 10:50:14 25 4
gpt4 key购买 nike

我正在尝试从用户计算机上打开的表单中获取选定的文本。目前我已经尝试使用 GetFocus 定义为

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

在 api 中它说 - 如果窗口附加到调用线程的消息队列,则检索具有键盘焦点的窗口的句柄。这解释了为什么我的应用程序可以获取选定的文本从我的应用程序的一部分窗口,而不是外部窗口,例如 pdf。

我可以使用哪种替代 win32 方法来满足此目的?

谢谢。

编辑:这是目前的尝试

[DllImport("user32.dll")] 静态外部 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);


// second overload of SendMessage

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);

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

private static string PerformCopy()
{
try
{
//Wait 5 seconds to give us a chance to give focus to some edit window,
//notepad for example
System.Threading.Thread.Sleep(1000);
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);

return builder.ToString();
}
catch (System.Exception oException)
{
throw oException;
}
}

最佳答案

关于c# - GetFocus - Win32api 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6548470/

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