Example #1
0
        public static Rectangle Find(Point point, IntPtr hWnd)
        {
            var foregroundWindow = WindowManagerMethods.GetForegroundWindow();

            WindowManagerMethods.SetForegroundWindow(hWnd);
            var bmp = CaptureWindow(hWnd);

            if (Debug)
            {
                bmp.SetPixel(point.X, point.Y, DebugInverseColor(bmp.GetPixel(point.X, point.Y)));
                bmp.Save("screenshot.png");
            }
            WindowManagerMethods.SetForegroundWindow(foregroundWindow);
            Otsu.Process(bmp);
            if (Debug)
            {
                bmp.Save("otsu.png");
            }
            var floodFillArea = FloodFiller.FloodFill(bmp, point, Color.Red);

            if (Debug)
            {
                bmp.Save("floodfill.png");
            }
            return(new Rectangle(floodFillArea.MinX, floodFillArea.MinY, floodFillArea.MaxX - floodFillArea.MinX, floodFillArea.MaxY - floodFillArea.MinY));
        }
        protected override int EvaluatePoints(WindowHandle handle)
        {
            //Skip empty titles
            if (string.IsNullOrEmpty(handle.Title))
            {
                return(-1);
            }

            //Skip non top-level windows
            if (!WindowManagerMethods.IsTopLevel(handle.Handle))
            {
                return(-1);
            }

            string handleTitle = handle.Title.ToLowerInvariant();
            int    points      = 0;

            //Give points for partial match
            if (handleTitle.Equals(TitleMatch))
            {
                points += 20;
            }
            else if (handleTitle.StartsWith(TitleMatch))
            {
                points += 15;
            }
            else if (handleTitle.Contains(TitleMatch))
            {
                points += 10;
            }

            return(points);
        }
Example #3
0
        /// <summary>Returns the child control of a window corresponding to a screen location.</summary>
        /// <param name="parent">Parent window to explore.</param>
        /// <param name="scrClickLocation">Child control location in screen coordinates.</param>
        private static IntPtr GetRealChildControlFromPoint(IntPtr parent, NPoint scrClickLocation)
        {
            IntPtr curr = parent, child = IntPtr.Zero;

            do
            {
                child = WindowManagerMethods.RealChildWindowFromPoint(curr,
                                                                      WindowManagerMethods.ScreenToClient(curr, scrClickLocation));

                if (child == IntPtr.Zero || child == curr)
                {
                    break;
                }

                //Update for next loop
                curr = child;
            }while (true);

            //Safety check, shouldn't happen
            if (curr == IntPtr.Zero)
            {
                curr = parent;
            }

            return(curr);
        }
Example #4
0
        private bool InspectWindow(WindowHandle handle, Action <WindowHandle> addHandler)
        {
            //Code taken from: http://www.thescarms.com/VBasic/alttab.aspx

            //Reject empty titles
            if (string.IsNullOrEmpty(handle.Title))
            {
                return(true);
            }

            //Accept windows that
            // - are visible
            // - do not have a parent
            // - have no owner and are not Tool windows OR
            // - have an owner and are App windows
            if ((long)WindowManagerMethods.GetParent(handle.Handle) != 0)
            {
                return(true);
            }

            var hasOwner = (long)WindowManagerMethods.GetWindow(handle.Handle, WindowManagerMethods.GetWindowMode.GwOwner) != 0;
            var exStyle  = (WindowMethods.WindowExStyles)WindowMethods.GetWindowLong(handle.Handle, WindowMethods.WindowLong.ExStyle);

            if ((exStyle & WindowMethods.WindowExStyles.ToolWindow) == 0 && !hasOwner || //unowned non-tool window
                (exStyle & WindowMethods.WindowExStyles.AppWindow) == WindowMethods.WindowExStyles.AppWindow && hasOwner)
            {
                addHandler(handle);
            }

            return(true);
        }
        protected override bool InspectWindow(WindowHandle handle)
        {
            //Code taken from: http://www.thescarms.com/VBasic/alttab.aspx

            //Reject empty titles
            if (string.IsNullOrEmpty(handle.Title))
            {
                return(true);
            }

            //Accept windows that
            // - are visible
            // - do not have a parent
            // - have no owner and are not Tool windows OR
            // - have an owner and are App windows
            if ((long)WindowManagerMethods.GetParent(handle.Handle) == 0)
            {
                bool hasOwner = (long)WindowManagerMethods.GetWindow(handle.Handle, WindowManagerMethods.GetWindowMode.GW_OWNER) != 0;
                WindowMethods.WindowExStyles exStyle = (WindowMethods.WindowExStyles)WindowMethods.GetWindowLong(handle.Handle, WindowMethods.WindowLong.ExStyle);

                if (((exStyle & WindowMethods.WindowExStyles.ToolWindow) == 0 && !hasOwner) ||                                  //unowned non-tool window
                    ((exStyle & WindowMethods.WindowExStyles.AppWindow) == WindowMethods.WindowExStyles.AppWindow && hasOwner)) //owned application window

                {
                    _list.Add(handle);
                }
            }

            return(true);
        }
