Страницы

Поиск по вопросам

четверг, 21 марта 2019 г.

user32.dll FindWindowEx получение текста

Как я могу получить текст с lable окна другой программы используя user32.dll? Пытаюсь найти но не получается.
int i = 0; void Button1Click(object sender, EventArgs e) {
var m = FindWindow("WindowsForms10.Window.8.app.0.33c0d9d", null); var child = FindWindowEx(m, IntPtr.Zero + i, null, null); textBox2.Text += i + GetWindowTextRaw(child); i++;
}
Вот что "говорит" Inspector по клику на lable:
How found: Mouse move (483,319) hwnd=0x00010AE0 32bit class="WindowsForms10.Window.8.app.0.33c0d9d" style=0x56010000 ex=0x10000 ChildId: 0 Interfaces: IEnumVARIANT IOleWindow Impl: Remote native IAccessible Name: "Command History:" Value: [null] Role: client (0xA) State: focusable (0x100000) Location: {l:24, t:163, w:1242, h:250} Selection: Description: [null] Kbshortcut: [null] DefAction: [null] Help: [null] HelpTopic: "" ChildCount: 12 Window: 0x10AE0 FirstChild: none : window : focusable LastChild: none : window : focusable Next: [null] Previous: [null] Left: [null] Up: [null] Right: [null] Down: [null] Other Props: Object has no additional properties Children: none : window : focusable none : window : focused,focusable "Project : " : window : focusable "1126F1_9363262_40A" : window : focusable "Run" : window : focusable "Stop" : window : focusable "Serial #" : window : focusable "1P11321126F1#2PG#SJ1641700018" : window : focusable none : window : focusable "TS128L " : window : focusable "TS128L " : window : focusable none : window : focusable


Ответ

Спустя несколько дней разобрался. Может кому пригодится.
void FindWindow(){ var hWndParent = FindWindow("WindowsForms10.Window.8.app.0.33c0d9d", null); FindChild(hWndParent); } void FindChild(IntPtr hWndParent){ EnumChildWindows(hWndParent, new EnumWindowsProc(( hWnd, lParam ) => { if (GetParent(hWnd) != hWndParent){ return true; } FindChild(hWnd); if(GetText(hWnd).StartsWith("1P11321126F1#2PG#")){ if(label1.InvokeRequired){ label1.Invoke(new MethodInvoker(delegate { label1.Text = GetText(hWnd).Trim();})); } } return true; }), IntPtr.Zero); } string GetText(IntPtr hWnd){ StringBuilder sb = new StringBuilder(WM_GETTEXTLENGTH + 100000); string text = SendMessage(hWnd, WM_GETTEXT, WM_GETTEXTLENGTH + 100000, sb).ToString(); return sb.ToString(); }

#region user32.dll delegate bool EnumWindowsProc ( IntPtr hWnd, IntPtr lParam );
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool EnumWindows ( EnumWindowsProc lpEnumFunc, IntPtr lParam );
[DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool EnumChildWindows ( IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam );
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetParent ( IntPtr hWnd ); [DllImport("user32.dll", SetLastError = true)] static extern int GetClassName ( IntPtr hWnd, StringBuilder lpString, int nMaxCount );
[DllImport("user32.dll", SetLastError = true)] static extern int GetWindowTextLength ( IntPtr hWnd );
[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam[Out] StringBuilder lParam);
const int WM_GETTEXT = 0xD; const int WM_GETTEXTLENGTH = 0x000E; #endregion

Комментариев нет:

Отправить комментарий