Pinvoke SetFocus到特定的控件

简单的问题:是否可以将焦点设置在另一个应用程序的文本框上(使用它的ClassName)。 我有窗口作为“intptr”等等,但只需要一些指导,以了解可用的function/ API!

问题是,我使用SetForegroundWindow API获取窗口焦点,但它不会让我发送Ctrl + L键来关注文本框!

任何帮助都会很棒!

…据我所知,这是我必须使用的代码才能完成这项工作 – 这在我的应用程序和更新的Windows等方面运行良好。

void SetFocus(IntPtr hwndTarget, string childClassName) { // hwndTarget is the other app's main window // ... IntPtr targetThreadID = WindowsAPI.GetWindowThreadProcessId(hwndTarget, IntPtr.Zero); //target thread id IntPtr myThreadID = WindowsAPI.GetCurrentThread(); // calling thread id, our thread id try { bool lRet = WindowsAPI.AttachThreadInput(myThreadID, targetThreadID, -1); // attach current thread id to target window // if it's not already in the foreground... lRet = WindowsAPI.BringWindowToTop(hwndTarget); WindowsAPI.SetForegroundWindow(hwndTarget); // if you know the child win class name do something like this (enumerate windows using Win API again)... var hwndChild = EnumAllWindows(hwndTarget, childClassName).FirstOrDefault(); if (hwndChild == IntPtr.Zero) { // or use keyboard etc. to focus, ie send keys/input... // SendInput (...); return; } // you can use also the edit control's hwnd or some child window (of target) here WindowsAPI.SetFocus(hwndChild); // hwndTarget); } finally { bool lRet = WindowsAPI.AttachThreadInput(myThreadID, targetThreadID, 0); //detach from foreground window } } 

…所以沿着这些方向做某事(它按照你的需要做正确的顺序,不要忘记分离等等 – 但你需要根据你的具体情况调整它,控制/编辑hwnd等等 – 和你可能还有其他与目标窗口/应用相关的问题,这适用于大多数情况,但并非在所有情况下,这是一个很长的故事,正如我所说,取决于你的具体情况),

(WindowsAPI是我认为的典型PInvoke包装器)基本上你需要附加到另一个线程进行’输入’操作,我相信这是官方解释“这也允许线程共享它们的输入状态,所以它们可以调用SetFocus函数来设置键盘聚焦到不同线程的窗口。“ 谷歌的AttachThreadInput更多信息(了解原因),它也经常与SetFocus和其他输入/键盘操作相关联。 此外,Automation API可以提供帮助 – 这是“最干净”的方式 – 但取决于目标应用程序是否正确暴露和处理 – 对于大多数人来说仍然“不存在”,不一致等等 – 如果你想要处理与你不同的“自己的应用程序”,你需要问自己什么是最好的方案等希望这有助于

注意:必须有十几个链接到类似的解决方案(和SO),因为这是一个众所周知的事情,但我无法找到一个正确的链接

编辑:我澄清了你的具体案例和儿童焦点
编辑(2):
代码是此规范的一个示例。 案例并基于工作代码 – 但可能需要测试并制定一些细节(这似乎超出了这个问题的范围),例如..
WindowsAPI包含Windows API和本机调用的PInvoke签名 (类似于MS.Win32.UnsafeNativeMethods),它是一个静态类(请参阅该类或http://pinvoke.net/ – 还访问Microsoft.Win32.UnsafeNativeMethods? ),被命名为(安全/不安全)NativeMethods( http://msdn.microsoft.com/en-us/library/ms182161.aspx ) – 并且还可以看到IntPtr,SafeHandle和HandleRef – 解释 (IntPtr有点’旧’风格)
EnumAllWindows使用EnumChildWindows和GetClassName Win API (这是我猜的另一个问题)并且需要一个包装器方法才能使它有用(EnumAllWindows是 – 它只是通过递归检查类名来枚举窗口)。