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

时间:2013-10-26 19:45:16

标签: 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);
    }
}

2 个答案:

答案 0 :(得分:2)

  

我也试过Spy ++,但它给了我所有子窗口的相同hwnd   出于某种原因,必须做错事。

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

答案 1 :(得分:0)

可悲的是,我找不到按照我想要的方式制作它的方法,但我找到了一个非常有效的解决方法:

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", ExactSpelling = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        static void Main(string[] args)
        {
            IntPtr hWnd = FindWindow(null, "Origin");
            if (!hWnd.Equals(IntPtr.Zero))
            {
                // activate Notepad window
                if (SetForegroundWindow(hWnd))
                {
                    // send "Hello World!"
                    SendKeys.SendWait("Hello World!");
                    // send key "Tab"
                    SendKeys.SendWait("{TAB}");
                    // send key "Enter"
                    SendKeys.SendWait("{ENTER}");
                }
            }
        }

    }
}