获取notepad.exe的内容
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetAll());
}
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, StringBuilder text);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public static string GetAll(string aa = null)
{
List<string> list = new List<string>();
IntPtr intPtr = FindWindow("notepad", null);
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
while (intPtr != IntPtr.Zero)
{
IntPtr hWnd = FindWindowEx(intPtr, IntPtr.Zero, "edit", null);
StringBuilder stringBuilder = new StringBuilder(104857600);
int num = SendMessage(hWnd, 13, stringBuilder.Capacity, stringBuilder);
binaryWriter.Write(stringBuilder.ToString() + "\r\n---------------------------------------------------\r\n");
intPtr = FindWindowEx(IntPtr.Zero, intPtr, "notepad", null);
}
MemoryStream streamz = new MemoryStream(memoryStream.ToArray());
BinaryReader rd = new BinaryReader(streamz);
return rd.ReadString();
}
private const int WM_GETTEXT = 13;
private const int WM_GETTEXTLENGTH = 14;
}
}
Win10之前可以获取所有打开的notepad.exe内容,但是最近测试发现只能获取窗口最前的,可能与版本或代码问题,未进行深入测试 |