我想从与我的程序无关的窗口中“读取”信息。如果我有进程 ID 和窗口句柄:
Process Proc = Process.GetProcessById(ProcID);
IntPtr hdl = Proc.MainWindowHandle;
我有来自 spy++ 的信息告诉我,我感兴趣的元素的控件 ID 是 00000003EA,我如何使用 C# 访问它?
谢谢你的帮助!
编辑_____________________________________
如果有人感兴趣,这就是我的工作方式:
Process p = Process.GetProcessById(ProcID);
IntPtr hdl = p.MainWindowHandle;
byte[] buffer = new byte[1024]; //Assume that 1024 bytes are enough! Better would be to get the text length..
UTF8Encoding enc = new UTF8Encoding();
uint Test = GetDlgItemText((int)hdl, Convert.ToInt32("0x000003EA", 16), buffer, 1024);
string TextFromOtherWindow = enc.GetString(buffer);
[DllImport("user32.dll")]
public static extern uint GetDlgItemText(
int hDlg, //A handle to the dialog box that contains the control.
int nIDDlgItem, //The identifier of the control whose title or text is to be retrieved.
byte[] lpString, //The buffer to receive the title or text.
int nMaxCount //The maximum length, in characters, of the string to be copied to the
//buffer pointed to by lpString. If the length of the string, including
//the null character, exceeds the limit, the string is truncated.
);
byte[] buffer
是来自另一个窗口的文本被写回的缓冲区。我假设文本长度不超过 1024 字节,但最好获得实际大小……</p>
就编码而言,不同的编码可能更适合您的需求。
十六进制的句柄需要转换为整数:Convert.ToInt32("0x000003EA", 16)
GetDlgItemText最适合(我认为)我获取静态文本的要求,而不是“<strong>SendMessage”和“<strong>WM_GETTEXT”。
感谢所有帮助我指出正确方向的人!
GetDlgItemText 的来源:MSDN
编辑_________________________________
嗯。我说得太早了......每次启动程序时都会更改元素ID。我在Persistent Element Identification提出了一个新问题。