-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
208 lines (167 loc) · 5.87 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PPSwitch
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string selectedProcess = "";
string selectedChosen = "";
string saveFilepath = "HighPerformanceProcesses.txt";
List<string> chosenOnes = new List<string>();
NotifyIcon nIcon;
Guid HPPScheme = Guid.Parse("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
Guid BALScheme = Guid.Parse("381b4222-f694-41f0-9685-ff5bb260df2e");
Boolean hpp;
Boolean closing = false;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
public Boolean HPP
{
get { return hpp; }
set {
if(hpp != value)
{
hpp = value;
if(hpp)
{
PowerSchemeHelper.SetPowerScheme(HPPScheme);
this.nIcon.Icon = new Icon(@"ico/highperformance.ico");
}
else
{
PowerSchemeHelper.SetPowerScheme(BALScheme);
this.nIcon.Icon = new Icon(@"ico/balance.ico");
}
}
}
}
System.Timers.Timer pTimer;
public MainWindow()
{
InitializeComponent();
initIcon();
if (File.Exists(saveFilepath))
chosenOnes = File.ReadAllLines(saveFilepath).ToList();
lblChosen.ItemsSource = chosenOnes;
fillList();
pTimer = new System.Timers.Timer(5000);
pTimer.Elapsed += PTimer_Elapsed;
pTimer.Start();
Visibility = Visibility.Hidden;
}
private void PTimer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Dispatcher.Invoke(() =>
{ fillList(); });
}
void fillList()
{
List<Process> localAll = Process.GetProcesses().ToList();
List<String> pNames = new List<string>();
bool switchHpp = false;
foreach (var p in localAll)
if (!pNames.Contains(p.ProcessName))
{
pNames.Add(p.ProcessName);
if (chosenOnes.Contains(p.ProcessName))
switchHpp = true;
}
HPP = switchHpp;
lblChosen.ItemsSource = chosenOnes;
pNames.Sort();
lblRunning.ItemsSource = pNames;
lblRunning.SelectedItem = selectedProcess;
}
void saveAndRefreshList()
{
lblChosen.ItemsSource = null;
lblChosen.ItemsSource = chosenOnes;
File.WriteAllLines(saveFilepath, chosenOnes);
}
private void lblRunning_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
selectedProcess = e.AddedItems[0].ToString();
}
private void lblRunning_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (!chosenOnes.Contains(selectedProcess))
{
chosenOnes.Add(selectedProcess);
saveAndRefreshList();
}
}
private void lblChosen_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count == 1)
selectedChosen = e.AddedItems[0].ToString();
}
private void lblChosen_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
chosenOnes.Remove(selectedChosen);
saveAndRefreshList();
selectedChosen = "";
}
private void initIcon()
{
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.nIcon = new NotifyIcon(components);
this.nIcon.Icon = new Icon(@"ico/balance.ico");
this.nIcon.Visible = true;
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { this.menuItem1 });
// Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += MenuItem1_Click;
// Set up how the form should be displayed.
nIcon.Text = "PPSwitch";
nIcon.ContextMenu = this.contextMenu1;
nIcon.DoubleClick += NIcon_DoubleClick;
}
private void NIcon_DoubleClick(object sender, EventArgs e)
{
this.Activate();
Visibility = Visibility.Visible;
this.Activate();
}
private void MenuItem1_Click(object sender, EventArgs e)
{
closing = true;
this.Close();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!closing)
{
e.Cancel = true;
Visibility = Visibility.Hidden;
}
else
nIcon.Visible = false;
}
}
}