SendToItem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using BulePointLilac.Controls;
  2. using BulePointLilac.Methods;
  3. using Microsoft.Win32;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. namespace ContextMenuManager.Controls
  8. {
  9. sealed class SendToItem : MyListItem, IChkVisibleItem, IBtnShowMenuItem, ITsiTextItem, ITsiIconItem, ITsiWebSearchItem, ITsiFilePathItem, ITsiDeleteItem
  10. {
  11. private static readonly IWshRuntimeLibrary.WshShell WshShell = new IWshRuntimeLibrary.WshShell();
  12. public SendToItem(string filePath)
  13. {
  14. InitializeComponents();
  15. this.FilePath = filePath;
  16. }
  17. private string filePath;
  18. public string FilePath
  19. {
  20. get => filePath;
  21. set
  22. {
  23. filePath = value;
  24. if(IsShortcut) this.Shortcut = WshShell.CreateShortcut(value);
  25. this.Text = this.ItemText;
  26. this.Image = this.ItemIcon.ToBitmap();
  27. ChkVisible.Checked = this.ItemVisible;
  28. }
  29. }
  30. private IWshRuntimeLibrary.IWshShortcut Shortcut;
  31. private string FileName => Path.GetFileName(FilePath);
  32. private string FileExtension => Path.GetExtension(FilePath);
  33. private bool IsShortcut => FileExtension.ToLower() == ".lnk";
  34. public string SearchText => $"{AppString.SideBar.SendTo} {Text}";
  35. public string ItemFilePath
  36. {
  37. get
  38. {
  39. if(IsShortcut) return Shortcut.TargetPath;
  40. else
  41. {
  42. string guidPath = Registry.ClassesRoot.OpenSubKey(FileExtension)?.GetValue("")?.ToString();
  43. if(string.IsNullOrEmpty(guidPath)) return null;
  44. else return Registry.ClassesRoot.OpenSubKey($@"{guidPath}\InProcServer32")?.GetValue("")?.ToString();
  45. }
  46. }
  47. }
  48. public bool ItemVisible
  49. {
  50. get => (File.GetAttributes(FilePath) & FileAttributes.Hidden) != FileAttributes.Hidden;
  51. set
  52. {
  53. FileAttributes attributes = File.GetAttributes(FilePath);
  54. if(value) attributes &= ~FileAttributes.Hidden;
  55. else attributes |= FileAttributes.Hidden;
  56. File.SetAttributes(FilePath, attributes);
  57. }
  58. }
  59. public string ItemText
  60. {
  61. get
  62. {
  63. string name = SendToList.GetMenuName(FileName);
  64. if(name == string.Empty) name = Path.GetFileNameWithoutExtension(FilePath);
  65. if(name == string.Empty) name = FileExtension;
  66. return name;
  67. }
  68. set
  69. {
  70. DesktopIniHelper.SetLocalizedFileName(FilePath, value);
  71. ExplorerRestarter.NeedRestart = true;
  72. }
  73. }
  74. public Icon ItemIcon
  75. {
  76. get
  77. {
  78. Icon icon = null;
  79. if(IsShortcut)
  80. {
  81. icon = ResourceIcon.GetIcon(IconLocation, out string iconPath, out int iconIndex);
  82. IconPath = iconPath; IconIndex = iconIndex;
  83. if(icon == null)
  84. {
  85. if(File.Exists(Shortcut.TargetPath)) icon = Icon.ExtractAssociatedIcon(Shortcut.TargetPath);
  86. else if(Directory.Exists(Shortcut.TargetPath)) icon = ResourceIcon.GetFolderIcon(Shortcut.TargetPath);
  87. }
  88. }
  89. icon = icon ?? ResourceIcon.GetExtensionIcon(FileExtension);
  90. return icon;
  91. }
  92. }
  93. public string IconLocation
  94. {
  95. get
  96. {
  97. string location = Shortcut.IconLocation;
  98. if(location.StartsWith(",")) location = $"{Shortcut.TargetPath}{location}";
  99. return location;
  100. }
  101. set
  102. {
  103. Shortcut.IconLocation = value;
  104. Shortcut.Save();
  105. }
  106. }
  107. public string IconPath { get; set; }
  108. public int IconIndex { get; set; }
  109. public VisibleCheckBox ChkVisible { get; set; }
  110. public MenuButton BtnShowMenu { get; set; }
  111. public ChangeTextMenuItem TsiChangeText { get; set; }
  112. public ChangeIconMenuItem TsiChangeIcon { get; set; }
  113. public WebSearchMenuItem TsiSearch { get; set; }
  114. public FilePropertiesMenuItem TsiFileProperties { get; set; }
  115. public FileLocationMenuItem TsiFileLocation { get; set; }
  116. public DeleteMeMenuItem TsiDeleteMe { get; set; }
  117. readonly ToolStripSeparator TsiIconSeparator = new ToolStripSeparator();
  118. readonly ToolStripMenuItem TsiDetails = new ToolStripMenuItem(AppString.Menu.Details);
  119. private void InitializeComponents()
  120. {
  121. BtnShowMenu = new MenuButton(this);
  122. ChkVisible = new VisibleCheckBox(this);
  123. TsiChangeText = new ChangeTextMenuItem(this);
  124. TsiChangeIcon = new ChangeIconMenuItem(this);
  125. TsiSearch = new WebSearchMenuItem(this);
  126. TsiFileLocation = new FileLocationMenuItem(this);
  127. TsiFileProperties = new FilePropertiesMenuItem(this);
  128. TsiDeleteMe = new DeleteMeMenuItem(this);
  129. ContextMenuStrip.Opening += (sender, e) => TsiChangeIcon.Visible = TsiIconSeparator.Visible = IsShortcut;
  130. ContextMenuStrip.Items.AddRange(new ToolStripItem[] { TsiChangeText, new ToolStripSeparator(),
  131. TsiChangeIcon, TsiIconSeparator, TsiDetails, new ToolStripSeparator(), TsiDeleteMe });
  132. TsiDetails.DropDownItems.AddRange(new ToolStripItem[] { TsiSearch, new ToolStripSeparator(),
  133. TsiFileProperties, TsiFileLocation });
  134. }
  135. public void DeleteMe()
  136. {
  137. File.Delete(this.FilePath);
  138. DesktopIniHelper.DeleteLocalizedFileName(FilePath);
  139. this.Dispose();
  140. }
  141. }
  142. }