プログラムに関係のないウィンドウから情報を「読み」たい。プロセスIDとウィンドウハンドルがある場合:
Process Proc = Process.GetProcessById(ProcID);
IntPtr hdl = Proc.MainWindowHandle;
また、spy ++から、関心のある要素のcontrol-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>
エンコーディングに関しては、別のエンコーディングの方がニーズに適している場合があります。
16進数のハンドルは整数に変換する必要があります。Convert.ToInt32("0x000003EA", 16)
GetDlgItemTextは、「<strong>SendMessage」や「<strong>WM_GETTEXT」ではなく、静的テキストを取得するという私の要件に最も適していました(私は思います)。
私を正しい方向に向けるのを手伝ってくれたすべての人に感謝します!
GetDlgItemTextのソース:MSDN
編集_________________________________
うーん。話が早すぎました...プログラムを起動するたびに要素IDが変更されます。永続要素の識別で新しい質問を開きました。