Button AddMenuItem(MenuItemInfo menuItem) { ImageSource imageSource = null; if (!string.IsNullOrWhiteSpace(menuItem.IconPath)) { imageSource = SystemIcon.GetImageSource($"{AppDomain.CurrentDomain.BaseDirectory}Images/{menuItem.IconPath}"); } else if (System.IO.File.Exists(menuItem.FilePath)) { imageSource = SystemIcon.GetImageSource(true, menuItem.FilePath); } var btn = new Button() { ToolTip = menuItem.Name, Content = new Image() { Source = imageSource } }; btn.Click += (s, e) => RunExe(menuItem); return(btn); }
Button AddMenuItem(MenuItemInfo menuItem) { ImageSource imageSource = null; if (!string.IsNullOrWhiteSpace(menuItem.IconPath)) { imageSource = SystemIcon.GetImageSource($"{AppDomain.CurrentDomain.BaseDirectory}Images/{menuItem.IconPath}"); } else if (System.IO.File.Exists(menuItem.FilePath)) { imageSource = SystemIcon.GetImageSource(true, menuItem.FilePath); } StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Vertical; Image img = new Image { Source = imageSource, Width = 36, Height = 36, Tag = menuItem }; sp.Children.Add(img); TextBlock tb = new TextBlock { Text = menuItem.Name, HorizontalAlignment = HorizontalAlignment.Center }; sp.Children.Add(tb); var btn = new Button() { ToolTip = menuItem.Name, Content = sp }; btn.Click += (s, e) => RunExe(menuItem); return(btn); }
private void Grid_Drop(object sender, DragEventArgs e) { try { var fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); MenuItemInfo menuItem = new MenuItemInfo() { FilePath = fileName }; // 快捷方式需要获取目标文件路径 if (fileName.ToLower().EndsWith("lnk")) { WshShell shell = new WshShell(); IWshShortcut wshShortcut = (IWshShortcut)shell.CreateShortcut(fileName); menuItem.FilePath = wshShortcut.TargetPath; } ImageSource imageSource = SystemIcon.GetImageSource(true, menuItem.FilePath); System.IO.FileInfo file = new System.IO.FileInfo(fileName); if (string.IsNullOrWhiteSpace(file.Extension)) { menuItem.Name = file.Name; } else { menuItem.Name = file.Name.Substring(0, file.Name.Length - file.Extension.Length); } menuItem.Type = MenuItemType.Exe; if (ConfigHelper.AddNewMenuItem(menuItem)) { var btn = AddMenuItem(menuItem); fishButtons.Children.Add(btn); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }