Пример #1
0
        private object findControl(int x, int y)
        {
            //Point newPoint = this.PointToScreen(new Point(x, y));
            //POINT windowPoint = POINT.FromPoint(newPoint);
            POINT  windowPoint = new POINT(x, y);
            IntPtr found       = NativeUtils.WindowFromPoint(windowPoint);

            // we have a valid window handle
            if (found != IntPtr.Zero)
            {
                // give it another try, it might be a child window (disabled, hidden .. something else)
                // offset the point to be a client point of the active window
                if (NativeUtils.ScreenToClient(found, ref windowPoint))
                {
                    // check if there is some hidden/disabled child window at this point
                    IntPtr childWindow = NativeUtils.ChildWindowFromPoint(found, windowPoint);
                    if (childWindow != IntPtr.Zero)
                    { // great, we have the inner child
                        found = childWindow;
                    }
                }

                object c = Control.FromHandle(found);

                /*if (c is MenuStrip)//MenuStrip继承自ToolStrip
                 * {
                 *  MenuStrip m = c as MenuStrip;
                 *  c = m.GetItemAt(windowPoint.ToPoint());
                 * }
                 * else*/if (c is ToolStripDropDownMenu)
                {
                    ToolStripDropDownMenu m = c as ToolStripDropDownMenu;
                    c = m.GetItemAt(windowPoint.ToPoint());
                }
                else if (c is ToolStrip)
                {
                    ToolStrip m = c as ToolStrip;
                    c = m.GetItemAt(windowPoint.ToPoint());
                }

                return(c);
            }
            else
            {
                return(null);
            }
        }
        private void WindowFinder_MouseMove(object sender, MouseEventArgs e)
        {
            if (!searching)
            {
                EndSearch();
            }

            // Grab the window from the screen location of the mouse.
            Point  newPoint    = this.PointToScreen(new Point(e.X, e.Y));
            POINT  windowPoint = POINT.FromPoint(newPoint);
            IntPtr found       = NativeUtils.WindowFromPoint(windowPoint);

            // we have a valid window handle
            if (found != IntPtr.Zero)
            {
                // give it another try, it might be a child window (disabled, hidden .. something else)
                // offset the point to be a client point of the active window
                if (NativeUtils.ScreenToClient(found, ref windowPoint))
                {
                    // check if there is some hidden/disabled child window at this point
                    IntPtr childWindow = NativeUtils.ChildWindowFromPoint(found, windowPoint);
                    if (childWindow != IntPtr.Zero)
                    {                     // great, we have the inner child
                        found = childWindow;
                    }
                }
            }

            // Is this the same window as the last detected one?
            if (lastPoint != newPoint)
            {
                lastPoint = newPoint;
                if (window.SetWindowHandle(found, lastPoint))
                {
                    //Trace.WriteLine("FoundWindow:" + window.Name + ":" + window.Text + " Managed:" + window.IsManaged);
                    InvokeActiveWindowChanged();
                }
            }
        }