window - 在另一个窗口中查找文本框的 hwnd

标签 window sendmessage hwnd

我正在努力让这个东西整天工作。 我正在尝试将输入文本粘贴到另一个 Windows 应用程序中的文本框(比如说 Chrome 的 URL 栏) 我设法获取了主窗口的hwnd,但是当我使用SendMessage时,它只是改变了窗口的标题

所以我试图弄清楚如何动态地找到我的文本框的 hwnd。 我也尝试过 Spy++,但由于某种原因它给了我所有子窗口相同的 hwnd,一定是做错了什么。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

        private static IntPtr WinGetHandle(string wName)
        {
            IntPtr hWnd = IntPtr.Zero;
            foreach (Process pList in Process.GetProcesses())
            {
                if (pList.MainWindowTitle.Contains(wName))
                {
                    hWnd = pList.MainWindowHandle;
                }
            }
            return hWnd;
        }
        static void Main(string[] args)
        {
            List<IntPtr> myList = GetChildWindows(WinGetHandle("chrome"));
            foreach(IntPtr ptr in myList) 
            {
                //No idea which one of those IntPtr is actually my textbox
                //SendMessage((IntPtr)788018, 0x000C, 0, "text here");
            }
            Console.ReadLine();
        }

        [DllImport("user32")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

        public static List<IntPtr> GetChildWindows(IntPtr parent)
        {
            List<IntPtr> result = new List<IntPtr>();
            GCHandle listHandle = GCHandle.Alloc(result);
            try
            {
                EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
                EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return result;
        }

        private static bool EnumWindow(IntPtr handle, IntPtr pointer)
        {
            GCHandle gch = GCHandle.FromIntPtr(pointer);
            List<IntPtr> list = gch.Target as List<IntPtr>;
            if (list == null)
            {
                throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
            }
            list.Add(handle);
            //  You can modify this to check to see if you want to cancel the operation, then return a null here
            return true;
        }

        public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
    }
}

最佳答案

I also tried Spy++ but it gave me the same hwnd for all child windows for some reason, must did something wrong.

并非所有应用程序都使用由 Windows 句柄支持的老式“重型”控件。例如,.Net WPF 应用程序将表现出与主窗口本身仅一个句柄相同的行为。其他所有内容都根据需要通过 DirectX 直接绘制到屏幕上。 Web 浏览器也很“轻”,不使用句柄支持的控件。基本上,如果 Spy++ 不在主窗口内显示子窗口,那么您将无法使用传统的窗口操作 API 或 SendMessage()。

关于window - 在另一个窗口中查找文本框的 hwnd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19611101/

相关文章:

javascript - 如何在提交 JavaScript 后关闭模态

css - 当窗口超过一定大小时导航链接移动

c++ - 发送消息不起作用

eclipse - 如何更改 Eclipse 窗口标题?

.net - 如何在 .NET 中获取当前窗口句柄计数和窗口句柄限制?

c++ - 如何让 C++ 按下空格键?

c# - 将右键单击发送到窗口

C++ 在运行时更改 HWND 窗口过程

c++ - 在 Qt 中释放和恢复嵌入窗口

c++ - 获取外部运行的可见窗口c++的句柄