c#获取notepad.exe内容
获取notepad.exe的内容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());
}
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, StringBuilder text);
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内容,但是最近测试发现只能获取窗口最前的,可能与版本或代码问题,未进行深入测试 对于使用标准Windows控件的编辑器(例如notepad),可以这么做,用SendMessage获取Text,还可以获取选中的文本。
但是对于许多自定义的文本内容(比如浏览器),就不行了,但是好在微软提供了无障碍阅读机制,提供了UIAutomation系列API,不过需要软件支持,几大浏览器基本都支持。 正好在学习C#语言,多看大神写的代码有利于帮助,谢谢大佬 优秀,测试通过{:1_893:}
页:
[1]