private FindControlResult FindControl(IntPtr hwnd, int x, int y) { var ctrl = Control.FromHandle(hwnd); if (ctrl == null) { return(new FindControlResult(Rectangle.Empty, null)); } var ctrlType = ctrl.GetType(); if (ctrlType == typeof(ToolStrip)) { var res = this.FindControl((ToolStrip)ctrl, x, y); return(res); } if (ctrlType == typeof(ToolStripDropDownMenu)) { var res = this.FindControl((ToolStripDropDownMenu)ctrl, x, y); return(res); } else { var inside = this.HandleControlInsideToolStripItem(ctrl); if (inside != null) { return(inside); } var bounds = ctrl.Parent?.RectangleToScreen(ctrl.Bounds) ?? ctrl.Bounds; var res = new FindControlResult(bounds, ctrl.Name ?? ctrlType.FullName); return(res); } }
private FindControlResult FindControl(ToolStripDropDownMenu ddm, int x, int y) { var p = ddm.PointToClient(new Point(x, y)); var item = GetAllChildren(ddm).FirstOrDefault(c => c.Bounds.Contains(p)); if (item == null) { var res = new FindControlResult(new Rectangle(), null); return(res); } else { var res = new FindControlResult(ddm.RectangleToScreen(item.Bounds), item.Name); return(res); } }
private FindControlResult FindControl(ToolStrip ts, int x, int y) { var p = ts.PointToClient(new Point(x, y)); var item = GetAllChildren(ts).FirstOrDefault(c => c.Bounds.Contains(p)); if (item == null) { var res = new FindControlResult(ts.Parent.RectangleToScreen(ts.Bounds), ts.Name); return(res); } else { var res = new FindControlResult(ts.RectangleToScreen(item.Bounds), item.Name); return(res); } }
// control inside ToolStripItem private FindControlResult HandleControlInsideToolStripItem(Control ctrl) { var ownerProperty = ctrl.GetType().GetProperty("Owner"); if (ownerProperty == null) { return(null); } if (!typeof(ToolStripItem).IsAssignableFrom(ownerProperty.PropertyType)) { return(null); } var owner = (ToolStripItem)ownerProperty.GetValue(ctrl); var result = new FindControlResult(owner.Owner.RectangleToScreen(owner.Bounds), owner.Name); return(result); }
private static void Send(IntPtr receiver, IntPtr controlHandle, FindControlResult fcr) { var data = new SendData { ControlHandle = controlHandle, ControlName = fcr.Name, Rectangle = fcr.Rectangle }; var dataPtr = IntPtrAlloc(data); var copyData = new COPYDATASTRUCT { dwData = new IntPtr(MessageIdentifier), cbData = Marshal.SizeOf(data), lpData = dataPtr }; var ptrCopyData = IntPtrAlloc(copyData); SendMessage(receiver, WM_COPYDATA, IntPtr.Zero, ptrCopyData); Marshal.FreeHGlobal(dataPtr); Marshal.FreeHGlobal(ptrCopyData); }