/// <summary>
        /// Populates the menu with a list of windows.
        /// </summary>
        /// <param name="menu">The menu to populate.</param>
        /// <param name="ownerForm">The owning form.</param>
        /// <param name="windowManager">The window manager that provides the windows list.</param>
        /// <param name="currentHandle">The currently used window (will be checked in the list).</param>
        /// <param name="clickHandler">Event handler for clicks on window items.</param>
        public static void PopulateMenu(this ToolStrip menu, Form ownerForm, BaseWindowSeeker windowManager,
                                        WindowHandle currentHandle, EventHandler clickHandler)
        {
            var regions = GetRegions();

            //Clear
            menu.Items.Clear();

            //"None" selection
            var nullTsi = new ToolStripMenuItem(Strings.MenuWindowsNone);
            nullTsi.Tag = null;
            nullTsi.Click += clickHandler;
            nullTsi.Checked = (currentHandle == null);
            menu.Items.Add(nullTsi);

            //Add an item for each window
            foreach (WindowHandle h in windowManager.Windows) {
                //Skip if in the same process
                if (h.Handle.Equals(ownerForm.Handle))
                    continue;

                var tsi = new ToolStripMenuItem();

                //Window title
                if (h.Title.Length > MaxWindowTitleLength) {
                    tsi.Text = h.Title.Substring(0, MaxWindowTitleLength) + "...";
                    tsi.ToolTipText = h.Title;
                }
                else
                    tsi.Text = h.Title;

                //Icon
                if (h.Icon != null) {
                    tsi.Image = h.Icon.ToBitmap();
                }

                //Check if this is the currently displayed window
                tsi.Checked = h.Equals(currentHandle);

                //Add direct click if no stored regions
                tsi.Tag = new WindowSelectionData {
                    Handle = h,
                    Region = null
                };
                tsi.Click += clickHandler;

                PopulateRegions(tsi, h, clickHandler, regions);

                menu.Items.Add(tsi);
            }
        }
        // This method that will be called when the thread is started
        public void Beta()
        {
            while (true)
            {
                Thread.Sleep(100);
                string line = Console.ReadLine();
                long result = Convert.ToInt64(line);
                IntPtr winId = new IntPtr(result);
                WindowHandle handle = new WindowHandle(winId, null);

                //IntPtr winId = new IntPtr(4981530);
                //WindowHandle handle = new WindowHandle(winId, null);
                Program._mainForm.SetThumbnail(handle, null);
            }
        }
        /// <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;
            }
        }
Exemple #4
0
        /// <summary>
        /// Creates a new thumbnail of a certain window.
        /// </summary>
        /// <param name="handle">Handle of the window to clone.</param>
        /// <param name="region">Optional region.</param>
        public void SetThumbnailHandle(WindowHandle handle, ThumbnailRegion region)
        {
            Log.WriteDetails("Setting new thumbnail",
                "HWND {0}, region {1}", handle, region
            );

            if (_thumbnail != null && !_thumbnail.IsInvalid) {
                _thumbnail.Close();
                _thumbnail = null;
            }

            //Attempt to get top level Form from Control
            Form owner = this.TopLevelControl as Form;
            if (owner == null)
                throw new Exception("Internal error: ThumbnailPanel.TopLevelControl is not a Form.");

            _labelGlass.Visible = false;

            //Register new thumbnail, update regioning directly and refresh thumbnail
            _thumbnail = DwmManager.Register(owner, handle.Handle);
            _currentRegion = region;
            _regionEnabled = (region != null);
            UpdateThubmnail();
        }
        /// <summary>
        /// Sets a new thumbnail.
        /// </summary>
        /// <param name="handle">Handle to the window to clone.</param>
        /// <param name="region">Region of the window to clone.</param>
        /// 
        public void SetThumbnail(WindowHandle handle, Rectangle? region)
        {
            try {
                CurrentThumbnailWindowHandle = handle;
                _thumbnailPanel.SetThumbnailHandle(handle);

            #if DEBUG
                string windowClass = WindowMethods.GetWindowClass(handle.Handle);
                Console.WriteLine("Cloning window HWND {0} of class {1}.", handle.Handle, windowClass);
            #endif

                if (region.HasValue)
                    _thumbnailPanel.SelectedRegion = region.Value;
                else
                    _thumbnailPanel.ConstrainToRegion = false;

                //Set aspect ratio (this will resize the form), do not refresh if in fullscreen
                //SetAspectRatio(_thumbnailPanel.ThumbnailOriginalSize, !IsFullscreen);
            }
            catch (Exception ex) {
                ThumbnailError(ex, false,"Unable to create thumbnails");
            }
        }
        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;
        }
