private void GetUIElements(List <SAPEventElement> list, GuiSession session, string Parent, SAPEventElement ele, bool VisibleOnly) { list.Add(ele); if (!string.IsNullOrEmpty(ele.Path)) { System.Diagnostics.Trace.WriteLine(ele.ToString() + " " + ele.Rectangle); } if (ele.Children == null) { ele.Load(VisibleOnly); } if (ele.Children != null) { foreach (var child in ele.Children) { GetUIElements(list, session, ele.Id, child, VisibleOnly); } } }
private void GetUIElements(List <SAPEventElement> list, SAPEventElement msg, GuiTree tree, string path, string SystemName) { GuiCollection keys = null; if (string.IsNullOrEmpty(path)) { keys = tree.GetNodesCol() as GuiCollection; } else { keys = tree.GetSubNodesCol(path) as GuiCollection; } if (keys != null) { foreach (string key in keys) { var _msg = new SAPEventElement(msg, tree, msg.Path, key, SystemName); _msg.type = "GuiTreeNode"; list.Add(_msg); System.Diagnostics.Trace.WriteLine(_msg.ToString()); GetUIElements(list, msg, tree, key, SystemName); } } }
private static void OnMouseMove(InputEventArgs e) { lock (_lock) { if (isMoving) { return; } isMoving = true; } try { if (SAPHook.Instance.Connections.Count() == 0 || SAPHook.Instance.UIElements.Count() == 0) { lock (_lock) { isMoving = false; } return; } var Element = System.Windows.Automation.AutomationElement.FromPoint(new System.Windows.Point(e.X, e.Y)); if (Element != null) { var ProcessId = Element.Current.ProcessId; if (ProcessId < 1) { lock (_lock) { isMoving = false; } return; } if (SAPProcessId > 0 && SAPProcessId != ProcessId) { lock (_lock) { isMoving = false; } return; } if (SAPProcessId != ProcessId) { var p = System.Diagnostics.Process.GetProcessById(ProcessId); if (p.ProcessName.ToLower() == "saplogon") { SAPProcessId = p.Id; } if (p.ProcessName.ToLower() != "saplogon") { lock (_lock) { isMoving = false; } return; } } if (SAPHook.Instance.Connections.Count() == 0) { SAPHook.Instance.RefreshSessions(); } if (SAPHook.Instance.UIElements.Count() == 0) { SAPHook.Instance.RefreshUIElements(true); } SAPEventElement[] elements = new SAPEventElement[] { }; lock (SAPHook.Instance.UIElements) { elements = SAPHook.Instance.UIElements.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); } if (elements.Count() > 0) { //Program.log("[mousemove] " + e.X + " " + e.Y); //foreach(var ele in elements) //{ // Program.log("[element] " + ele.ToString()); //} var found = elements.OrderBy(x => x.IdPathCell.Length).Last(); if (found.Items != null && found.Items.Length > 0) { elements = found.Items.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); if (elements != null && elements.Length > 0) { found = elements.OrderBy(x => x.IdPathCell.Length).Last(); } } //Program.log("[element] " + found.ToString() + " " + found.Rectangle.ToString()); if (found.Items != null && found.Items.Length > 0) { var found2 = found.Items.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); if (found2.Length > 0) { found = found2.First(); } } if (LastElement != null && (found.Id == LastElement.Id && found.Path == LastElement.Path && found.Cell == LastElement.Cell)) { // form.AddText("[SKIP] mousemove " + LastElement.ToString()); lock (_lock) { isMoving = false; } return; } LastElement = found; SAPEvent message = new SAPEvent("mousemove"); message.Set(LastElement); form.AddText("[send] " + message.action + " " + LastElement.ToString() + " " + LastElement.Rectangle.ToString()); pipe.PushMessage(message); } else { log("Mouseover " + e.X + "," + e.Y + " not found in UI List"); } } } catch (Exception) { } lock (_lock) { isMoving = false; } }
private static void Server_OnReceivedMessage(NamedPipeConnection <SAPEvent, SAPEvent> connection, SAPEvent message) { try { if (message == null) { return; } form.AddText("[resc] " + message.action); if (message.action == "beginrecord") { try { recordstarting = true; var recinfo = message.Get <SAPToogleRecordingEvent>(); var overlay = false; if (recinfo != null) { overlay = recinfo.overlay; //StartMonitorMouse(recinfo.mousemove); StartMonitorMouse(true); } SAPHook.Instance.BeginRecord(overlay); form.AddText("[send] " + message.action); pipe.PushMessage(message); recordstarting = false; } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } if (message.action == "endrecord") { try { StopMonitorMouse(); SAPHook.Instance.EndRecord(); form.AddText("[send] " + message.action); pipe.PushMessage(message); } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } if (message.action == "login") { try { var login = message.Get <SAPLoginEvent>(); if (SAPHook.Instance.Login(login)) { var session = SAPHook.Instance.GetSession(login.SystemName); if (session == null) { message.error = "Login failed"; } } else { message.error = "Login failed"; } form.AddText("[send] " + message.action); pipe.PushMessage(message); } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } if (message.action == "getconnections") { try { var result = new SAPGetSessions(); SAPHook.Instance.RefreshSessions(); result.Connections = SAPHook.Instance.Connections; message.Set(result); form.AddText("[send] " + message.action); pipe.PushMessage(message); } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } if (message.action == "getitems") { } if (message.action == "getitem") { try { var msg = message.Get <SAPEventElement>(); if (string.IsNullOrEmpty(msg.SystemName)) { throw new ArgumentException("System Name is mandatory right now!"); } var session = SAPHook.Instance.GetSession(msg.SystemName); if (session != null) { GuiComponent comp = session.GetSAPComponentById <GuiComponent>(msg.Id); if (comp is null) { throw new ArgumentException("Item with id " + msg.Id + " was not found"); } msg.Id = comp.Id; msg.Name = comp.Name; msg.SystemName = session.Info.SystemName; msg.ContainerType = comp.ContainerType; msg.type = comp.Type; var p = comp.Parent as GuiComponent; string parent = (p != null) ? p.Id : null; msg.Parent = parent; // msg.LoadProperties(comp, true); msg.LoadProperties(comp, msg.GetAllProperties); var children = new List <SAPEventElement>(); if (comp.ContainerType) { var cont = comp as GuiVContainer; if (comp is GuiVContainer vcon) { for (var i = 0; i < vcon.Children.Count; i++) { GuiComponent Element = vcon.Children.ElementAt(i); p = Element.Parent as GuiComponent; parent = (p != null) ? p.Id : null; var _newchild = new SAPEventElement(Element, session.Info.SystemName, parent, false); children.Add(_newchild); if (msg.MaxItem > 0) { if (children.Count >= msg.MaxItem) { break; } } } } else if (comp is GuiContainer con) { for (var i = 0; i < con.Children.Count; i++) { GuiComponent Element = con.Children.ElementAt(i); p = Element.Parent as GuiComponent; parent = (p != null) ? p.Id : null; children.Add(new SAPEventElement(Element, session.Info.SystemName, parent, false)); if (msg.MaxItem > 0) { if (children.Count >= msg.MaxItem) { break; } } } } else if (comp is GuiStatusbar sbar) { msg.type = "GuiStatusbar"; for (var i = 0; i < sbar.Children.Count; i++) { GuiComponent Element = sbar.Children.ElementAt(i); p = Element.Parent as GuiComponent; parent = (p != null) ? p.Id : null; children.Add(new SAPEventElement(Element, session.Info.SystemName, parent, false)); if (msg.MaxItem > 0) { if (children.Count >= msg.MaxItem) { break; } } } } else { throw new Exception("Unknown container type " + comp.Type + "!"); } } if (comp is GuiTree tree) { msg.type = "GuiTree"; GuiCollection keys = null; if (string.IsNullOrEmpty(msg.Path)) { keys = tree.GetNodesCol() as GuiCollection; } else { msg = new SAPEventElement(msg, tree, "", msg.Path, session.Info.SystemName); keys = tree.GetSubNodesCol(msg.Path) as GuiCollection; } if (keys != null) { foreach (string key in keys) { var _msg = new SAPEventElement(msg, tree, msg.Path, key, session.Info.SystemName); _msg.type = "GuiTreeNode"; children.Add(_msg); System.Diagnostics.Trace.WriteLine(_msg.ToString()); if (msg.MaxItem > 0) { if (children.Count >= msg.MaxItem) { break; } } } } } if (comp is GuiTableControl table) { msg.type = "GuiTable"; if (string.IsNullOrEmpty(msg.Path)) { msg.type = "GuiTable"; } else { } var columns = new List <string>(); for (var i = 0; i < table.Columns.Count; i++) { columns.Add(table.Columns.ElementAt(i).ToString()); } for (var i = 0; i < table.RowCount; i++) { var row = table.Rows.ElementAt(i) as GuiTableRow; // var _msg = new SAPEventElement(msg, tree, msg.Path, key, session.Info.SystemName); // children.Add(_msg); // System.Diagnostics.Trace.WriteLine(_msg.ToString()); System.Diagnostics.Trace.WriteLine(row.ToString()); if (msg.MaxItem > 0) { if (i >= msg.MaxItem) { break; } } } } if (comp is GuiGridView grid) { msg.type = "GuiGrid"; if (string.IsNullOrEmpty(msg.Path)) { for (var i = 0; i < grid.RowCount; i++) { var _msg = new SAPEventElement(msg, grid, msg.Path, i, session.Info.SystemName); _msg.type = "GuiGridNode"; children.Add(_msg); System.Diagnostics.Trace.WriteLine(_msg.ToString()); if (msg.MaxItem > 0) { if (i >= msg.MaxItem) { break; } } } } else { msg = new SAPEventElement(msg, grid, msg.Path, int.Parse(msg.Path), session.Info.SystemName); } } msg.Children = children.ToArray(); } else { message.error = "SAP not running, or session " + msg.SystemName + " not found."; } message.Set(msg); form.AddText("[send] " + message.action); pipe.PushMessage(message); } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } if (message.action == "invokemethod" || message.action == "setproperty" || message.action == "getproperty" || message.action == "highlight") { try { var step = message.Get <SAPInvokeMethod>(); if (step != null) { var session = SAPHook.Instance.GetSession(step.SystemName); if (session != null) { GuiComponent comp = session.GetSAPComponentById <GuiComponent>(step.Id); if (comp == null) { if (step.Id.Contains("/tbar[1]/")) { comp = session.GetSAPComponentById <GuiComponent>(step.Id.Replace("/tbar[1]/", "/tbar[0]/")); } } if (comp == null) { throw new Exception(string.Format("Can't find component using id {0}", step.Id)); } string typeName = _prefix + comp.Type; Type t = SAPGuiApiAssembly.GetType(typeName); if (t == null) { throw new Exception(string.Format("Can't find type {0}", typeName)); } var Parameters = Newtonsoft.Json.JsonConvert.DeserializeObject <object[]>(step.Parameters); if (message.action == "invokemethod") { step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.InvokeMethod, null, comp, Parameters); } if (message.action == "setproperty") { step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.SetProperty, null, comp, Parameters); } if (message.action == "getproperty") { step.Result = t.InvokeMember(step.ActionName, System.Reflection.BindingFlags.GetProperty, null, comp, Parameters); } var vcomp = comp as GuiVComponent; if (message.action == "highlight" && vcomp != null) { try { if (_lastHighlight != null) { _lastHighlight.Visualize(false); } } catch (Exception) { } _lastHighlight = null; _lastHighlight = comp as GuiVComponent; _lastHighlight.Visualize(true); } } else { message.error = "SAP not running, or session " + step.SystemName + " not found."; } message.Set(step); } form.AddText("[send] " + message.action); pipe.PushMessage(message); } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } } catch (Exception ex) { log(ex.ToString()); form.AddText(ex.ToString()); } }
private static void OnMouseDown(InputEventArgs e) { try { if (LastElement != null) { SAPEvent message = new SAPEvent("mousedown"); message.Set(LastElement); if (log_send_message) { form.AddText("[send] " + message.action + " " + LastElement.ToString()); } pipe.PushMessage(message); } // if (SAPHook.Instance.UIElements.Count() == 0) return; // var Element = System.Windows.Automation.AutomationElement.FromPoint(new System.Windows.Point(e.X, e.Y)); // if (Element != null) // { // var ProcessId = Element.Current.ProcessId; // if (ProcessId < 1) return; // if (SAPProcessId > 0 && SAPProcessId != ProcessId) return; // if (SAPProcessId != ProcessId) // { // using (var p = System.Diagnostics.Process.GetProcessById(ProcessId)) // { // if (p.ProcessName.ToLower() == "saplogon") SAPProcessId = p.Id; // if (p.ProcessName.ToLower() != "saplogon") return; // } // } // SAPEventElement[] elements = new SAPEventElement[] { }; // if (System.Threading.Monitor.TryEnter(SAPHook.Instance.UIElements, 10000)) // { // try // { // elements = SAPHook.Instance.UIElements.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); // } // finally // { // System.Threading.Monitor.Exit(SAPHook.Instance.UIElements); // } // } // if (elements.Count() > 0) // { // var last = elements.OrderBy(x => x.Id.Length).Last(); // SAPEvent message = new SAPEvent("mousedown"); // message.Set(last); // if (log_send_message) form.AddText("[send] " + message.action + " " + last.ToString()); // pipe.PushMessage(message); // Task.Run(() => { if (recordstarting && !SAPHook.Instance.refreshingui) SAPHook.Instance.RefreshUIElements(true); }); // // // } // else // { // log("OnMouseDown " + e.X + "," + e.Y + " not found in UI List"); // } // } } catch (Exception) { } }
private static void OnMouseMove(InputEventArgs e) { if (_isMoving) { return; } try { _isMoving = true; var app = SAPHook.Instance.app; if (app != null && app.Children != null) { for (int x = 0; x < app.Children.Count; x++) { var con = app.Children.ElementAt(x) as GuiConnection; if (con.Sessions.Count == 0) { continue; } for (int j = 0; j < con.Sessions.Count; j++) { var session = con.Children.ElementAt(j) as GuiSession; var SystemName = session.Info.SystemName.ToLower(); // var ele = session as GuiComponent; GuiCollection keys = null; try { if (System.Threading.Monitor.TryEnter(_lock, 1)) { try { var _y = session.ActiveWindow.Top; var _x = session.ActiveWindow.Left; var w = session.ActiveWindow.Width; var h = session.ActiveWindow.Height; if (e.X < _x || e.Y < _y) { return; } if (e.X > (_x + w) || e.Y > (_y + h)) { return; } keys = session.FindByPosition(e.X, e.Y); } finally { System.Threading.Monitor.Exit(_lock); } } else { return; } } catch (System.Runtime.InteropServices.COMException ex) { // OnMouseMove(e); } catch (Exception ex) { log(ex.Message); } if (keys == null) { return; } var _keys = new List <string>(); foreach (string key in keys) { _keys.Add(key); } SAPEventElement[] elements = new SAPEventElement[] { }; log("**************************"); var children = new List <SAPEventElement>(); GuiComponent last = null; foreach (string key in keys) { log(key.ToString()); if (string.IsNullOrEmpty(key)) { continue; } if (!key.Contains("?")) { var ele = session.FindById(key); if (ele == null) { continue; } last = ele as GuiComponent; // last = ele as GuiVComponent; // if(last != null) last.Visualize(true); var _msg = new SAPEventElement(ele, SystemName, "", false); children.Add(_msg); } } if (last != null) { foreach (string key in keys) { if (string.IsNullOrEmpty(key)) { continue; } if (key.Contains("?")) { if (LastElement == null) { continue; } var _key = key.Substring(0, key.IndexOf("?")); log(_key); var msg = new SAPEventElement(session, last, session.Info.SystemName, false, _key, "", false, false, 1, false); if (msg != null) { children.Add(msg); LastElement = msg; } } } } LastElement = children.LastOrDefault(); if (LastElement != null) { SAPEvent message = new SAPEvent("mousemove"); message.Set(LastElement); if (log_send_message) { form.AddText("[send] " + message.action + " " + LastElement.ToString() + " " + LastElement.Rectangle.ToString()); } pipe.PushMessage(message); } } } } // elements = children.ToArray(); //if (System.Threading.Monitor.TryEnter(_lock, 10000)) //{ // try // { // if (isMoving) return; // isMoving = true; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } //} //try //{ // if (SAPHook.Instance.Connections.Count() == 0 || SAPHook.Instance.UIElements.Count() == 0) // { // if (System.Threading.Monitor.TryEnter(_lock, 10000)) // { // try // { // isMoving = false; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } // } // return; // } // var Element = System.Windows.Automation.AutomationElement.FromPoint(new System.Windows.Point(e.X, e.Y)); // if (Element != null) // { // var ProcessId = Element.Current.ProcessId; // if (ProcessId < 1) // { // if (System.Threading.Monitor.TryEnter(_lock, 10000)) // { // try // { // isMoving = false; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } // } // return; // } // if (SAPProcessId > 0 && SAPProcessId != ProcessId) // { // if (System.Threading.Monitor.TryEnter(_lock, 10000)) // { // try // { // isMoving = false; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } // } // return; // } // if (SAPProcessId != ProcessId) // { // using (var p = System.Diagnostics.Process.GetProcessById(ProcessId)) // { // if (p.ProcessName.ToLower() == "saplogon") SAPProcessId = p.Id; // if (p.ProcessName.ToLower() != "saplogon") // { // if (System.Threading.Monitor.TryEnter(_lock, 10000)) // { // try // { // isMoving = false; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } // } // return; // } // } // } // if (SAPHook.Instance.Connections.Count() == 0) SAPHook.Instance.RefreshSessions(); // if (SAPHook.Instance.UIElements.Count() == 0) SAPHook.Instance.RefreshUIElements(true); // SAPEventElement[] elements = new SAPEventElement[] { }; // if (System.Threading.Monitor.TryEnter(SAPHook.Instance.UIElements, 10000)) // { // try // { // elements = SAPHook.Instance.UIElements.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); // } // finally // { // System.Threading.Monitor.Exit(SAPHook.Instance.UIElements); // } // } // if (elements.Count() > 0) // { // //Program.log("[mousemove] " + e.X + " " + e.Y); // //foreach(var ele in elements) // //{ // // Program.log("[element] " + ele.ToString()); // //} // var found = elements.OrderBy(x => x.IdPathCell.Length).Last(); // if (found.Items != null && found.Items.Length > 0) // { // elements = found.Items.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); // if (elements != null && elements.Length > 0) found = elements.OrderBy(x => x.IdPathCell.Length).Last(); // } // //Program.log("[element] " + found.ToString() + " " + found.Rectangle.ToString()); // if (found.Items != null && found.Items.Length > 0) // { // var found2 = found.Items.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); // if (found2.Length > 0) // { // found = found2.First(); // } // } // if (LastElement != null && (found.Id == LastElement.Id && found.Path == LastElement.Path && found.Cell == LastElement.Cell)) // { // // form.AddText("[SKIP] mousemove " + LastElement.ToString()); // if (System.Threading.Monitor.TryEnter(_lock, 10000)) // { // try // { // isMoving = false; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } // } // return; // } // LastElement = found; // SAPEvent message = new SAPEvent("mousemove"); // message.Set(LastElement); // if (log_send_message) form.AddText("[send] " + message.action + " " + LastElement.ToString() + " " + LastElement.Rectangle.ToString()); // pipe.PushMessage(message); // } // else // { // // log("Mouseover " + e.X + "," + e.Y + " not found in UI List"); // } // } //} //catch (Exception) //{ //} //if (System.Threading.Monitor.TryEnter(_lock, 10000)) //{ // try // { // isMoving = false; // } // finally // { // System.Threading.Monitor.Exit(_lock); // } //} } finally { _isMoving = false; } }