public static Color requestColorDialog(Control sender) { Color res = Color.Empty; ContextMenuStrip cm = new ContextMenuStrip(); var ColorDic = new Dictionary <string, Color>(); ColorDic.Add("Red", Color.Red); ColorDic.Add("Blue", Color.Blue); ColorDic.Add("Yellow", Color.Yellow); ColorDic.Add("Green", Color.Green); ColorDic.Add("Transparent", Color.FromArgb(0, 255, 255, 255)); foreach (KeyValuePair <string, Color> pair in ColorDic) { Bitmap bmp = new Bitmap(10, 10); for (int k = 0; k < bmp.Width; k++) { for (int v = 0; v < bmp.Height; v++) { bmp.SetPixel(k, v, pair.Value); } } cm.Items.Add(new ToolStripMenuItem(pair.Key, bmp, (s, e) => { res = pair.Value; })); } cm.Items.Add(new ToolStripMenuItem("Custom...", new Bitmap(1, 1), (s, e) => { res = oldDialogPicker(); })); cm.Show(sender, sender.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y))); cm.Focus(); while (cm.Visible == true) { Application.DoEvents(); } return(res); }
/// <summary> /// This method will show the supplied context menu at the mouse cursor, also makes sure it has focus and it's not visible in the taskbar. /// </summary> /// <param name="menu"></param> private static void ShowMenuAtCursor(ContextMenuStrip menu) { // find a suitable location Point location = Cursor.Position; Rectangle menuRectangle = new Rectangle(location, menu.Size); menuRectangle.Intersect(WindowCapture.GetScreenBounds()); if (menuRectangle.Height < menu.Height) { location.Offset(-40, -(menuRectangle.Height - menu.Height)); } else { location.Offset(-40, -10); } // This prevents the problem that the context menu shows in the task-bar User32.SetForegroundWindow(PluginUtils.Host.NotifyIcon.ContextMenuStrip.Handle); menu.Show(location); menu.Focus(); // Wait for the menu to close, so we can dispose it. while (true) { if (menu.Visible) { Application.DoEvents(); Thread.Sleep(100); } else { menu.Dispose(); break; } } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var menuStrip = new ContextMenuStrip(); var gear = Parser.ReadConfigs(); if (gear == null || gear.MenuItems == null || gear.MenuItems.Count == 0) { TurnOffAsync(); return; } menuStrip.Items.AddRange(gear.MenuItems.ToArray()); menuStrip.Closed += MenuStripClosed; menuStrip.Show(gear.StartX, gear.StartY); menuStrip.Focus(); menuStrip.Items[gear.SelectedIndex].Select(); Application.Run(); }
private void HkManagerOnKeyPressed(object sender, KeyPressedEventArgs e) { _menuStrip.Show(); if (_locationOfIcon != null) { _menuStrip.Location = (Point)_locationOfIcon; } _menuStrip.Focus(); }
protected override void OnClick(EventArgs e) { base.OnClick(e); if (m_menu == null) { return; } if (Parent != null) { Parent.Focus(); } m_menu.Show(this, new Point(1, Height)); m_menu.Focus(); }
public static void ShowMenuAtCursor(ContextMenuStrip menu) { // find a suitable location Point location = Cursor.Position; Rectangle menuRectangle = new Rectangle(location, menu.Size); menuRectangle.Intersect(WindowCapture.GetScreenBounds()); if (menuRectangle.Height < menu.Height) { location.Offset(-40, -(menuRectangle.Height - menu.Height)); } else { location.Offset(-40, -10); } menu.Show(location); menu.Focus(); }
public void ShowMenu() { ContextMenuStrip cms = new ContextMenuStrip() { Font = new Font("Arial", 10f), AutoClose = false }; cms.KeyUp += (sender, e) => { if (e.KeyCode == Keys.Escape) { cms.Close(); } }; ToolStripMenuItem tsmiContinue = new ToolStripMenuItem(Resources.QuickTaskMenu_ShowMenu_Continue); tsmiContinue.Image = Resources.control; tsmiContinue.Click += (sender, e) => { cms.Close(); OnTaskInfoSelected(null); }; cms.Items.Add(tsmiContinue); cms.Items.Add(new ToolStripSeparator()); if (Program.Settings != null && Program.Settings.QuickTaskPresets != null && Program.Settings.QuickTaskPresets.Count > 0) { foreach (QuickTaskInfo taskInfo in Program.Settings.QuickTaskPresets) { if (taskInfo.IsValid) { ToolStripMenuItem tsmi = new ToolStripMenuItem { Text = taskInfo.ToString().Replace("&", "&&"), Tag = taskInfo }; tsmi.Image = FindSuitableIcon(taskInfo); tsmi.Click += (sender, e) => { QuickTaskInfo selectedTaskInfo = ((ToolStripMenuItem)sender).Tag as QuickTaskInfo; cms.Close(); OnTaskInfoSelected(selectedTaskInfo); }; cms.Items.Add(tsmi); } else { cms.Items.Add(new ToolStripSeparator()); } } cms.Items[0].Select(); cms.Items.Add(new ToolStripSeparator()); } ToolStripMenuItem tsmiEdit = new ToolStripMenuItem(Resources.QuickTaskMenu_ShowMenu_Edit_this_menu___); tsmiEdit.Image = Resources.pencil; tsmiEdit.Click += (sender, e) => { cms.Close(); new QuickTaskMenuEditorForm().ShowDialog(); }; cms.Items.Add(tsmiEdit); cms.Items.Add(new ToolStripSeparator()); ToolStripMenuItem tsmiCancel = new ToolStripMenuItem(Resources.QuickTaskMenu_ShowMenu_Cancel); tsmiCancel.Image = Resources.cross; tsmiCancel.Click += (sender, e) => cms.Close(); cms.Items.Add(tsmiCancel); if (ShareXResources.UseCustomTheme) { ShareXResources.ApplyCustomThemeToContextMenuStrip(cms); } Point cursorPosition = CaptureHelpers.GetCursorPosition(); cursorPosition.Offset(-10, -10); cms.Show(cursorPosition); cms.Focus(); }
/// <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> public ExportInformation ShowPickerMenu(bool addDynamics, ISurface surface, ICaptureDetails captureDetails, IEnumerable <IDestination> destinations) { // Generate an empty ExportInformation object, for when nothing was selected. ExportInformation exportInformation = new ExportInformation(Designation, Language.GetString("settings_destination_picker")); var menu = new ContextMenuStrip { ImageScalingSize = CoreConfig.IconSize, Tag = null, TopLevel = true }; menu.Closing += delegate(object source, ToolStripDropDownClosingEventArgs eventArgs) { Log.DebugFormat("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.DebugFormat("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 += delegate { // in case the menu has been unfocused, focus again so that dropdown menus will still open on mouseenter if (!menu.ContainsFocus) { menu.Focus(); } }; foreach (IDestination destination in destinations) { // Fix foreach loop variable for the delegate ToolStripMenuItem item = destination.GetMenuItem(addDynamics, menu, delegate(object sender, EventArgs e) { ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem; IDestination clickedDestination = (IDestination)toolStripMenuItem?.Tag; if (clickedDestination == null) { return; } menu.Tag = clickedDestination.Designation; // Export exportInformation = clickedDestination.ExportCapture(true, surface, captureDetails); if (exportInformation != null && exportInformation.ExportMade) { Log.InfoFormat("Export to {0} success, closing menu", exportInformation.DestinationDescription); // close menu if the destination wasn't the editor menu.Close(); // 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("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); } } ); if (item != null) { menu.Items.Add(item); } } // Close menu.Items.Add(new ToolStripSeparator()); ToolStripMenuItem closeItem = new ToolStripMenuItem(Language.GetString("editor_close")) { Image = GreenshotResources.getImage("Close.Image") }; closeItem.Click += delegate { // This menu entry is the close itself, we can dispose the surface menu.Close(); if (!captureDetails.HasDestination("Editor")) { surface.Dispose(); surface = null; } }; menu.Items.Add(closeItem); ShowMenuAtCursor(menu); return(exportInformation); }
/// <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); }