private IntPtr GetControlHandle(string className, string text = "") { if (Process == null || Process.HasExited || Handle == IntPtr.Zero) { return(IntPtr.Zero); } if (Controls.Count == 0) { var windowEnumerator = new EnumWindows(); Controls = windowEnumerator.EnumChildWindows(Handle); } var stringBuilder = new StringBuilder(); var controlHandle = IntPtr.Zero; foreach (var control in Controls) { NativeMethods.GetClassName(control, stringBuilder, stringBuilder.Capacity); if (stringBuilder.ToString() != className) { continue; } if (string.IsNullOrEmpty(text)) { controlHandle = control; break; } else { NativeMethods.SendMessage(control, NativeMethods.WM_GETTEXT, new IntPtr(stringBuilder.Capacity), stringBuilder); if (stringBuilder.ToString() != text) { continue; } controlHandle = control; break; } } return(controlHandle); }
private IntPtr GetControlHandle(string className, string text = "") { if (Process == null || Process.HasExited || Handle == IntPtr.Zero) return IntPtr.Zero; if (Controls.Count == 0) { var windowEnumerator = new EnumWindows(); Controls = windowEnumerator.EnumChildWindows(Handle); } var stringBuilder = new StringBuilder(); var controlHandle = IntPtr.Zero; foreach (var control in Controls) { NativeMethods.GetClassName(control, stringBuilder, stringBuilder.Capacity); if (stringBuilder.ToString() != className) continue; if (string.IsNullOrEmpty(text)) { controlHandle = control; break; } else { NativeMethods.SendMessage(control, NativeMethods.WM_GETTEXT, new IntPtr(stringBuilder.Capacity), stringBuilder); if (stringBuilder.ToString() != text) continue; controlHandle = control; break; } } return controlHandle; }