-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessEditorForm.cs
More file actions
173 lines (145 loc) · 5.93 KB
/
ProcessEditorForm.cs
File metadata and controls
173 lines (145 loc) · 5.93 KB
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
using Mouse_Mender.Modules;
namespace Mouse_Mender;
public partial class ProcessEditorForm : Form
{
// Declared Variables
private MainForm mainForm;
private AutoProcessEnable autoProcessEnable;
public ProcessEditorForm()
{
InitializeComponent();
autoProcessEnable = new AutoProcessEnable(mainForm);
}
// ProcessEditorForm Load Event
private void ProcessEditorForm_Load(object sender, EventArgs e)
{
// Set ProcessEditorForm start location to center of MainForm's current position
this.Location = CalculateStartPosition();
// Fill List Box with Saved Processes
listBox1.Items.Clear();
if (Properties.Settings.Default.AutoEnableProcessList != null)
{
foreach (var process in Properties.Settings.Default.AutoEnableProcessList)
{
listBox1.Items.Add(process);
}
}
}
// Calculate Start Position From MainForm Size & Current Location
private Point CalculateStartPosition()
{
// MainForm dimensions and location
Point mainFormSavedLocation = Properties.Settings.Default.LastWindowLocation;
int mainFormWidth = 429;
int mainFormHeight = 250;
// ProcessEditorForm dimensions
int processEditorFormWidth = 328;
int processEditorFormHeight = 250;
// Calculate center position for a child form
int newX = mainFormSavedLocation.X + (mainFormWidth - processEditorFormWidth) / 2;
int newY = mainFormSavedLocation.Y + (mainFormHeight - processEditorFormHeight) / 2;
return new Point(newX, newY);
}
// Add Process
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(textBox1.Text) && textBox1.Text != "process.exe")
{
String NewProcess;
// Check if the TextBox1 string ends with ".exe" and add it if it doesn't
if (!textBox1.Text.EndsWith(".exe"))
{
NewProcess = textBox1.Text.Trim() + ".exe";
}
else
{
NewProcess = textBox1.Text.Trim();
}
// Check if the process already exists in the listBox1 or AutoEnableProcessList
bool existsInListBox = listBox1.Items.Contains(NewProcess);
bool existsInSettings = Properties.Settings.Default.AutoEnableProcessList != null &&
Properties.Settings.Default.AutoEnableProcessList.Contains(NewProcess);
if (existsInListBox || existsInSettings)
{
// Show a message box if the process already exists
MessageBox.Show("The process already exists in the list.", "Duplicate Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else // Contuine adding the new process
{
// Add item to listbox
listBox1.Items.Add(NewProcess);
// Add the new item to the settings collection
if (Properties.Settings.Default.AutoEnableProcessList == null)
{
Properties.Settings.Default.AutoEnableProcessList = new System.Collections.Specialized.StringCollection();
}
// Add the New Process (Standardized) to the StringCollection
Properties.Settings.Default.AutoEnableProcessList.Add(NewProcess);
// Save Process List
Properties.Settings.Default.Save();
// Clear textbox
textBox1.Clear();
// Reset Auto Enable in MainForm
autoProcessEnable.RestartAutoEnableTimer();
}
}
else
{
// Show a message box if the input is invalid
MessageBox.Show("Please enter a valid process name. Format is 'processname(.exe)'", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Delete Process
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
// Remove the selected item from the settings collection
Properties.Settings.Default.AutoEnableProcessList.Remove(listBox1.SelectedItem.ToString());
Properties.Settings.Default.Save();
// Remove from the ListBox
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
// Reset Auto Enable in MainForm
autoProcessEnable.RestartAutoEnableTimer();
}
}
// Move Process Down
private void button4_Click(object sender, EventArgs e)
{
MoveItem(1);
UpdateSettingsCollection();
}
// Move Process Up
private void button3_Click(object sender, EventArgs e)
{
MoveItem(-1);
UpdateSettingsCollection();
}
// Move Listbox Process - Helper Function
private void MoveItem(int direction)
{
// Checking selected item
if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
return; // No selected item
// Calculate new index using direction (-1 for up, 1 for down)
int newIndex = listBox1.SelectedIndex + direction;
// Checking bounds of the list
if (newIndex < 0 || newIndex >= listBox1.Items.Count)
return; // Index out of range
// Perform the move
object selected = listBox1.SelectedItem;
listBox1.Items.Remove(selected);
listBox1.Items.Insert(newIndex, selected);
listBox1.SetSelected(newIndex, true);
}
// Update String Collection - Helper Function
private void UpdateSettingsCollection()
{
Properties.Settings.Default.AutoEnableProcessList = new System.Collections.Specialized.StringCollection();
foreach (var process in listBox1.Items)
{
Properties.Settings.Default.AutoEnableProcessList.Add(process.ToString());
}
Properties.Settings.Default.Save();
}
}