/// <summary> /// Opens the shell context menu instead of the normal context menu on right click. /// </summary> static void OnContextMenuOpening(object sender, ContextMenuEventArgs e) { Control control = sender as Control; // Attempt to open the explorer context menu for the application. If the file does not exist or // there is some other problem then a context menu (defined in the XAML) with just "Edit Entry" // and "Remove Entry" is opened instead. if (control != null && control.DataContext is ApplicationViewModel) { ApplicationViewModel application = (ApplicationViewModel)control.DataContext; if (File.Exists(application.FilePath)) { ContextMenuWrapper cmw = new ContextMenuWrapper(); cmw.OnQueryMenuItems += (QueryMenuItemsEventHandler) delegate(object s, QueryMenuItemsEventArgs args) { args.ExtraMenuItems = new string[] { "Edit Dock Entry", "Remove Dock Entry", "---" }; args.GrayedItems = new string[] { "delete", "rename", "cut", "copy" }; args.HiddenItems = new string[] { "link" }; args.DefaultItem = 1; }; cmw.OnAfterPopup += (AfterPopupEventHandler) delegate(object s, AfterPopupEventArgs args) { Messenger.Default.Send <ShellContextMenuMessage>(ShellContextMenuMessage.Closed()); }; Messenger.Default.Send <ShellContextMenuMessage>(ShellContextMenuMessage.Opened()); try { FileSystemInfoEx[] files = new[] { FileInfoEx.FromString(application.FilePath) }; int[] position = Win32Mouse.GetMousePosition(); string command = cmw.Popup(files, new System.Drawing.Point(position[0], position[1])); // Handle the click on the 'ExtraMenuItems'. switch (command) { case "Edit Dock Entry": Messenger.Default.Send <ApplicationMessage>(ApplicationMessage.Edit(application)); break; case "Remove Dock Entry": Messenger.Default.Send <ApplicationMessage>(ApplicationMessage.Remove(application)); break; } e.Handled = true; // Don't open the normal context menu. } catch (Exception ex) { Debug.Print("Problem displaying shell context menu: {0}", ex); } } } }
public Window1() { InitializeComponent(); Debug.Write("tv.DataContext="); Debug.WriteLine(tv.DataContext); ContextMenuWrapper cmw = new ContextMenuWrapper(); cmw.OnQueryMenuItems += (QueryMenuItemsEventHandler) delegate(object s, QueryMenuItemsEventArgs args) { string firstCmd = @"Tools\&Add" + (firstOption ? "[*]" : ""); string secondCmd = @"Tools\Remove" + (secondOption ? "[*]" : ""); args.ExtraMenuItems = new string[] { firstCmd, @"Tools\---", secondCmd, "Again", "---" }; args.GrayedItems = new string[] { "delete", "rename", "cut", "copy" }; args.HiddenItems = new string[] { "link" }; args.DefaultItem = 1; }; cmw.OnMouseHover += (MouseHoverEventHandler) delegate(object s, MouseHoverEventArgs args) { sbar1.Text = args.Info == "" ? args.Command : args.Info; }; lv.PreviewMouseDown += (MouseButtonEventHandler) delegate(object sender, MouseButtonEventArgs args) { //Double Click if (args.LeftButton == MouseButtonState.Pressed && args.ClickCount == 2) { FileInfoEx selected = lv.SelectedValue as FileInfoEx; if (selected != null) { Process.Start(selected.FullName); } args.Handled = true; } }; lv.MouseUp += (MouseButtonEventHandler) delegate(object sender, MouseButtonEventArgs args) { if (args.ChangedButton == MouseButton.Right) { List <FileSystemInfoEx> files = new List <FileSystemInfoEx>(); foreach (object item in lv.SelectedItems) { files.Add((FileSystemInfoEx)item); } //FileSystemInfoEx file = lv.SelectedItem as FileSystemInfoEx; if (files.Count > 0) { Point pt = this.PointToScreen(args.GetPosition(null)); handleCommand(cmw.Popup(files.ToArray(), new System.Drawing.Point((int)pt.X, (int)pt.Y))); } } }; tv.MouseUp += (MouseButtonEventHandler) delegate(object sender, MouseButtonEventArgs args) { if (args.ChangedButton == MouseButton.Right) { DirectoryInfoEx dir = tv.SelectedItem as DirectoryInfoEx; if (dir != null) { Point pt = this.PointToScreen(args.GetPosition(null)); handleCommand(cmw.Popup(new FileSystemInfoEx[] { dir }, new System.Drawing.Point((int)pt.X, (int)pt.Y))); } } }; }
protected void SetupCommands(Func <ExModel[]> getSelectionFunc, Func <DirectoryModel> getCurrentFunc, Func <System.Drawing.Point> getMousePositionFunc) { #region OpenCommand and ContextMenuCommand OpenCommand = new SimpleRoutedCommand(ApplicationCommands.Open) { CanExecuteDelegate = x => { return(getCurrentFunc() != null); }, ExecuteDelegate = x => { Process.Start(getCurrentFunc().EmbeddedDirectoryEntry.FullName); } }; ContextMenuCommand = new SimpleRoutedCommand(ApplicationCommands.ContextMenu) { CanExecuteDelegate = x => { return(getSelectionFunc() != null && getSelectionFunc().Length > 0); }, ExecuteDelegate = x => { ContextMenuWrapper _cmw = new ContextMenuWrapper(); _cmw.OnBeforeInvokeCommand += (InvokeCommandEventHandler) delegate(object sender, InvokeCommandEventArgs args) { if (args.Command == "open") { args.ContinueInvoke = false; } if (args.Command == "openas" && args.SelectedItems != null && args.SelectedItems.Length == 1) { args.ContinueInvoke = false; } }; var selectedItems = (from model in getSelectionFunc() select model.EmbeddedEntry).ToArray(); System.Drawing.Point pt = getMousePositionFunc(); string command = _cmw.Popup(selectedItems, pt); switch (command) { case "open": OpenCommand.Execute(null); break; case "openas": OpenCommand.Execute(null); break; case "rename": RenameCommand.Execute(null); break; case "refresh": RefreshCommand.Execute(null); break; } } }; #endregion #region Delete, Copy and Paste DeleteCommand = new SimpleRoutedCommand(ApplicationCommands.Delete) { CanExecuteDelegate = x => { if (getSelectionFunc() != null && getSelectionFunc().Length > 0) { ; } { var selectedItems = (from model in getSelectionFunc() select model.EmbeddedEntry).ToArray(); foreach (FileSystemInfoEx item in selectedItems) { if ((item.Attributes & FileAttributes.ReadOnly) != 0) { return(false); } } return(true); } //return false; }, ExecuteDelegate = x => { var selectedItems = (from model in getSelectionFunc() select model.EmbeddedEntry).ToArray(); int itemCount = selectedItems.Length; if (System.Windows.Forms.MessageBox.Show(String.Format("Are you sure want to permanently remove these {0} item{1}?", itemCount, itemCount > 1 ? "s" : ""), "Delete", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { foreach (FileSystemInfoEx item in selectedItems) { item.Delete(); } } } }; CopyCommand = new SimpleRoutedCommand(ApplicationCommands.Copy) { CanExecuteDelegate = x => { return(getSelectionFunc() != null && getSelectionFunc().Length > 0); }, ExecuteDelegate = x => { var selectedItems = (from model in getSelectionFunc() select model.EmbeddedEntry).ToArray(); StringCollection fileList = new StringCollection(); foreach (FileSystemInfoEx item in selectedItems) { fileList.Add(item.FullName); } Clipboard.Clear(); Clipboard.SetFileDropList(fileList); } }; PasteCommand = new SimpleRoutedCommand(ApplicationCommands.Paste) { CanExecuteDelegate = x => { return (getCurrentFunc() != null && ((getCurrentFunc().EmbeddedDirectoryEntry.Attributes & FileAttributes.ReadOnly) != 0) && Clipboard.ContainsFileDropList()); }, ExecuteDelegate = x => { DirectoryInfoEx parentDir = getCurrentFunc().EmbeddedDirectoryEntry; List <FileSystemInfoEx> entryList = new List <FileSystemInfoEx>(); foreach (string path in Clipboard.GetFileDropList()) { IOTools.Copy(path, PathEx.Combine(parentDir.FullName, PathEx.GetFileName(path))); } } }; #endregion #region PropertiesCommand PropertiesCommand = new SimpleRoutedCommand(ApplicationCommands.Properties) { CanExecuteDelegate = x => { return(getSelectionFunc() != null && getSelectionFunc().Length > 0); }, ExecuteDelegate = x => { System.Windows.Point position = Mouse.GetPosition(null); var selectedItems = (from model in getSelectionFunc() select model.EmbeddedEntry).ToArray(); ContextMenuHelper.InvokeCommand(getSelectionFunc()[0].EmbeddedEntry.Parent, selectedItems, "properties", new System.Drawing.Point((int)position.X, (int)position.Y)); } }; #endregion }