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); } 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, false); 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); } } 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)); } } else if (comp is GuiStatusbar sbar) { 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)); } } else { throw new Exception("Unknown container type " + comp.Type + "!"); } } 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 OnMouseMove(InputEventArgs e) { System.Diagnostics.Trace.WriteLine("OnMouseMove: " + isMoving); if (isMoving) { return; } isMoving = true; try { if (SAPHook.Instance.Connections.Count() == 0) { return; } 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) { var p = System.Diagnostics.Process.GetProcessById(ProcessId); if (p.ProcessName.ToLower() == "saplogon") { SAPProcessId = p.Id; } if (p.ProcessName.ToLower() != "saplogon") { return; } } LastElement = Element; if (SAPHook.Instance.Connections.Count() == 0) { SAPHook.Instance.RefreshSessions(); } if (SAPHook.Instance.UIElements.Count() == 0) { SAPHook.Instance.RefreshUIElements(); } 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) { var last = elements.OrderBy(x => x.Id.Length).Last(); SAPEvent message = new SAPEvent("mousemove"); message.Set(last); form.AddText("[send] " + message.action + " " + last.Id); pipe.PushMessage(message); } else { log("Mouseover " + e.X + "," + e.Y + " not found in UI List"); } } } catch (Exception) { } 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); form.AddText("StartMonitorMouse::begin"); StartMonitorMouse(true); form.AddText("StartMonitorMouse::end"); } form.AddText("BeginRecord::begin"); SAPHook.Instance.BeginRecord(overlay); form.AddText("BeginRecord::end"); 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>(); bool dologin = true; if (SAPHook.Instance.Sessions != null) { foreach (var session in SAPHook.Instance.Sessions) { if (session.Info.SystemName.ToLower() == login.SystemName.ToLower()) { dologin = false; break; } } } if (dologin) { 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") { try { var msg = message.Get <SAPEventElement>(); var session = SAPHook.Instance.GetSession(msg.SystemName); var sapelements = new List <GuiComponent>(); msg.Id = StripSession(msg.Id); var paths = msg.Id.Split(new string[] { "/" }, StringSplitOptions.None); for (var i = 0; i < paths.Length; i++) { string part = paths[i]; int index = ExtractIndex(ref part); if (i == 0) { if (index > -1) { sapelements.Add(session.Children.ElementAt(index)); } if (index == -1) { for (var x = 0; x < session.Children.Count; x++) { sapelements.Add(session.Children.ElementAt(x)); } } } } } catch (Exception ex) { message.error = ex.Message; form.AddText("[send] " + message.action); pipe.PushMessage(message); } } 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) { msg.Id = null; message.Set(msg); form.AddText("[send] " + message.action); pipe.PushMessage(message); return; } // msg.Id = StripSession(msg.Id); GuiComponent comp = session.GetSAPComponentById <GuiComponent>(msg.Id); if (comp is null) { msg.Id = null; message.Set(msg); form.AddText("[send] " + message.action); pipe.PushMessage(message); return; } msg = new SAPEventElement(comp, session.Info.SystemName, msg.GetAllProperties, msg.Path, msg.Cell, msg.Flat, true, msg.MaxItem, msg.VisibleOnly); 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; if (ex.InnerException != null) { message.error = ex.InnerException.Message; } form.AddText("[send] " + message.action); pipe.PushMessage(message); } } } catch (Exception ex) { log(ex.ToString()); form.AddText(ex.ToString()); } }
private void Session_Change(GuiSession Session, GuiComponent Component, object CommandArray) { // if (Program.recordstarting) return; object[] objs = CommandArray as object[]; objs = objs[0] as object[]; var Action = "SetProperty"; switch (objs[0].ToString().ToLower()) { case "m": Action = "InvokeMethod"; break; case "sp": Action = "SetProperty"; break; } var ActionName = objs[1].ToString(); upperFirstChar(ref ActionName); if (ActionName == "ResizeWorkingPane") { return; } string id = Component.Id; var pathToRoot = new List <GuiComponent>(); GuiComponent element = Component; while (element != null) { Program.log(element.Id); if (element is GuiSession) { id = id.Substring(element.Id.Length + 1); } //var Type = element.Type; //GuiContainer container = element as GuiContainer; //if(container != null) //{ // var count = container.Children.Count; // for (int i = 0; i < count; i++) // { // GuiComponent comp = container.Children.ElementAt(i); // } //} //if (Type == "GuiToolbar") //{ // var menu = element as GuiToolbar; //} pathToRoot.Add(element); element = element.Parent as GuiComponent; } var e = new SAPRecordingEvent(); e.Action = Action; e.ActionName = ActionName; e.Name = Component.Name; e.Type = Component.Type; e.TypeAsNumber = Component.TypeAsNumber; e.ContainerType = Component.ContainerType; e.Id = id; try { if (objs.Length > 2) { var s = objs[1]; //e.Parameters = new object[objs.Length - 2]; ////objs.CopyTo(e.parameters, 3); //Array.Copy(objs, 2, e.Parameters, 0, e.Parameters.Length); //var _params = new List<SAPEventParameter>(); //for(var i = 2; i < objs.Length; i++) //{ // if (objs[i] != null) // { // _params.Add(new SAPEventParameter() { Value = objs[i], ValueType = objs[i].GetType().FullName }); // } // else // { // _params.Add(new SAPEventParameter() { Value = null, ValueType = typeof(object).FullName}); // } //} //e.Parameters = _params.ToArray(); var _temparr = new object[objs.Length - 2]; Array.Copy(objs, 2, _temparr, 0, _temparr.Length); e.Parameters = JsonConvert.SerializeObject(_temparr); } Program.log(e.Action + " " + e.ActionName + " " + e.Id); e.SystemName = Session.Info.SystemName; var msg = new SAPEvent("recorderevent"); msg.Set(e); Program.log("[send] " + msg.action); Program.pipe.PushMessage(msg); } catch (Exception ex) { Program.log(ex.ToString()); } // OnRecordEvent?.Invoke(Element); }
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 OnMouseDown(InputEventArgs e) { try { if (SAPHook.Instance.Connections.Count() == 0) { return; } 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[] { }; lock (SAPHook.Instance.UIElements) { elements = SAPHook.Instance.UIElements.Where(x => x.Rectangle.Contains(e.X, e.Y)).ToArray(); } 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 void Session_Change(GuiSession Session, GuiComponent Component, object CommandArray) { // if (Program.recordstarting) return; object[] objs = CommandArray as object[]; objs = objs[0] as object[]; var Action = "SetProperty"; switch (objs[0].ToString().ToLower()) { case "m": Action = "InvokeMethod"; break; case "sp": Action = "SetProperty"; break; } var ActionName = objs[1].ToString(); upperFirstChar(ref ActionName); string id = Component.Id; var pathToRoot = new List <GuiComponent>(); GuiComponent element = Component; while (element != null) { Program.log(element.Id); if (element is GuiSession) { id = id.Substring(element.Id.Length + 1); } pathToRoot.Add(element); element = element.Parent as GuiComponent; } var e = new SAPRecordingEvent(); e.Action = Action; e.ActionName = ActionName; e.Name = Component.Name; e.Type = Component.Type; e.TypeAsNumber = Component.TypeAsNumber; e.ContainerType = Component.ContainerType; e.Id = id; // /app/con[0]/ses[0]/wnd[0]/sbar/pane[0] var idarr = id.Split('/'); var sbarpath = idarr[0] + "/sbar/pane[0]"; GuiStatusPane sbar = Session.GetSAPComponentById <GuiStatusPane>(sbarpath); if (sbar != null) { e.StatusBarText = sbar.Text; } // StatusBarText try { if (objs.Length > 2) { var s = objs[1]; var _temparr = new object[objs.Length - 2]; Array.Copy(objs, 2, _temparr, 0, _temparr.Length); e.Parameters = JsonConvert.SerializeObject(_temparr); } Program.log(e.Action + " " + e.ActionName + " " + e.Id); e.SystemName = Session.Info.SystemName; var msg = new SAPEvent("recorderevent"); msg.Set(e); Program.log("[send] " + msg.action); Program.pipe.PushMessage(msg); } catch (Exception ex) { Program.log(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; } }