我想从另一个应用程序的文本框中检索文本。第二个应用程序的 ProcessName 是“TestTextBox”,TextBox 的名称是“textBox1”。
我的代码,它返回空字符串:
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);
Process[] processes = Process.GetProcessesByName("TestTextBox");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
const int WM_GETTEXT = 0x0D;
StringBuilder sb = new StringBuilder();
IntPtr retVal = SendMessage(pFoundWindow, WM_GETTEXT, 100, sb);
MessageBox.Show(sb.ToString());
}