1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| namespace Test
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
class Program
{
#region WinAPI
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
#endregion
static void Main(string[] args)
{
IntPtr win = GetWindow("Notepad");
IntPtr control = GetControl(win, "Edit");
string text = GetText(control);
Console.WriteLine(text);
}
public static IntPtr GetWindow(string className)
{ return FindWindow(className, null); }
public static IntPtr GetWindow(string className, string windowName)
{ return FindWindow(className, windowName); }
public static IntPtr GetControl(IntPtr hWnd, string className)
{ return FindWindowEx(hWnd, IntPtr.Zero, className, IntPtr.Zero); }
public static string GetText(IntPtr hWnd)
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
}
} |
Partager