Exemple #7
0
        /// <summary>
        /// Sets a new thumbnail.
        /// </summary>
        /// <param name="handle">Handle to the window to clone.</param>
        /// <param name="region">Region of the window to clone or null.</param>
        public void SetThumbnail(WindowHandle handle, ThumbnailRegion region)
        {
            try {
                System.Diagnostics.Trace.WriteLine(string.Format("Cloning window HWND {0} of class {1}.", handle.Handle, handle.Class));

                CurrentThumbnailWindowHandle = handle;
                _thumbnailPanel.SetThumbnailHandle(handle, region);

                //Set aspect ratio (this will resize the form), do not refresh if in fullscreen
                SetAspectRatio(_thumbnailPanel.ThumbnailPixelSize, !FullscreenManager.IsFullscreen);
            }
            catch (Exception ex) {
                System.Diagnostics.Trace.Fail("Unable to set thumbnail.", ex.ToString());

                ThumbnailError(ex, false, Strings.ErrorUnableToCreateThumbnail);
                _thumbnailPanel.UnsetThumbnail();
            }
        }
        private void PopulateRegionsDropdown(ToolStripMenuItem parent, WindowHandle parentHandle, StoredRegion[] regions)
        {
            parent.DropDownItems.Clear();

            //No region
            var nullRegionItem = new ToolStripMenuItem(Strings.MenuWindowsWholeRegion);
            nullRegionItem.Tag = parentHandle;
            nullRegionItem.Image = Resources.regions;
            nullRegionItem.Click += MenuWindowClickHandler;
            parent.DropDownItems.Add(nullRegionItem);

            //Video detector
            /*var detectorItem = new ToolStripMenuItem("Autodetect plugin");
            detectorItem.Tag = parentHandle;
            detectorItem.Click += MenuVideoCropperClickHandler;
            parent.DropDownItems.Add(detectorItem);*/

            //Regions (if any)
            if (regions == null || regions.Length == 0)
                return;

            parent.DropDownItems.Add(new ToolStripSeparator());

            foreach (StoredRegion region in regions) {
                var regionItem = new ToolStripMenuItem(region.Name);
                regionItem.Tag = new Tuple<WindowHandle, StoredRegion>(parentHandle, region);
                regionItem.Click += MenuRegionWindowClickHandler;

                parent.DropDownItems.Add(regionItem);
            }
        }
        /// <summary>
        /// Populates the menu with windows from the window seeker instance.
        /// </summary>
        /// <param name="currentSelection">Handle of the currently selected window or null if none selected.</param>
        private void PopulateMenu(WindowHandle currentSelection)
        {
            var regions = GetStoredRegions();

            _windowsMenu.Items.Clear();

            //"None" selection
            var nullTsi = new ToolStripMenuItem(Strings.MenuWindowsNone);
            nullTsi.Tag = null;
            nullTsi.Click += MenuWindowClickHandler;
            nullTsi.Checked = (currentSelection == null);
            _windowsMenu.Items.Add(nullTsi);

            //Add an item for each window
            foreach (WindowHandle h in WindowSeeker.Windows) {
                var tsi = new ToolStripMenuItem();

                //Window title
                if (h.Title.Length > MaxWindowTitleLength) {
                    tsi.Text = h.Title.Substring(0, MaxWindowTitleLength) + "...";
                    tsi.ToolTipText = h.Title;
                }
                else
                    tsi.Text = h.Title;

                //Icon
                if (h.Icon != null) {
                    try {
                        tsi.Image = h.Icon.ToBitmap();
                    }
                    catch (Exception) {
                        tsi.Image = null;
                    }
                }

                //Check if this is the currently displayed window
                tsi.Checked = h.Equals(currentSelection);

                //Click handler
                tsi.Tag = h;
                tsi.Click += MenuWindowClickHandler;

                PopulateRegionsDropdown(tsi, h, regions);

                _windowsMenu.Items.Add(tsi);
            }
        }
