-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
313 lines (271 loc) · 11.6 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace EVE_Online_Quick_Client_Changer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<EveClientData> EveClients;
// hotKeyID, (hotKeyVirtualKeyCode, ClientHandle)
private Dictionary<int, HotKeyData> RegisteredHotkeys = [];
private IntPtr hwnd;
private readonly int hotKeyInitialID = 9000;
// Using Windows API to register hotkeys
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
// title 명으로 window hWnd 찾기
// TODO: hwnd 저장해두고 하는게 더 나을수도??
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// 최소화시 윈도우 활성화
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
// 윈도우 가장 위로 올리기
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
hwnd = new WindowInteropHelper(this).Handle;
// Load settings from file
// Disable reload button while loading to prevent bad things happen
btnClientReload.IsEnabled = false;
EveClients = SettingsHandler.LoadSettings();
btnClientReload.IsEnabled = true;
LoadEveClients();
// Disable SetClientKey button if no client is selected
if (lbEveClients.SelectedIndex == -1)
{
btnSetClientKey.IsEnabled = false;
}
// Register hotkey hook
HwndSource source = HwndSource.FromHwnd(hwnd);
source.AddHook(HwndHook);
}
private void LoadEveClients()
{
// Disable reload button while loading
btnClientReload.IsEnabled = false;
// Create keyMap for current eve online processes
// Search for all EVE Online clients
System.Diagnostics.Process[] eveProcesses = System.Diagnostics.Process.GetProcessesByName("exefile");
Dictionary<string, int> eveClientsPairs = [];
foreach (System.Diagnostics.Process process in eveProcesses)
{
// skip EVE clients in login screen
if (process.MainWindowTitle == "EVE")
continue;
eveClientsPairs.Add(process.MainWindowTitle, process.Id);
}
// Loop through ALL saved clients
foreach (EveClientData client in EveClients)
{
// Check if client is still running
if (eveClientsPairs.ContainsKey(client.MainWindowTitle))
{
// Update process ID
//client.ProcessID = eveClientsPairs[client.MainWindowTitle];
// Update color
client.TextColor = "Black";
// Remove from Dictionary
eveClientsPairs.Remove(client.MainWindowTitle);
// check if hotkey is set
if (client.HotKeyVirtualKeyCode != 0)
{
try { RegisterHotKeyWrapper(client.HotKeyVirtualKeyCode, client.MainWindowTitle); }
catch (Exception)
{
MessageBox.Show("단축키 등록에 실패했습니다. 다른 키를 선택해주세요.");
client.HotKeyName = "없음";
client.HotKeyVirtualKeyCode = 0;
}
}
}
else
{
// Gray out text if client is not running
client.TextColor = "Gray";
}
}
// Loop through remaining clients
foreach (KeyValuePair<string, int> client in eveClientsPairs)
{
// Add new client to list
EveClients.Add(new EveClientData
{
MainWindowTitle = client.Key,
// It is placeholder value
HotKeyName = "없음",
HotKeyVirtualKeyCode = 0,
TextColor = "Black"
});
}
// Load data to listbox
lbEveClients.ItemsSource = EveClients;
lbEveClients.Items.Refresh();
// Enable reload button after loading
btnClientReload.IsEnabled = true;
}
private void RegisterHotKeyWrapper(uint hotKeyVirtualKeyCode, string mainWindowTitle)
{
// Register hotkey if not already registered
if (!IsThisKeyCodeAlreadyRegistered(hotKeyVirtualKeyCode))
{
// Register hotkey
// TODO: Can add modifier key support here
// Like Ctrl + C, Alt + C, etc
int hotKeyIndex = hotKeyInitialID + RegisteredHotkeys.Count;
if (!RegisterHotKey(hwnd, hotKeyIndex, 0, hotKeyVirtualKeyCode))
{
throw new Exception();
}
else
{
// Add to registered hotkeys
RegisteredHotkeys.Add(hotKeyIndex, new HotKeyData { HotKeyVirtualKeyCode = hotKeyVirtualKeyCode, MainWindowTitle = mainWindowTitle });
}
}
}
private bool IsThisKeyCodeAlreadyRegistered(uint hotKeyVirtualKeyCode)
{
foreach (var hotkey in RegisteredHotkeys)
{
if (hotkey.Value.HotKeyVirtualKeyCode == hotKeyVirtualKeyCode)
return true;
}
return false;
}
// Button click events
private void ButtonClickEventsHandler(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
switch (button.Name)
{
case "btnClientReload":
// "클라이언트 리로드" 버튼 클릭
// Reload client list
LoadEveClients();
break;
case "btnSetClientKey":
// "클라이언트 키 지정" 버튼 클릭
// Set hotkey for selected client
// SelectedIndex Will return -1 if no item is selected
if (lbEveClients.SelectedIndex == -1)
{
MessageBox.Show("단축키를 지정할 클라이언트가 선택되지 않았습니다.");
return;
}
GetKeyWindow getKeyWindow = new();
bool? result = getKeyWindow.ShowDialog();
if (result == true)
{
string hotKeyName = getKeyWindow.HotKeyName;
uint hotKeyVirtualKeyCode = getKeyWindow.HotKeyVirtualKeyCode;
// Update selected client hotkey
EveClients[lbEveClients.SelectedIndex].HotKeyName = hotKeyName;
EveClients[lbEveClients.SelectedIndex].HotKeyVirtualKeyCode = hotKeyVirtualKeyCode;
// Reload data to listbox
lbEveClients.ItemsSource = EveClients;
lbEveClients.Items.Refresh();
// Refresh selected client info
// TODO: Better way to do this?
lbEveClients_SelectionChanged(lbEveClients, null);
}
break;
case "btnSave":
// "저장" 버튼 클릭
// Save settings as JSON settings
SettingsHandler.SaveSettings(EveClients);
break;
case "btnReset":
// "초기화" 버튼 클릭
// Nuke all settings ( need comfirm window )
break;
case "btnToggleHotkeys":
// "단축키 동작중" / "단축키 미동작중" 버튼 클릭
// Toggle hotkey usage
break;
}
}
}
private void lbEveClients_SelectionChanged(object sender, SelectionChangedEventArgs? e)
{
if (lbEveClients.SelectedIndex == -1)
{
btnSetClientKey.IsEnabled = false;
}
else
{
btnSetClientKey.IsEnabled = true;
}
// Update selected client info
// Split by space and get 3rd element
// EVE - Goem Funila -> Goem Funaila
lblSelectedClientName.Content = "클라명: " + ((EveClientData)lbEveClients.SelectedItem).MainWindowTitle.Split(" - ")[1];
lblSelectedClientHotKey.Content = "설정된 키: " + ((EveClientData)lbEveClients.SelectedItem).HotKeyName;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// TODO: Add form closing event: Compare Between settings.json and current settings
// and ask user to save settings if there is any difference
// Text will be: "저장되지 않은 변경사항이 있습니다. 저장하시겠습니까?"
foreach (var hotkey in RegisteredHotkeys)
{
UnregisterHotKey(hwnd, hotkey.Key);
}
}
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x312;
if (msg == WM_HOTKEY)
{
int hotkeyId = wParam.ToInt32();
if (RegisteredHotkeys.ContainsKey(hotkeyId))
{
// Handle the hotkey press
IntPtr hWnd = FindWindow(null, RegisteredHotkeys[hotkeyId].MainWindowTitle);
if (!hWnd.Equals(IntPtr.Zero)) {
ShowWindowAsync(hWnd, 1);
SetForegroundWindow(hWnd);
}
handled = true;
}
else
{
MessageBox.Show($"Hotkey {hotkeyId} not found! Something wrong...");
}
}
return IntPtr.Zero;
}
}
struct HotKeyData
{
public uint HotKeyVirtualKeyCode;
public string MainWindowTitle;
}
public class EveClientData
{
public required string MainWindowTitle { get; set; }
public required string HotKeyName { get; set; }
public required uint HotKeyVirtualKeyCode { get; set; }
public required string TextColor { get; set; }
}
}