Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions ContextMenuManager/BluePointLilac.Controls/MyListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ContextMenuManager.Methods;

namespace BluePointLilac.Controls
{
Expand Down Expand Up @@ -42,7 +43,46 @@ public MyList()
this.DoubleBuffered = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
}

// MyList 基类 控件仓库(缓存)
private Dictionary<string, MyCustomList<MyListItem>> itemRepo = new Dictionary<string, MyCustomList<MyListItem>>();
// 重置仓库的 Idx
protected void ResetRepo()
{
foreach (string className in itemRepo.Keys)
itemRepo[className].Idx = 0;
}
// 从仓库获取控件
protected bool GetFromRepo(string itemClassName,out MyListItem ret)
{
if (itemRepo.TryGetValue(itemClassName, out MyCustomList<MyListItem> itemlist))
{
for (int i = itemlist.Idx; i < itemlist.Count; i++)
{
MyListItem ctr = itemlist[i];
if (ctr.Parent == null)
{
ret = ctr;
itemlist.Idx = i + 1;
return true;
}
}
itemlist.Idx = itemlist.Count;
}
ret = null;
return false;
}
// 保存控件到缓存
protected void SaveToRepo(string itemClassName,MyListItem ctr)
{
if (!itemRepo.TryGetValue(itemClassName, out MyCustomList<MyListItem> itemlist)||itemlist==null)
{
itemlist = new MyCustomList<MyListItem>();
itemRepo[itemClassName] = itemlist;
}
itemlist.Add(ctr);
// 表示控件可重用
ctr.Reuse = true;
}
private MyListItem hoveredItem;
public MyListItem HoveredItem
{
Expand Down Expand Up @@ -117,6 +157,14 @@ public virtual void ClearItems()
{
Control ctr = this.Controls[i];
this.Controls.Remove(ctr);
// 不销毁 Reuse=true的控件
if (ctr is MyListItem myListItem)
{
if (myListItem.Reuse)
{
continue;
}
}
ctr.Dispose();
}
this.ResumeLayout();
Expand Down Expand Up @@ -168,7 +216,8 @@ public MyListItem()
AddCtr(pnlScrollbar, 0);
this.ResumeLayout();
}

// 需要重用,不能销毁(Dispose)
public bool Reuse { get; set; } = false;
public Image Image
{
get => picImage.Image;
Expand Down
3 changes: 2 additions & 1 deletion ContextMenuManager/ContextMenuManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>ContextMenuManager</RootNamespace>
<AssemblyName>ContextMenuManager</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
Expand Down Expand Up @@ -196,6 +196,7 @@
<Compile Include="BluePointLilac.Controls\UAWebClient.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Methods\MyUtils.cs" />
<Compile Include="Methods\UwpHelper.cs" />
<Compile Include="BluePointLilac.Methods\WinOsVersion.cs" />
<Compile Include="BluePointLilac.Methods\ShellLink.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ContextMenuManager/Controls/ShellExItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public ShellExItem(Guid guid, string regPath)
this.Guid = guid;
this.RegPath = regPath;
}
public void ReInit(Guid guid, string regPath1)
{
// InitializeComponents();
this.Guid = guid;
this.RegPath = regPath1;
}

private string regPath;
public string RegPath
Expand Down
7 changes: 6 additions & 1 deletion ContextMenuManager/Controls/ShellItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public ShellItem(string regPath)
InitializeComponents();
this.RegPath = regPath;
}

// 重新初始化
public void ReInit(string regPath1)
{
// InitializeComponents();
this.RegPath = regPath1;
}
private string regPath;
public string RegPath
{
Expand Down
37 changes: 34 additions & 3 deletions ContextMenuManager/Controls/ShellList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,37 @@ private static string CurrentExtensionPerceivedType
private static string GetPerceivedType(string extension) => Registry.GetValue($@"{RegistryEx.CLASSES_ROOT}\{extension}", "PerceivedType", null)?.ToString();

public Scenes Scene { get; set; }

// 获取一个实例,可能是新建的或重用的; 先从仓库(缓存)中获取,没有则新建
private void Gen_ShellItem(out ShellItem ret,string regPath)
{
if (this.GetFromRepo("ShellItem",out MyListItem myListItem))
{
ret = (ShellItem)myListItem;
// 更新控件信息
ret.ReInit(regPath);
}
else
{
ret = new ShellItem(regPath);
this.SaveToRepo("ShellItem", ret);
}
}
private void Gen_ShellExItem(out ShellExItem ret,Guid guid, string regPath)
{
if (this.GetFromRepo("ShellExItem",out MyListItem myListItem))
{
ret = (ShellExItem)myListItem;
ret.ReInit(guid,regPath);
}
else
{
ret = new ShellExItem(guid,regPath);
this.SaveToRepo( "ShellExItem", ret);
}
}
public void LoadItems()
{
ResetRepo();
string scenePath = null;
switch(Scene)
{
Expand Down Expand Up @@ -296,7 +324,9 @@ private void LoadShellItems(string shellPath)
RegTrustedInstaller.TakeRegTreeOwnerShip(shellKey.Name);
foreach(string keyName in shellKey.GetSubKeyNames())
{
this.AddItem(new ShellItem($@"{shellPath}\{keyName}"));
// this.AddItem(new ShellItem($@"{shellPath}\{keyName}"));
this.Gen_ShellItem(out ShellItem shellItem,$@"{shellPath}\{keyName}");
this.AddItem(shellItem);
}
}
}
Expand All @@ -321,7 +351,8 @@ private void LoadShellExItems(string shellExPath)
string keyName = RegistryEx.GetKeyName(path);
if(!names.Contains(keyName))
{
ShellExItem item = new ShellExItem(dic[path], path);
// ShellExItem item = new ShellExItem(dic[path], path);
this.Gen_ShellExItem(out ShellExItem item, dic[path], path);
if(groupItem != null)
{
item.FoldGroupItem = groupItem;
Expand Down
24 changes: 24 additions & 0 deletions ContextMenuManager/Methods/MyUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace ContextMenuManager.Methods
{
// List带一个Idx属性
public class MyCustomList<T>: List<T>
{

public MyCustomList():base(100)
{
}
public MyCustomList(int n):base(n)
{
}
// 列表迭代时起点索引
private int idx=0;

public int Idx
{
get => idx;
set => idx = value;
}
}
}