ShellStoreDialog.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using BluePointLilac.Controls;
  2. using BluePointLilac.Methods;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace ContextMenuManager.Controls
  9. {
  10. sealed class ShellStoreDialog : CommonDialog
  11. {
  12. public List<string> SelectedKeyNames { get; private set; }
  13. public List<string> IgnoredKeyNames { get; set; }
  14. public string ShellPath { get; set; }
  15. public override void Reset() { }
  16. protected override bool RunDialog(IntPtr hwndOwner)
  17. {
  18. using(ShellStoreForm frm = new ShellStoreForm(this.ShellPath, this.IgnoredKeyNames))
  19. {
  20. bool flag = frm.ShowDialog() == DialogResult.OK;
  21. if(flag) this.SelectedKeyNames = frm.SelectedKeyNames;
  22. return flag;
  23. }
  24. }
  25. public sealed class ShellStoreForm : Form
  26. {
  27. public string ShellPath { get; private set; }
  28. public List<string> IgnoredKeyNames { get; private set; }
  29. public List<string> SelectedKeyNames { get; private set; } = new List<string>();
  30. private bool IsPublic => ShellPath.Equals(ShellItem.CommandStorePath, StringComparison.OrdinalIgnoreCase);
  31. public ShellStoreForm(string shellPath, List<string> ignoredKeyNames)
  32. {
  33. this.ShellPath = shellPath;
  34. this.IgnoredKeyNames = ignoredKeyNames;
  35. this.AcceptButton = btnOk;
  36. this.CancelButton = btnCancel;
  37. this.Font = SystemFonts.MessageBoxFont;
  38. this.ShowIcon = this.ShowInTaskbar = false;
  39. this.StartPosition = FormStartPosition.CenterParent;
  40. this.MinimumSize = this.Size = new Size(652, 425).DpiZoom();
  41. this.Text = IsPublic ? AppString.Dialog.CheckReference : AppString.Dialog.CheckCopy;
  42. btnOk.Click += (sender, e) => GetSelectedItems();
  43. list.Owner = listBox;
  44. InitializeComponents();
  45. LoadItems();
  46. }
  47. readonly MyList list = new MyList();
  48. readonly MyListBox listBox = new MyListBox();
  49. readonly Panel pnlBorder = new Panel
  50. {
  51. BackColor = Color.FromArgb(200, 200, 200)
  52. };
  53. readonly Button btnOk = new Button
  54. {
  55. Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
  56. DialogResult = DialogResult.OK,
  57. Text = AppString.Dialog.Ok,
  58. AutoSize = true
  59. };
  60. readonly Button btnCancel = new Button
  61. {
  62. Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
  63. DialogResult = DialogResult.Cancel,
  64. Text = AppString.Dialog.Cancel,
  65. AutoSize = true
  66. };
  67. private void InitializeComponents()
  68. {
  69. this.Controls.AddRange(new Control[] { listBox, pnlBorder, btnOk, btnCancel });
  70. int a = 20.DpiZoom();
  71. listBox.Location = new Point(a, a);
  72. pnlBorder.Location = new Point(a - 1, a - 1);
  73. btnOk.Top = btnCancel.Top = this.ClientSize.Height - btnCancel.Height - a;
  74. btnCancel.Left = this.ClientSize.Width - btnCancel.Width - a;
  75. btnOk.Left = btnCancel.Left - btnOk.Width - a;
  76. this.OnResize(null);
  77. }
  78. protected override void OnResize(EventArgs e)
  79. {
  80. base.OnResize(e);
  81. listBox.Width = ClientSize.Width - 2 * listBox.Left;
  82. listBox.Height = btnOk.Top - 2 * listBox.Top;
  83. pnlBorder.Width = listBox.Width + 2;
  84. pnlBorder.Height = listBox.Height + 2;
  85. }
  86. private void LoadItems()
  87. {
  88. using(var shellKey = RegistryEx.GetRegistryKey(ShellPath))
  89. {
  90. Array.ForEach(Array.FindAll(shellKey.GetSubKeyNames(), itemName =>
  91. !IgnoredKeyNames.Contains(itemName, StringComparer.OrdinalIgnoreCase)), itemName =>
  92. {
  93. string regPath = $@"{ShellPath}\{itemName}";
  94. list.AddItem(new StoreShellItem(regPath, IsPublic));
  95. });
  96. }
  97. }
  98. private void GetSelectedItems()
  99. {
  100. foreach(StoreShellItem item in list.Controls)
  101. if(item.IsSelected) SelectedKeyNames.Add(item.KeyName);
  102. }
  103. }
  104. }
  105. sealed class StoreShellItem : ShellItem
  106. {
  107. public StoreShellItem(string regPath, bool isPublic, bool isSelect = true) : base(regPath)
  108. {
  109. this.IsPublic = isPublic;
  110. if(isSelect)
  111. {
  112. this.AddCtr(chkSelected, 40.DpiZoom());
  113. this.ContextMenuStrip = null;
  114. ChkVisible.Visible = BtnShowMenu.Visible = BtnSubItems.Visible = false;
  115. }
  116. RegTrustedInstaller.TakeRegTreeOwnerShip(regPath);
  117. }
  118. public bool IsSelected => chkSelected.Checked;
  119. public bool IsPublic { get; set; }
  120. readonly CheckBox chkSelected = new CheckBox { AutoSize = true };
  121. public override void DeleteMe()
  122. {
  123. if(IsPublic && MessageBoxEx.Show(AppString.Message.ConfirmDeleteReferenced,
  124. MessageBoxButtons.YesNo) != DialogResult.Yes) return;
  125. base.DeleteMe();
  126. }
  127. }
  128. }