-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrayFolder.cs
233 lines (199 loc) · 6.9 KB
/
TrayFolder.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
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.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace TrayFolder
{
public partial class TrayFolder : Form
{
const string KEY_FOLDER = "Folder";
const string APPNAME = "TrayFolder";
private SystemIconsImageList SystemIcons = new SystemIconsImageList();
private ToolStripMenuItem SettingMenu;
public TrayFolder()
{
InitializeComponent();
Icon = Win32.GetStockIcon(Win32.SHSIID_FOLDER, Win32.SHGSI_LARGEICON);
DialogResult = DialogResult.OK;
SettingMenu = CreateSettingMenu();
TrayIcon.Icon = this.Icon;
TrayIcon.Click += OnTrayIconClick;
TextPath.Text = Properties.Settings.Default.FolderPath;
HideExtensions.Checked = Properties.Settings.Default.HideExtensions;
TrayIcon.Text = Properties.Settings.Default.FolderName;
IconName.Text = Properties.Settings.Default.FolderName;
NumericDepth.Value = Properties.Settings.Default.MaxDepth;
OpenFolders.Checked = Properties.Settings.Default.OpenFolders;
LaunchStartup.Checked = GetLaunchOnStartup();
NumericFailSafe.Value = Properties.Settings.Default.FailSafe;
Text = APPNAME + " v" + Application.ProductVersion;
}
protected override void OnLoad( EventArgs e )
{
if(IsValid())
{
HideMe();
UpdateMenu();
}
base.OnLoad(e);
}
private bool IsValid()
{
if(Properties.Settings.Default.FirstRun)
return (false);
if(String.IsNullOrWhiteSpace(TextPath.Text))
return (false);
if(Directory.Exists(TextPath.Text) == false)
return (false);
return (true);
}
private void OnTrayIconClick( object sender, EventArgs e )
{
MouseEventArgs me = (MouseEventArgs)e;
Win32.SetForegroundWindow(new HandleRef(FolderMenu, FolderMenu.Handle));
SettingMenu.Visible = me.Button == MouseButtons.Right;
FolderMenu.Show(Cursor.Position);
}
private void OnSelectFolder( object sender, EventArgs e )
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = TextPath.Text;
if(fbd.ShowDialog(this) == DialogResult.OK)
{
TextPath.Text = fbd.SelectedPath;
UpdateMenu();
}
}
public ToolStripMenuItem CreateSettingMenu()
{
ToolStripMenuItem Settings = new ToolStripMenuItem("Setup");
// Settings.Image = DefaultIcons.Extract("shell32.dll", 21, false).ToBitmap();
ToolStripMenuItem MenuSettings = new ToolStripMenuItem("Settings...");
MenuSettings.Click += OnMenuSettings;
Settings.DropDownItems.Add(MenuSettings);
Settings.DropDownItems.Add(new ToolStripSeparator());
ToolStripMenuItem MenuUpdate = new ToolStripMenuItem("Update Folders");
MenuUpdate.Click += OnMenuUpdate;
Settings.DropDownItems.Add(MenuUpdate);
Settings.DropDownItems.Add(new ToolStripSeparator());
ToolStripMenuItem MenuQuit = new ToolStripMenuItem("Quit");
MenuQuit.Click += OnQuit;
Settings.DropDownItems.Add(MenuQuit);
return (Settings);
}
private void UpdateMenu()
{
MenuBuilder Builder = new MenuBuilder(SystemIcons)
{
HideExtensions = HideExtensions.Checked,
MaxDepth = (int)NumericDepth.Value,
OpenFolders = OpenFolders.Checked,
};
Builder.UpdateMenu(TextPath.Text, FolderMenu);
FolderMenu.Items.Add(SettingMenu);
}
private void OnSave( object sender, EventArgs e )
{
Properties.Settings.Default.FirstRun = false;
Properties.Settings.Default.FolderName = IconName.Text;
Properties.Settings.Default.FolderPath = TextPath.Text;
Properties.Settings.Default.HideExtensions = HideExtensions.Checked;
Properties.Settings.Default.MaxDepth = (int)NumericDepth.Value;
Properties.Settings.Default.FailSafe = (int)NumericFailSafe.Value;
Properties.Settings.Default.OpenFolders = OpenFolders.Checked;
Properties.Settings.Default.Save();
TrayIcon.Text = IconName.Text;
if(IsValid())
{
HideMe();
UpdateMenu();
}
}
protected override void OnClosing( CancelEventArgs e )
{
if(DialogResult == DialogResult.OK)
{
e.Cancel = true;
HideMe();
}
base.OnClosing(e);
}
private void HideMe()
{
Visible = false;
ShowInTaskbar = false;
}
private void OnMenuUpdate( object sender, EventArgs e )
{
UpdateMenu();
}
private void OnQuit( object sender, EventArgs e )
{
DialogResult = DialogResult.Cancel;
Close();
}
private void OnMenuSettings( object sender, EventArgs e )
{
ShowInTaskbar = true;
Visible = true;
}
private bool GetLaunchOnStartup()
{
try
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
return (key.GetValue(APPNAME) as String == Application.ExecutablePath);
}
catch
{
}
return (false);
}
private void OnLaunchOnStartup( object sender, EventArgs e )
{
try
{
RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if(LaunchStartup.Checked)
{
regkey.SetValue(APPNAME, Application.ExecutablePath, RegistryValueKind.String);
}
else
{
regkey.DeleteValue(APPNAME, false);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}