-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
176 lines (163 loc) · 5.81 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
#nullable enable
namespace BuyiToolbar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DirectoryInfo? theDir = null;
private void Form1_Load(object sender, EventArgs e)
{
try
{
var args = Environment.GetCommandLineArgs();
if (args.Length < 2)
{
throw new Exception($"你没有输入指定的文件夹的路径。 需要作为启动项的第一个参数");
}
var path = args[1].Trim().Trim('/', '\\', '\'', '\"');
if (string.IsNullOrEmpty(path))
{
throw new Exception($"无法识别的文件夹名字");
}
var dir = new DirectoryInfo(path);
if (!dir.Exists)
{
throw new Exception($"文件夹不存在 {dir.FullName}");
}
theDir = dir;
BgIcon.Icon = CreateIconFromText(dir.Name.Substring(0, 1));
this.Icon = BgIcon.Icon;
BgIcon.Visible = true;
BuildMenu(MainMenu.Items, theDir);
}
catch (Exception ex)
{
PopError(ex);
BgIcon.Visible = false;
Process.GetCurrentProcess().Kill();
}
}
private const int limitItems = 49;
private void BuildMenu(ToolStripItemCollection parent, DirectoryInfo dir)
{
parent.Clear();
dir.Refresh();
if (!dir.Exists) { return; }
Bitmap? explorerIcon = null;
foreach (var f in dir.EnumerateDirectories())
{
if (f.Attributes.HasFlag(FileAttributes.Hidden)) { continue; }
var v = new ToolStripMenuItem();
v.Text = f.Name;
if (explorerIcon == null)
{
explorerIcon = GetIconBitmap("C:/Windows/explorer.exe");
}
v.Image = explorerIcon;
v.Tag = f.FullName;
v.Click += OnMenuItemClick;
parent.Add(v);
var theBlank = new ToolStripMenuItem();
theBlank.Text = "loading...";
v.DropDownItems.Add(theBlank);
v.DropDownOpening += (_, _) =>
{
BuildMenu(v.DropDownItems, f);
};
if (parent.Count > limitItems) { break; }
}
foreach (var f in dir.EnumerateFiles())
{
if (f.Attributes.HasFlag(FileAttributes.Hidden)) { continue; }
var v = new ToolStripMenuItem();
if (f.Name.EndsWith(".lnk", StringComparison.CurrentCultureIgnoreCase))
{
v.Text = Path.GetFileNameWithoutExtension(f.Name);
}
else
{
v.Text = f.Name;
}
v.Image = GetIconBitmap(f.FullName);
v.Click += OnMenuItemClick;
v.Tag = f.FullName;
parent.Add(v);
if (parent.Count > limitItems) { break; }
}
if (parent.Count < 1)
{
var v = new ToolStripMenuItem();
v.Text = "空的文件夹";
v.Tag = dir.FullName;
v.Click += OnMenuItemClick;
parent.Add(v);
}
}
private void MainMenu_Opening(object sender, CancelEventArgs e)
{
if (theDir == null) { return; }
try
{
BuildMenu(MainMenu.Items, theDir);
}
catch (Exception ex)
{
PopError(ex);
}
}
private void OnMenuItemClick(object sender, EventArgs e)
{
if (sender is not ToolStripMenuItem item) { return; }
var tag = item.Tag;
if (tag == null || tag is not string path) { return; }
try
{
var pinfo = new ProcessStartInfo
{
FileName = path,
UseShellExecute = true
};
using var _ = Process.Start(pinfo);
}
catch (Exception)
{
}
}
private static void PopError(Exception ex)
{
MessageBox.Show(ex.Message, "Buyi Toolbar. Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private static Bitmap GetIconBitmap(string f)
{
using var icon = System.Drawing.Icon.ExtractAssociatedIcon(f);
return icon.ToBitmap();
}
private static readonly SolidBrush backgroundBrush = new(Color.Black);
private static readonly SolidBrush fontBrush = new(Color.White);
private const int iconWidth = 128;
private static readonly Font iconFont = new("Microsoft YaHei UI", Convert.ToInt32(iconWidth * 0.75), System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);
private static Icon CreateIconFromText(string text)
{
using var iconBitmap = new Bitmap(iconWidth, iconWidth);
using var g = Graphics.FromImage(iconBitmap);
g.FillRectangle(backgroundBrush, 0, 0, iconWidth, iconWidth);
g.DrawString(text, iconFont, fontBrush, 1, 1);
var icon = System.Drawing.Icon.FromHandle(iconBitmap.GetHicon());
return icon;
}
}
}