[WinApi] C#获取其他窗口文本框内容
程序员文章站
2023-08-25 13:25:09
声明部分: const int WM_GETTEXT = 0x000D; const int WM_GETTEXTLENGTH = 0x000E; [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern ......
声明部分:
const int wm_gettext = 0x000d; const int wm_gettextlength = 0x000e; [dllimport("user32.dll", entrypoint = "sendmessagea")] public static extern int sendmessage(intptr hwnd, int wmsg, int wparam, byte[] lparam); [dllimport("user32.dll", entrypoint="sendmessagea")] public static extern int sendmessage(intptr hwnd, int wmsg, int wparam, int lparam);
函数部分:
intptr mwh = findmainwindowhandle("窗口名称", 100, 25); //主窗口句柄 intptr hwndstatue = findwindowex(mwh, intptr.zero, "edit", null); //文本框句柄 if (hwndstatue != intptr.zero) { int textlen; textlen = sendmessage(hwndstatue, wm_gettextlength, 0, 0); byte[] byt = new byte[textlen]; sendmessage(hwndstatue, wm_gettext, textlen + 1, byt); string str = encoding.default.getstring(byt); }