Esempio n. 1
0
        public FormExtendsDpiAwareForm()
        {
            InitializeComponent();

            _contextMenuDpiHandler = contextMenuStrip1.AttachDpiHandler();

            _dpiChangeSubscription = _contextMenuDpiHandler.OnDpiChanged.Subscribe(dpi =>
            {
                Log.Info().WriteLine("ContextMenuStrip DPI: {0}", dpi.NewDpi);
            });

            // TODO: Create a "SizeScaleHandler" or something
            var initialMenuStripSize = menuStrip1.ImageScalingSize;

            FormDpiHandler.OnDpiChanged.Subscribe(dpiChangeInfo =>
            {
                menuStrip1.ImageScalingSize = DpiHandler.ScaleWithDpi(initialMenuStripSize, dpiChangeInfo.NewDpi);
            });

            _scaleHandler = BitmapScaleHandler.WithComponentResourceManager <Bitmap>(FormDpiHandler, GetType(), BitmapScaleHandler.SimpleBitmapScaler)
                            .AddTarget(somethingMenuItem, "somethingMenuItem.Image", b => b)
                            .AddTarget(something2MenuItem, "something2MenuItem.Image", b => b);

            EnvironmentMonitor.EnvironmentUpdateEvents.Subscribe(args =>
            {
                Log.Info().WriteLine("{0} - {1}", args.SystemParametersInfoAction, args.Area);
                MessageBox.Show(this, $@"{args.SystemParametersInfoAction} - {args.Area}", @"Change!");
            });
        }
        public FormWithAttachedDpiHandler()
        {
            FormDpiHandler = this.AttachDpiHandler();
            InitializeComponent();

            var initialMenuStripSize = menuStrip1.ImageScalingSize;

            FormDpiHandler.OnDpiChanged.Subscribe(dpiChangeInfo =>
            {
                menuStrip1.ImageScalingSize = DpiHandler.ScaleWithDpi(initialMenuStripSize, dpiChangeInfo.NewDpi);
            });

            _scaleHandler = BitmapScaleHandler.WithComponentResourceManager <Bitmap>(FormDpiHandler, GetType(), BitmapScaleHandler.SimpleBitmapScaler)
                            .AddTarget(somethingMenuItem, "somethingMenuItem.Image", b => b)
                            .AddTarget(something2MenuItem, "something2MenuItem.Image", b => b);

            // This can be used to do something with DPI changes, subscription should be disposed!
            _dpiChangeSubscription = FormDpiHandler.OnDpiChanged.Subscribe(dpi =>
            {
                Log.Info().WriteLine("New DPI: {0}", dpi);
            });

            EnvironmentMonitor.EnvironmentUpdateEvents.Subscribe(args =>
            {
                Log.Info().WriteLine("{0} - {1}", args.SystemParametersInfoAction, args.Area);
                MessageBox.Show(this, $@"{args.SystemParametersInfoAction} - {args.Area}", @"Change!");
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Setup the Bitmap scaling (for icons)
        /// </summary>
        private void SetupBitmapScaleHandler()
        {
            ContextMenuDpiHandler = contextMenu.AttachDpiHandler();

            var dpiChangeSubscription = DpiHandler.OnDpiChangeInfo.Subscribe(info =>
            {
                switch (info.DpiChangeEventType)
                {
                case DpiChangeEventTypes.Before:
                    // Change the ImageScalingSize before setting the bitmaps
                    var width = DpiHandler.ScaleWithDpi(coreConfiguration.IconSize.Width, info.NewDpi);
                    var size  = new Size(width, width);
                    contextMenu.SuspendLayout();
                    contextMenu.ImageScalingSize   = size;
                    contextmenu_quicksettings.Size = new Size(170, width + 8);
                    break;

                case DpiChangeEventTypes.After:
                    // Redraw the form
                    contextMenu.ResumeLayout(true);
                    contextMenu.Refresh();
                    notifyIcon.Icon = GreenshotResources.GetGreenshotIcon();
                    break;
                }
            });

            var contextMenuResourceScaleHandler = BitmapScaleHandler.WithComponentResourceManager(ContextMenuDpiHandler, GetType(), (bitmap, dpi) => bitmap.ScaleIconForDisplaying(dpi));

            contextMenuResourceScaleHandler.AddTarget(contextmenu_capturewindow, "contextmenu_capturewindow.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_capturearea, "contextmenu_capturearea.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_capturelastregion, "contextmenu_capturelastregion.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_capturefullscreen, "contextmenu_capturefullscreen.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_captureclipboard, "contextmenu_captureclipboard.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_openfile, "contextmenu_openfile.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_settings, "contextmenu_settings.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_help, "contextmenu_help.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_donate, "contextmenu_donate.Image");
            contextMenuResourceScaleHandler.AddTarget(contextmenu_exit, "contextmenu_exit.Image");

            // this is special handling, for the icons which come from the executables
            var exeBitmapScaleHandler = BitmapScaleHandler.Create <string>(ContextMenuDpiHandler,
                                                                           (path, dpi) => PluginUtils.GetCachedExeIcon(path, 0, dpi >= 120),
                                                                           (bitmap, dpi) => bitmap.ScaleIconForDisplaying(dpi));

            exeBitmapScaleHandler.AddTarget(contextmenu_captureie, PluginUtils.GetExePath("iexplore.exe"));

            // Add cleanup
            Application.ApplicationExit += (sender, args) =>
            {
                dpiChangeSubscription.Dispose();
                ContextMenuDpiHandler.Dispose();
                contextMenuResourceScaleHandler.Dispose();
                exeBitmapScaleHandler.Dispose();
            };
        }
Esempio n. 4
0
        public FormExtendsDpiAwareForm()
        {
            InitializeComponent();

            _contextMenuDpiHandler = contextMenuStrip1.AttachDpiHandler();

            _contextMenuDpiHandler.OnDpiChanged.Subscribe(dpi =>
            {
                Log.Info().WriteLine("ContextMenuStrip DPI: {0}", dpi);
            });
            ScaleHandler = BitmapScaleHandler.WithComponentResourceManager(DpiHandler, GetType(), ScaleIconForDisplaying);

            ScaleHandler.AddTarget(somethingMenuItem, "somethingMenuItem.Image");
            ScaleHandler.AddTarget(something2MenuItem, "something2MenuItem.Image");

            EnvironmentMonitor.EnvironmentUpdateEvents.Subscribe(args =>
            {
                Log.Info().WriteLine("{0} - {1}", args.SystemParametersInfoAction, args.Area);
                MessageBox.Show(this, $"{args.SystemParametersInfoAction} - {args.Area}", "Change!");
            });
        }
Esempio n. 5
0
        public FormWithAttachedDpiHandler()
        {
            _dpiHandler = this.AttachDpiHandler();
            InitializeComponent();
            ScaleHandler = BitmapScaleHandler.WithComponentResourceManager(_dpiHandler, GetType(), ScaleIconForDisplaying);

            // This can be used to do something with DPI changes, subscription should be disposed!
            _dpiChangeSubscription = _dpiHandler.OnDpiChanged.Subscribe(dpi =>
            {
                var width = DpiHandler.ScaleWithDpi(20, dpi);
                var size  = new Size(width, width);
                //menuStrip1.ImageScalingSize = size;
            });


            ScaleHandler.AddTarget(somethingMenuItem, "somethingMenuItem.Image");
            ScaleHandler.AddTarget(something2MenuItem, "something2MenuItem.Image");

            EnvironmentMonitor.EnvironmentUpdateEvents.Subscribe(args =>
            {
                Log.Info().WriteLine("{0} - {1}", args.SystemParametersInfoAction, args.Area);
                MessageBox.Show(this, $"{args.SystemParametersInfoAction} - {args.Area}", "Change!");
            });
        }
Esempio n. 6
0
        /// <summary>
        ///     This method will create and show the destination picker menu
        /// </summary>
        /// <param name="addDynamics">Boolean if the dynamic values also need to be added</param>
        /// <param name="surface">The surface which can be exported</param>
        /// <param name="captureDetails">Details for the surface</param>
        /// <param name="destinations">The list of destinations to show</param>
        /// <returns></returns>
        protected ExportInformation ShowPickerMenu(bool addDynamics, ISurface surface, ICaptureDetails captureDetails, IEnumerable <IDestination> destinations)
        {
            var menu = new ContextMenuStrip
            {
                Tag      = null,
                TopLevel = true
            };
            var dpiHandler         = menu.AttachDpiHandler();
            var bitmapScaleHandler = BitmapScaleHandler.Create <IDestination>(
                dpiHandler,
                (destination, dpi) => destination.GetDisplayIcon(dpi),
                (bitmap, d) => bitmap.ScaleIconForDisplaying(d));

            dpiHandler.OnDpiChanged.Subscribe(dpi =>
            {
                var width             = DpiHandler.ScaleWithDpi(CoreConfiguration.IconSize.Width, dpi);
                var size              = new Size(width, width);
                menu.ImageScalingSize = size;
            });

            // Generate an empty ExportInformation object, for when nothing was selected.
            var exportInformation = new ExportInformation("", GreenshotLanguage.SettingsDestinationPicker);

            menu.Closing += (source, eventArgs) =>
            {
                Log.Debug().WriteLine("Close reason: {0}", eventArgs.CloseReason);
                switch (eventArgs.CloseReason)
                {
                case ToolStripDropDownCloseReason.AppFocusChange:
                    if (menu.Tag == null)
                    {
                        // Do not allow the close if the tag is not set, this means the user clicked somewhere else.
                        eventArgs.Cancel = true;
                    }
                    else
                    {
                        Log.Debug().WriteLine("Letting the menu 'close' as the tag is set to '{0}'", menu.Tag);
                    }
                    break;

                case ToolStripDropDownCloseReason.ItemClicked:
                case ToolStripDropDownCloseReason.CloseCalled:
                    // The ContextMenuStrip can be "closed" for these reasons.
                    break;

                case ToolStripDropDownCloseReason.Keyboard:
                    // Dispose as the close is clicked
                    if (!captureDetails.HasDestination("Editor"))
                    {
                        surface.Dispose();
                        surface = null;
                    }
                    break;

                default:
                    eventArgs.Cancel = true;
                    break;
                }
            };
            menu.MouseEnter += (sender, args) =>
            {
                // in case the menu has been unfocused, focus again so that dropdown menus will still open on mouseenter
                if (!menu.ContainsFocus)
                {
                    menu.Focus();
                }
            };
            foreach (var destination in destinations)
            {
                // Fix foreach loop variable for the delegate
                var item = destination.GetMenuItem(addDynamics, menu,
                                                   async(sender, e) =>
                {
                    var toolStripMenuItem  = sender as ToolStripMenuItem;
                    var clickedDestination = (IDestination)toolStripMenuItem?.Tag;
                    if (clickedDestination == null)
                    {
                        return;
                    }
                    menu.Tag = clickedDestination.Designation;
                    // Export
                    exportInformation = await clickedDestination.ExportCaptureAsync(true, surface, captureDetails);
                    if (exportInformation != null && exportInformation.ExportMade)
                    {
                        Log.Info().WriteLine("Export to {0} success, closing menu", exportInformation.DestinationDescription);
                        // close menu
                        menu.Close();
                        menu.Dispose();
                        // Cleanup surface, only if there is no editor in the destinations and we didn't export to the editor
                        if (!captureDetails.HasDestination("Editor") && !"Editor".Equals(clickedDestination.Designation))
                        {
                            surface.Dispose();
                            surface = null;
                        }
                    }
                    else
                    {
                        Log.Info().WriteLine("Export cancelled or failed, showing menu again");

                        // Make sure a click besides the menu don't close it.
                        menu.Tag = null;

                        // This prevents the problem that the context menu shows in the task-bar
                        ShowMenuAtCursor(menu);
                    }
                }, bitmapScaleHandler
                                                   );
                if (item != null)
                {
                    menu.Items.Add(item);
                }
            }
            // Close
            menu.Items.Add(new ToolStripSeparator());
            var closeItem = new ToolStripMenuItem(GreenshotLanguage.Close)
            {
                Image = GreenshotResources.GetBitmap("Close.Image")
            };

            closeItem.Click += (sender, args) =>
            {
                // This menu entry is the close itself, we can dispose the surface
                menu.Close();
                menu.Dispose();
                // Only dispose if there is a destination which keeps the capture
                if (captureDetails.HasDestination("Editor"))
                {
                    return;
                }

                surface.Dispose();
                surface = null;
            };
            menu.Items.Add(closeItem);

            ShowMenuAtCursor(menu);
            return(exportInformation);
        }
Esempio n. 7
0
        /// <summary>
        ///     Return a menu item
        /// </summary>
        /// <param name="addDynamics">bool is dynamic entries need to be added</param>
        /// <param name="menu">ContextMenuStrip</param>
        /// <param name="destinationClickHandler">EventHandler</param>
        /// <param name="scaleHandler">BitmapScaleHandler</param>
        /// <returns>ToolStripMenuItem</returns>
        public virtual ToolStripMenuItem GetMenuItem(bool addDynamics, ContextMenuStrip menu, EventHandler destinationClickHandler, BitmapScaleHandler <IDestination> scaleHandler)
        {
            var basisMenuItem = new ToolStripMenuItem(Description)
            {
                Tag  = this,
                Text = Description
            };

            scaleHandler.AddTarget(basisMenuItem, this);

            AddTagEvents(basisMenuItem, menu, Description);
            basisMenuItem.Click -= destinationClickHandler;
            basisMenuItem.Click += destinationClickHandler;

            if (IsDynamic && addDynamics)
            {
                basisMenuItem.DropDownOpening += (sender, args) =>
                {
                    if (basisMenuItem.DropDownItems.Count != 0)
                    {
                        return;
                    }

                    var subDestinations = new List <IDestination>();
                    // Fixing Bug #3536968 by catching the COMException (every exception) and not displaying the "subDestinations"
                    try
                    {
                        subDestinations.AddRange(DynamicDestinations());
                    }
                    catch (Exception ex)
                    {
                        Log.Error().WriteLine("Skipping {0}, due to the following error: {1}", Description, ex.Message);
                    }

                    if (subDestinations.Count <= 0)
                    {
                        return;
                    }

                    if (UseDynamicsOnly && subDestinations.Count == 1)
                    {
                        basisMenuItem.Tag    = subDestinations[0];
                        basisMenuItem.Text   = subDestinations[0].Description;
                        basisMenuItem.Click -= destinationClickHandler;
                        basisMenuItem.Click += destinationClickHandler;
                    }
                    else
                    {
                        foreach (var subDestination in subDestinations)
                        {
                            var destinationMenuItem = new ToolStripMenuItem(subDestination.Description)
                            {
                                Tag = subDestination,
                            };
                            scaleHandler.AddTarget(destinationMenuItem, subDestination);

                            destinationMenuItem.Click += destinationClickHandler;
                            AddTagEvents(destinationMenuItem, menu, subDestination.Description);
                            basisMenuItem.DropDownItems.Add(destinationMenuItem);
                        }
                    }
                };
            }

            return(basisMenuItem);
        }
Esempio n. 8
0
 /// <summary>
 /// Default constructor, for default functionality like DPI awareness
 /// </summary>
 protected GreenshotForm()
 {
     // Add the Dapplo.Windows DPI change handler
     ScaleHandler = BitmapScaleHandler.WithComponentResourceManager(DpiHandler, GetType(), (bitmap, dpi) => bitmap.ScaleIconForDisplaying(dpi));
 }