-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
420 lines (382 loc) · 17.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using WindowsDisplayAPI.DisplayConfig;
namespace BlackScreensWPF
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private event EventHandler Resized;
public MainWindow()
{
CommonData.dataInstance.LogToFile.Debug("MainWindow()");
InitializeComponent();
this.tbTitle.Text = "BlackScreens 1.14";
CommonData.dataInstance.LogToFile.Info("MainWindow() "+ this.tbTitle.Text + " launching");
this.DataContext = CommonData.dataInstance;
notifyIcon.TrayLeftMouseDown += NotifyIcon_TrayLeftMouseDown;
refreshMainWindowCurrentScreen();
updateScreenNames();
}
/// <summary>
/// Refresh name of the current param window screen
/// </summary>
public void refreshMainWindowCurrentScreen()
{
CommonData.dataInstance.LogToFile.Debug("refreshMainWindowCurrentScreen()");
System.Windows.Interop.WindowInteropHelper wih = new System.Windows.Interop.WindowInteropHelper(this);
Screen currentScreen = Screen.FromHandle(wih.Handle);
CommonData.dataInstance.ParamsScreenDeviceName = currentScreen.DeviceName;
}
private void NotifyIcon_TrayLeftMouseDown(object sender, RoutedEventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
showWindow();
}
else
{
minimizeWindow();
}
}
public void minimizeWindow()
{
this.WindowState = WindowState.Minimized;
this.ShowInTaskbar = false;
}
/// <summary>
/// Update all screens names on application launch, and handle visibility of information on params window
/// </summary>
public void updateScreenNames()
{
String setImage = BlackScreens.Properties.Resources.setImage;
String stopUsingImage = BlackScreens.Properties.Resources.stopUsingImage;
CommonData.dataInstance.LogToFile.Debug("updateScreenNames()");
if (Screen.AllScreens.Length > 0)
{
PathDisplayTarget display = CommonData.dataInstance.findDisplayByScreenName(Screen.AllScreens[0].DeviceName);
if (display != null)
{
CommonData.dataInstance.Screen1Name = display.FriendlyName;
tbScreen1Name.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
// Case of Windows not sending back any screen 1 name
if (CommonData.dataInstance.Screen1Name.Length == 0)
{
CommonData.dataInstance.Screen1Name = "No screen name";
}
// Update label for image usage, instead of black color
if (!String.IsNullOrEmpty(CommonData.dataInstance.ImageFileNameScreen1))
{
tbScreen1SetImage.Text = stopUsingImage;
}
else
tbScreen1SetImage.Text = setImage;
}
if (Screen.AllScreens.Length > 1)
{
PathDisplayTarget display = CommonData.dataInstance.findDisplayByScreenName(Screen.AllScreens[1].DeviceName);
if (display != null)
{
CommonData.dataInstance.Screen2Name = display.FriendlyName;
tbScreen1Name.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
// Update label for image usage, instead of black color
if (!String.IsNullOrEmpty(CommonData.dataInstance.ImageFileNameScreen2))
{
tbScreen2SetImage.Text = stopUsingImage;
}
else
tbScreen2SetImage.Text = setImage;
}
if (Screen.AllScreens.Length > 2)
{
PathDisplayTarget display = CommonData.dataInstance.findDisplayByScreenName(Screen.AllScreens[2].DeviceName);
if (display != null)
{
CommonData.dataInstance.Screen3Name = display.FriendlyName;
tbScreen1Name.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
// Update label for image usage, instead of black color
if (!String.IsNullOrEmpty(CommonData.dataInstance.ImageFileNameScreen3))
{
tbScreen3SetImage.Text = stopUsingImage;
}
else
tbScreen3SetImage.Text = setImage;
}
if (Screen.AllScreens.Length > 3)
{
tbScreen4Name.Visibility = Visibility.Visible;
PathDisplayTarget display = CommonData.dataInstance.findDisplayByScreenName(Screen.AllScreens[3].DeviceName);
if (display != null)
{
CommonData.dataInstance.Screen4Name = display.FriendlyName;
tbScreen1Name.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
}
if (Screen.AllScreens.Length > 4)
{
tbScreen4Name.Visibility = Visibility.Visible;
PathDisplayTarget display = CommonData.dataInstance.findDisplayByScreenName(Screen.AllScreens[4].DeviceName);
if (display != null)
{
CommonData.dataInstance.Screen5Name = display.FriendlyName;
tbScreen1Name.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
}
if (Screen.AllScreens.Length > 5)
{
tbScreen4Name.Visibility = Visibility.Visible;
PathDisplayTarget display = CommonData.dataInstance.findDisplayByScreenName(Screen.AllScreens[5].DeviceName);
if (display != null)
{
CommonData.dataInstance.Screen6Name = display.FriendlyName;
tbScreen1Name.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
}
tbScreen2AltKeyInfo.Visibility = (Screen.AllScreens.Length > 1) ? Visibility.Visible : Visibility.Hidden;
tbScreen2Name.Visibility = (Screen.AllScreens.Length > 1) ? Visibility.Visible : Visibility.Collapsed;
tbScreen2SetImage.Visibility = (Screen.AllScreens.Length > 1) ? Visibility.Visible : Visibility.Collapsed;
tbScreen3AltKeyInfo.Visibility = (Screen.AllScreens.Length > 2) ? Visibility.Visible : Visibility.Hidden;
tbScreen3Name.Visibility = (Screen.AllScreens.Length > 2) ? Visibility.Visible : Visibility.Collapsed;
tbScreen3SetImage.Visibility = (Screen.AllScreens.Length > 2) ? Visibility.Visible : Visibility.Collapsed;
tbScreen4AltKeyInfo.Visibility = (Screen.AllScreens.Length > 3) ? Visibility.Visible : Visibility.Hidden;
// Collapsed used to hide complete line (screen 4,5,6) if there is not at least 4 screens used (presume large majority of users cases!)
tbScreen4Name.Visibility = (Screen.AllScreens.Length > 3) ? Visibility.Visible : Visibility.Collapsed;
tbScreen5AltKeyInfo.Visibility = (Screen.AllScreens.Length > 4) ? Visibility.Visible : Visibility.Collapsed;
tbScreen5Name.Visibility = (Screen.AllScreens.Length > 4) ? Visibility.Visible : Visibility.Collapsed;
tbScreen6AltKeyInfo.Visibility = (Screen.AllScreens.Length > 5) ? Visibility.Visible : Visibility.Collapsed;
tbScreen6Name.Visibility = (Screen.AllScreens.Length > 5) ? Visibility.Visible : Visibility.Collapsed;
}
public void showWindow()
{
this.Topmost = true;
this.WindowState = WindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
this.Activate();
}
private void OnMinimizeButtonClick(object sender, RoutedEventArgs e)
{
minimizeWindow();
}
private void OnCloseButtonClick(object sender, RoutedEventArgs e)
{
App.Current.Shutdown();
}
private void sOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
CommonData.dataInstance.updateAllBlackWindowParams();
}
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Normal)
{
this.Width = 800;
this.ShowInTaskbar = true;
refreshMainWindowCurrentScreen();
}
else
this.ShowInTaskbar = false;
}
private void bExit_Click(object sender, RoutedEventArgs e)
{
App.Current.Shutdown();
}
private void bMinimize_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
this.ShowInTaskbar = false;
}
void MainWindow_Resized(object sender, EventArgs e)
{
// In case param Window is moved by user along different screens
refreshMainWindowCurrentScreen();
}
/// <summary>
/// Windows system rezized used to handle EXIT MOVE by user, which is not handle by any WPF window native event
/// </summary>
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//const int WM_ENTERSIZEMOVE = 0x0231;
const int WM_EXITSIZEMOVE = 0x0232;
if (msg == WM_EXITSIZEMOVE)
{
if (Resized != null)
{
Resized(this, EventArgs.Empty);
}
}
return IntPtr.Zero;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Hook for windows system resized by user only on EXIT MOVE
// In order to have less event possible, compared to WPF native move event which launch nearly each mouse pixel move
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
this.Resized += MainWindow_Resized;
}
private void notifyIcon_TrayRightMouseDown(object sender, RoutedEventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
miShowHideParameters.Header = "Show Parameters";
}
else
miShowHideParameters.Header = "Hide Parameters";
}
public void toggleShowHideWindow()
{
if (this.WindowState == WindowState.Minimized)
{
showWindow();
}
else
minimizeWindow();
}
private void miShowHideParameters_Click(object sender, RoutedEventArgs e)
{
toggleShowHideWindow();
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if ((e.Key == Key.Down) || (e.Key == Key.Left))
{
sOpacity.Value--;
}
if ((e.Key == Key.Up) || (e.Key == Key.Right))
{
sOpacity.Value++;
}
e.Handled = true;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
CommonData.dataInstance.saveUserConfigFiles();
}
private String debugOneScreenToString(System.Drawing.Rectangle r, String screenName, int screenNumber)
{
String txt = "";
txt += "Screen " + screenNumber + " : " + screenName + Environment.NewLine;
txt += "Left " + r.Left + " / X = " + r.X + Environment.NewLine;
txt += "Top " + r.Top + " / Y = " + r.Y + Environment.NewLine;
txt += "Right " + r.Right + " / Width = " + r.Width + Environment.NewLine;
txt += "Bottom " + r.Bottom + " / Height = " + r.Height + Environment.NewLine;
txt += "Size.Height " + r.Size.Height + " / Size.Width = " + r.Size.Width + Environment.NewLine;
txt += "Location.X " + r.Location.X + " / Location.Y = " + r.Location.Y + Environment.NewLine + Environment.NewLine;
return txt;
}
/// <summary>
/// Debug all screens positioning information in ClipBoard
/// </summary>
private void debugAllScreensInClipboard()
{
String debugTxt = "";
if (Screen.AllScreens.Length > 0)
{
debugTxt += debugOneScreenToString(CommonData.dataInstance.Screen1TooltipData, CommonData.dataInstance.Screen1Name, 1);
}
if (Screen.AllScreens.Length > 1)
{
debugTxt += debugOneScreenToString(CommonData.dataInstance.Screen2TooltipData, CommonData.dataInstance.Screen2Name, 2);
if (Screen.AllScreens.Length > 2)
{
debugTxt += debugOneScreenToString(CommonData.dataInstance.Screen3TooltipData, CommonData.dataInstance.Screen3Name, 3);
if (Screen.AllScreens.Length > 3)
{
debugTxt += debugOneScreenToString(CommonData.dataInstance.Screen4TooltipData, CommonData.dataInstance.Screen4Name, 4);
if (Screen.AllScreens.Length > 4)
{
debugTxt += debugOneScreenToString(CommonData.dataInstance.Screen5TooltipData, CommonData.dataInstance.Screen5Name, 5);
if (Screen.AllScreens.Length > 5)
{
debugTxt += debugOneScreenToString(CommonData.dataInstance.Screen6TooltipData, CommonData.dataInstance.Screen6Name, 6);
}
}
}
}
}
System.Windows.Clipboard.SetText(debugTxt);
}
private void miAbout_Click(object sender, RoutedEventArgs e)
{
// @TODO
spUpdatesHistory.Visibility = Visibility.Visible;
}
private void tbScreen1Name_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
debugAllScreensInClipboard();
}
private void expUpdatesHistory_Collapsed(object sender, RoutedEventArgs e)
{
spUpdatesHistory.Visibility = Visibility.Collapsed;
expUpdatesHistory.IsExpanded = true;
}
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
spUpdatesHistory.Visibility = spUpdatesHistory.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
private void Hyperlink_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Hyperlink hl = (Hyperlink)sender;
System.Diagnostics.Process.Start(hl.NavigateUri.AbsoluteUri);
}
private void cbClickThrough_Click(object sender, RoutedEventArgs e)
{
CommonData.dataInstance.updateAllBlackWindowParams();
}
private void cbHideOnTaskBarOnStart_Click(object sender, RoutedEventArgs e)
{
CommonData.dataInstance.updateAllBlackWindowParams();
}
private void changeImage(TextBlock tbImageButton, string screenName, out String imageName)
{
imageName = "";
if (tbImageButton.Text == "Set image")
{
System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
openFileDialog.Filter = "Image Files (*.png;*.jpg)|*.png;*.jpg";
openFileDialog.Title = "Image file to use for "+ screenName;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
imageName = openFileDialog.FileName;
tbImageButton.Text = "Stop using image";
}
}
else
{
tbImageButton.Text = "Set image";
}
}
private void tbScreen1SetImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
String imageFileName = CommonData.dataInstance.ImageFileNameScreen1;
changeImage(tbScreen1SetImage, "screen 1", out imageFileName);
CommonData.dataInstance.ImageFileNameScreen1 = imageFileName;
CommonData.dataInstance.updateAllBlackWindowParams();
}
private void tbScreen2SetImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
String imageFileName = CommonData.dataInstance.ImageFileNameScreen2;
changeImage(tbScreen2SetImage, "screen 2", out imageFileName);
CommonData.dataInstance.ImageFileNameScreen2 = imageFileName;
CommonData.dataInstance.updateAllBlackWindowParams();
}
private void tbScreen3SetImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
String imageFileName = CommonData.dataInstance.ImageFileNameScreen2;
changeImage(tbScreen3SetImage, "screen 3", out imageFileName);
CommonData.dataInstance.ImageFileNameScreen3 = imageFileName;
CommonData.dataInstance.updateAllBlackWindowParams();
}
}
}