Exemple #10
0
        /// <summary>
        /// Creates a new thumbnail of a certain window.
        /// </summary>
        /// <param name="handle">Handle of the window to clone.</param>
        /// <param name="region">Optional region.</param>
        public void SetThumbnailHandle(WindowHandle handle, ThumbnailRegion region)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Setting thumbnail to handle {0}, with region {1}.", handle, region), "ThumbnailPanel");

            if (_thumbnail != null && !_thumbnail.IsInvalid) {
                _thumbnail.Close();
                _thumbnail = null;
            }

            //Get form and register thumbnail on it
            Form owner = this.TopLevelControl as Form;
            if(owner == null)
                throw new Exception("Internal error: ThumbnailPanel.TopLevelControl is not a Form.");

            _labelGlass.Visible = false;

            //Register new thumbnail, disable regioning directly and refresh
            _thumbnail = DwmManager.Register(owner, handle.Handle);
            _currentRegion = region;
            _regionEnabled = (region != null);
            UpdateThubmnail();
        }
Exemple #11
0
        /// <summary>
        /// Sets a new thumbnail.
        /// </summary>
        /// <param name="handle">Handle to the window to clone.</param>
        /// <param name="region">Region of the window to clone or null.</param>
        public void SetThumbnail(WindowHandle handle, ThumbnailRegion region)
        {
            try {
                Log.Write("Cloning window HWND {0} of class {1}", handle.Handle, handle.Class);

                CurrentThumbnailWindowHandle = handle;
                _thumbnailPanel.SetThumbnailHandle(handle, region);

                //Set aspect ratio (this will resize the form), do not refresh if in fullscreen
                SetAspectRatio(_thumbnailPanel.ThumbnailPixelSize, !FullscreenManager.IsFullscreen);
            }
            catch (Exception ex) {
                Log.WriteException("Unable to set new thumbnail", ex);

                ThumbnailError(ex, false, Strings.ErrorUnableToCreateThumbnail);
                _thumbnailPanel.UnsetThumbnail();
            }
        }
        /// <summary>
        /// Creates a new thumbnail of a certain window.
        /// </summary>
        /// <param name="handle">Handle of the window to clone.</param>
        public void SetThumbnailHandle(WindowHandle handle)
        {
            if (_thumbnail != null && !_thumbnail.IsInvalid)
                _thumbnail.Close();

            //Get form and register thumbnail on it
            Form owner = this.TopLevelControl as Form;
            if(owner == null)
                throw new Exception("Internal error: ThumbnailPanel.TopLevelControl is not a Form.");

            _labelGlass.Visible = false;

            _thumbnail = DwmManager.Register(owner, handle.Handle);
            ConstrainToRegion = false; //this also invokes a thumbnail update
        }
        private static void PopulateRegions(ToolStripMenuItem tsi, WindowHandle handle,
                                            EventHandler clickHandler, IEnumerable<StoredRegion> regions)
        {
            if (regions != null) {
                //Add subitem for no region
                var nullRegionItem = new ToolStripMenuItem(Strings.MenuWindowsWholeRegion);
                nullRegionItem.Tag = new WindowSelectionData {
                    Handle = handle,
                    Region = null
                };
                nullRegionItem.Image = Resources.regions;
                nullRegionItem.Click += clickHandler;
                tsi.DropDownItems.Add(nullRegionItem);

                foreach (StoredRegion region in regions) {
                    var regionItem = new ToolStripMenuItem(region.Name);
                    regionItem.Tag = new WindowSelectionData {
                        Handle = handle,
                        Region = region
                    };
                    regionItem.Click += clickHandler;

                    tsi.DropDownItems.Add(regionItem);
                }
            }
        }