2

我试图让这件事整天工作。我试图将输入文本粘贴到另一个 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);
    }
}
4

2 回答 2

2

我也尝试过 Spy++,但由于某种原因,它为所有子窗口提供了相同的 hwnd,一定是做错了什么。

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

于 2013-10-26T21:05:14.007 回答
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}");
                }
            }
        }

    }
}
于 2013-10-26T22:03:33.227 回答