Example #6
0
        /// <summary>
        /// Gets a handle to the window that currently is in the foreground.
        /// </summary>
        /// <returns>May return null if call fails or no valid window selected.</returns>
        public static WindowHandle GetCurrentForegroundWindow()
        {
            IntPtr handle = WindowManagerMethods.GetForegroundWindow();

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            return(new WindowHandle(handle));
        }
Example #7
0
        /// <summary>Inject a fake left mouse click on a target window, on a location expressed in client coordinates.</summary>
        /// <param name="window">Target window to click on.</param>
        /// <param name="clickLocation">Location of the mouse click expressed in client coordiantes of the target window.</param>
        /// <param name="doubleClick">True if a double click should be injected.</param>
        public static void InjectFakeMouseClick(IntPtr window, CloneClickEventArgs clickArgs)
        {
            NPoint clientClickLocation = NPoint.FromPoint(clickArgs.ClientClickLocation);
            NPoint scrClickLocation    = WindowManagerMethods.ClientToScreen(window, clientClickLocation);

            //HACK (?)
            //If target window has a Menu (which appears on the thumbnail) move the clicked location down
            //in order to adjust (the menu isn't part of the window's client rect).
            IntPtr hMenu = WindowMethods.GetMenu(window);

            if (hMenu != IntPtr.Zero)
            {
                scrClickLocation.Y -= SystemInformation.MenuHeight;
            }

            IntPtr hChild            = GetRealChildControlFromPoint(window, scrClickLocation);
            NPoint clntClickLocation = WindowManagerMethods.ScreenToClient(hChild, scrClickLocation);

            if (clickArgs.Buttons == MouseButtons.Left)
            {
                if (clickArgs.IsDown && MouseButtonDown == false)
                {
                    InjectLeftMouseDown(hChild, clntClickLocation);
                    MouseButtonDown = true;
                }
                else if (clickArgs.IsDown && MouseButtonDown)
                {
                    InjectLeftMouseMove(hChild, clntClickLocation);
                    MouseButtonDown = true;
                }

                else if (clickArgs.IsDown == false)
                {
                    InjectLeftMouseUp(hChild, clntClickLocation);

                    MouseButtonDown = false;
                }
            }

            else if (clickArgs.Buttons == MouseButtons.Right)
            {
                if (clickArgs.IsDown)
                {
                    InjectRightMouseDown(hChild, clntClickLocation);
                }
                else if (clickArgs.IsDown == false)
                {
                    InjectRightMouseUp(hChild, clntClickLocation);
                }
            }
        }
        /// <summary>
        /// Attempts to locate a plugin region inside a window.
        /// </summary>
        /// <param name="handle">The handle to the parent window.</param>
        /// <returns>The region where a plugin window is located or null if none found.</returns>
        public Rectangle?LocatePluginRegion(WindowHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException();
            }

            WindowManagerMethods.EnumChildWindows(handle.Handle, LocatingWndProc, IntPtr.Zero);

            if (_selectedHandle != null)
            {
                Console.Out.WriteLine("Selected {0} '{1}' (class {2})", _selectedHandle.Handle, _selectedHandle.Title, _selectedHandle.Class);

                NRectangle rect;
                WindowMethods.GetWindowRect(_selectedHandle.Handle, out rect);

                NRectangle clientRect;
                WindowMethods.GetClientRect(_selectedHandle.Handle, out clientRect);

                Console.Out.WriteLine("WindowRect: {0}", rect);

                NRectangle ownerRect;
                WindowMethods.GetWindowRect(handle.Handle, out ownerRect);

                Console.Out.WriteLine("Owner WindowRect: {0}", ownerRect);

                var ret = new Rectangle {
                    X      = rect.Left - ownerRect.Left,
                    Y      = rect.Top - ownerRect.Top,
                    Width  = clientRect.Width,
                    Height = clientRect.Height
                };

                //Safety check (this may happen when the plugin client area is 0 pixel large)
                if (ret.Width < 0 || ret.Height < 0)
                {
                    return(null);
                }

                Console.Out.WriteLine("Selected region: {0}", ret);

                return(ret);
            }
            else
            {
                Console.Out.WriteLine("None found.");
                return(null);
            }
        }
