2010-03-31 67 views
3

我打算创建一个C#Windows窗体应用程序作为第三方Win32应用程序的扩展,但我难以理解如何执行此操作马上。我得到的最远距离是知道它涉及到Win32 Hooking,并且有一个名为EasyHook的开源项目可以让我做到这一点。从封闭源代码第三方Win32应用程序的窗口中捕获数据

我想知道如何从第三方Win32应用程序中的文本框或控件中的其他数据获取文本。控件中的文本/数据将在用户按下按钮时从外部应用程序的运行窗口捕获。

我想这个问题可以概括如下:

  1. 你如何确定该事件 钩,当用户点击某个按钮?
  2. 在单击按钮时,如何获取由Win32控件显示的值 ?

回答

1

例如我为你创建了一个名为'Example'的应用程序,然后添加了一个文本框。 它是一个c#应用程序,但您也可以对所有Win32应用程序使用此方法。 首先创建一个名为Example的C#应用​​程序,然后为其添加一个文本框。 您需要将文本框的类名称用于此应用程序。然后创建一个应用程序并粘贴这些代码。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     [DllImport("user32.dll")] 
     public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 
     [DllImport("user32.dll")] 
     public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam); 
     [DllImport("user32.dll")] 
     public static extern IntPtr FindWindowEx(IntPtr Parent,IntPtr child, string classname, string WindowTitle); 
     const int WM_GETTEXT = 0x00D; 
     const int WM_GETTEXTLENGTH = 0x00E; 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      Process procc = GetProcByName("Example"); 
      // You can use spy++ to get main window handle so you don't need to use this code 
      if (procc != null) 
      { 
       IntPtr child = FindWindowEx(procc.MainWindowHandle, IntPtr.Zero, "WindowsForms10.EDIT.app.0.2bf8098_r16_ad1", null); 
       // Use Spy++ to get textbox's class name. for me it was "WindowsForms10.EDIT.app.0.2bf8098_r16_ad1" 
       int length = SendMessage(child, WM_GETTEXTLENGTH, 0, 0); 
       StringBuilder text = new StringBuilder(); 
       text = new StringBuilder(length + 1); 
       int retr2 = SendMessage(child, WM_GETTEXT, length + 1, text); 
       MessageBox.Show(text.ToString()); 
       // now you will see value of the your textbox on another application 

      } 

     } 
     public Process GetProcByName(string Name) 
     { 
      Process proc = null; 
      Process[] processes = Process.GetProcesses(); 
      for (int i = 0; i < processes.Length; i++) 
      { if (processes[i].ProcessName == Name)proc = processes[i]; } 
      return proc; 
     } 

    } 
} 

希望得到这个帮助。

+0

不,不,不!使用[UI自动化](http://msdn.microsoft.com/en-us/library/ms747327.aspx)。它是**官方**,**支持**和**文档**界面 - uhm - 自动化第三方应用程序。您为按钮设置事件侦听器,并在触发时检索文本框内容。 – IInspectable 2014-08-21 14:20:56

相关问题