[WinApi] C#获取其他窗口文本框内容(转)
声明部分:
1 const int WM_GETTEXT = 0x000D; 2 const int WM_GETTEXTLENGTH = 0x000E; 3 4 [DllImport("user32.dll", EntryPoint = "SendMessageA")] 5 public static extern int SendMessage(int hwnd, int wMsg, int wParam, Byte[] lParam); 6 7 [DllImport("user32.dll", EntryPoint="SendMessageA")] 8 public static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
函数部分:
1 IntPtr mwh = FindMainWindowHandle("窗口名称", 100, 25); //主窗口句柄 2 IntPtr HwndStatue = FindWindowEx(mwh, IntPtr.Zero, "Edit", null); //文本框句柄 3 4 if (HwndStatue != IntPtr.Zero) 5 { 6 int TextLen; 7 TextLen = SendMessage((int)HwndStatue, WM_GETTEXTLENGTH, 0, 0); 8 Byte[] byt = new Byte[TextLen]; 9 SendMessage((int)HwndStatue, WM_GETTEXT, TextLen + 1, byt); 10 string str = Encoding.Default.GetString(byt); 11 }