Example #9
0
        public override void Refresh()
        {
            var windowsSnapshot = new List <WindowHandle>();

            WindowManagerMethods.EnumWindows((hwnd, lParam) => RefreshCallback(
                                                 hwnd,
                                                 handle =>
            {
                handle.ZOrder = windowsSnapshot.Count;
                windowsSnapshot.Add(handle);
            }),
                                             IntPtr.Zero);

            Windows = new ReadOnlyCollection <WindowHandle>(windowsSnapshot);
        }
Example #10
0
        private bool RefreshCallback(IntPtr hwnd, Action <WindowHandle> addHandler)
        {
            if (BlacklistedWindows.Contains(hwnd))
            {
                return(true);
            }

            if (SkipNotVisibleWindows && !WindowManagerMethods.IsWindowVisible(hwnd))
            {
                return(true);
            }

            var handle = new WindowHandle(hwnd);

            return(InspectWindow(handle, addHandler));
        }
Example #11
0
        protected override int EvaluatePoints(WindowHandle handle)
        {
            if (!WindowManagerMethods.IsTopLevel(handle.Handle))
            {
                return(-1);
            }

            int points = 0;

            //Class exact match
            if (!string.IsNullOrEmpty(Class))
            {
                string wndClass = handle.Class;
                if (wndClass.Equals(Class, StringComparison.InvariantCulture))
                {
                    points += 10;
                }
            }

            //Title match (may not be exact, but let's try)
            if (!string.IsNullOrEmpty(Title) && !string.IsNullOrEmpty(handle.Title))
            {
                if (handle.Title.StartsWith(Title, StringComparison.InvariantCultureIgnoreCase))
                {
                    points += 5;
                }
                if (handle.Title.Equals(Title, StringComparison.InvariantCultureIgnoreCase))
                {
                    points += 10;
                }
            }

            //Handle match (will probably not work, but anyhow)
            if (Handle != IntPtr.Zero)
            {
                if (Handle == handle.Handle)
                {
                    points += 10;
                }
            }

            return(points);
        }
Example #12
0
        private bool RefreshCallback(IntPtr hwnd, IntPtr lParam)
        {
            //Skip owner
            if (hwnd == OwnerHandle)
            {
                return(true);
            }

            if (SkipNotVisibleWindows && !WindowManagerMethods.IsWindowVisible(hwnd))
            {
                return(true);
            }

            //Extract basic properties
            string title  = WindowMethods.GetWindowText(hwnd);
            var    handle = new WindowHandle(hwnd, title);

            return(InspectWindow(handle));
        }
        private bool LocatingWndProc(IntPtr handle, IntPtr lParam)
        {
            //Skip non visible windows
            if (!WindowManagerMethods.IsWindowVisible(handle))
            {
                return(true);
            }

            //Class name check
            string cl = WindowMethods.GetWindowClass(handle);

            System.Diagnostics.Trace.WriteLine(string.Format("Child window, class {0}", cl));

            if (_pluginClassNames.Contains(cl))
            {
                //Found plugin window, stop now
                _selectedHandle = new WindowHandle(handle);
                return(false);
            }

            return(true);
        }
Example #14
0
 /// <summary>
 /// Forces a window list refresh.
 /// </summary>
 public virtual void Refresh()
 {
     WindowManagerMethods.EnumWindows(RefreshCallback, IntPtr.Zero);
 }