public static bool Enter(string login, string pass) { var hLoginWnd = Win32Functions.FindWindow("#32770", "Идентификация пользователя"); if (hLoginWnd == IntPtr.Zero) { hLoginWnd = Win32Functions.FindWindow("#32770", "User identification"); } if (hLoginWnd == IntPtr.Zero) { return(false); } if (hLoginWnd != IntPtr.Zero) { var nBtnOk = WindowTools.FindWindowByIndex(hLoginWnd, 1, "Button"); var hLogin = WindowTools.FindWindowByIndex(hLoginWnd, 1, "Edit"); var nPassw = WindowTools.FindWindowByIndex(hLoginWnd, 2, "Edit"); WindowTools.SetWindowText(hLogin, login); WindowTools.SetWindowText(nPassw, pass); WindowTools.Click(nBtnOk); } return(true); }
/// <summary> /// Отправить текст в окно с данными пользователя. /// </summary> /// <param name="hWnd"></param> /// <param name="text"></param> public static void SetWindowText(IntPtr hWnd, string text) { try { Win32Functions.SetFocus(hWnd); Win32Functions.SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, null); foreach (var c in text) { Thread.Sleep(50); var val = new IntPtr((Int32)c); Win32Functions.PostMessage(hWnd, WM_CHAR, val, new IntPtr(0)); } } catch (Exception) { throw; } }
/// <summary> /// Найти дискриптор окна по индексу. /// </summary> /// <param name="hWndParent"></param> /// <param name="index"></param> /// <param name="type"></param> /// <returns></returns> public static IntPtr FindWindowByIndex(IntPtr hWndParent, int index, string type) { if (index == 0) { return(hWndParent); } int ct = 0; var result = IntPtr.Zero; do { result = Win32Functions.FindWindowEx(hWndParent, result, type, null); if (result != IntPtr.Zero) { ++ct; } }while (ct < index && result != IntPtr.Zero); return(result); }
public static void Click(IntPtr nBtn) { Win32Functions.SetFocus(nBtn); Win32Functions.PostMessage(nBtn, BM_CLICK, new IntPtr(0), new IntPtr(0)); }