-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
259 lines (240 loc) · 10.4 KB
/
App.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
using BlackScreenAppWPF;
using BlackScreens;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Navigation;
using WindowsDisplayAPI.DisplayConfig;
namespace BlackScreensWPF
{
public partial class App : System.Windows.Application
{
public KeyboardHook kh;
void App_Startup(object sender, StartupEventArgs e)
{
// For testing languages localization purpose
//System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
// Check for duplicate launch of application
String thisprocessname = Process.GetCurrentProcess().ProcessName;
if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1)
App.Current.Shutdown();
// Initiating each screen tooltip
CommonData.dataInstance.Screen1TooltipData = Screen.AllScreens[0].Bounds;
CommonData.dataInstance.Screen2TooltipData = (Screen.AllScreens.Length > 1) ? Screen.AllScreens[1].Bounds : new Rectangle();
CommonData.dataInstance.Screen3TooltipData = (Screen.AllScreens.Length > 2) ? Screen.AllScreens[2].Bounds : new Rectangle();
CommonData.dataInstance.Screen4TooltipData = (Screen.AllScreens.Length > 3) ? Screen.AllScreens[3].Bounds : new Rectangle();
CommonData.dataInstance.Screen5TooltipData = (Screen.AllScreens.Length > 4) ? Screen.AllScreens[4].Bounds : new Rectangle();
CommonData.dataInstance.Screen6TooltipData = (Screen.AllScreens.Length > 5) ? Screen.AllScreens[5].Bounds : new Rectangle();
//bool result = System.Windows.UI.ViewManagement.ApplicationViewScaling.TrySetDisableLayoutScaling(true);
// Hook for listen to Windows Keys
kh = new KeyboardHook(false);
kh.KeyDown += Kh_KeyDown;
kh.hook();
try
{
CommonData.dataInstance.Displays = PathDisplayTarget.GetDisplayTargets();
}
catch (Exception)
{
}
CommonData.dataInstance.FParams = new MainWindow();
CommonData.dataInstance.loadUserConfigFile(CommonData.dataInstance.FParams);
CommonData.dataInstance.LogToFile.Debug("App/xaml.cs.App_Startup() FirstAppLaunch = "+ CommonData.dataInstance.FirstAppLaunch);
CommonData.dataInstance.LogToFile.Debug("App/xaml.cs.App_Startup() ReduceAppOnLaunch = " + CommonData.dataInstance.ReduceAppOnLaunch);
if ((CommonData.dataInstance.FirstAppLaunch == true) || (CommonData.dataInstance.ReduceAppOnLaunch == false))
{
CommonData.dataInstance.FParams.showWindow();
}
else
{
CommonData.dataInstance.FParams.updateScreenNames();
CommonData.dataInstance.FParams.minimizeWindow();
}
CommonData.dataInstance.FirstAppLaunch = false;
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
}
/// <summary>
/// Return number of active BlackScreen, meaning count of BlackWindow which are currently visible
/// </summary>
/// <returns></returns>
private int numberOfActiveBlackScreen(BlackWindow[] bwArray)
{
int nbActiveBlackScreens = 0;
foreach (BlackWindow bw in bwArray)
{
if ((bw != null) && (bw.IsVisible))
{
nbActiveBlackScreens++;
}
}
return nbActiveBlackScreens;
}
/// <summary>
/// Init a new BlackWindow
/// </summary>
/// <returns></returns>
private BlackWindow initNewBlackWindow(int screenNumber)
{
// Creating first time BlackWindows for this screen
BlackWindow bw = new BlackWindow();
bw.ScreenNumber = screenNumber;
bw.updateBlackWindowParams();
return bw;
}
/// <summary>
/// Hide this specific BlackWindow
/// </summary>
/// <param name="bsTohide"></param>
private void hideBlackWindow(BlackWindow bsTohide)
{
if (bsTohide != null)
{
bsTohide.HideWindow();
CommonData.dataInstance.FParams.Topmost = false;
}
}
/// <summary>
/// Show a specific BlackWindow on associated screen
/// </summary>
/// <param name="bsToShow">BlackWindow to show</param>
/// <param name="screen">Associated screen boundaries</param>
/// <param name="bwNumber">BlackWindow number (1 to 6)</param>
private void showBlackWindow(BlackWindow bwToShow, Screen screen, int bwNumber)
{
String textAltToShow = "ALT + " + bwNumber;
Rectangle bounds = screen.Bounds;
//currentBlackWindow.Left = bounds.X;
//currentBlackWindow.Top = bounds.Y;
bwToShow.Left = bounds.Left;
bwToShow.Top = bounds.Top;
bwToShow.Height = bounds.Height;
bwToShow.Width = bounds.Width;
bwToShow.KeyToUse = "Use key " + textAltToShow + " to switch";
bwToShow.ScreenNumber = bwNumber;
bwToShow.ShowWindow();
// Hidding parameters window if it's on the same black screen to switch visible
if (CommonData.dataInstance.ParamsScreenDeviceName == bwToShow.ScreenDeviceName)
{
//CommonData.dataInstance.FParams.WindowState = WindowState.Minimized;
CommonData.dataInstance.FParams.minimizeWindow();
}
}
/// <summary>
/// Show all BlackWindows for all existing screens
/// </summary>
/// <param name="blackWindows">BlackWindow Array</param>
private void showAllBlackWindows(BlackWindow[] blackWindows)
{
for (int iBw=0; iBw < blackWindows.Length; iBw++)
{
if (blackWindows[iBw] == null)
blackWindows[iBw] = initNewBlackWindow(iBw+1);
showBlackWindow(blackWindows[iBw], Screen.AllScreens[iBw], iBw+1);
}
}
/// <summary>
/// Hide all black Windows for all existing screens
/// </summary>
/// <param name="blackWindows"></param>
private void hideAllBlackWindows(BlackWindow[] blackWindows)
{
for (int iBw = 0; iBw < blackWindows.Length; iBw++)
{
hideBlackWindow(blackWindows[iBw]);
}
}
private bool Kh_KeyDown(int wParam, KeyboardHookData lParam)
{
bool keyHandled = false;
Keys keyData = (Keys)lParam.vkCode;
// Testing alt+x key pressed
if (kh.AltHeld && !(kh.CtrlHeld || kh.ShiftHeld))
{
int screenNumKey = -1;
if (keyData == Keys.D0) {
keyHandled = true;
int nbOfActiveScreen = numberOfActiveBlackScreen(CommonData.dataInstance.BlackWindows);
// Showing all black screens if number of screens actually shows is less than half of existing screens
if (nbOfActiveScreen <= ((Double)CommonData.dataInstance.BlackWindows.Length/2))
{
showAllBlackWindows(CommonData.dataInstance.BlackWindows);
}
else // Hide all screens
if (nbOfActiveScreen > (CommonData.dataInstance.BlackWindows.Length / 2))
{
hideAllBlackWindows(CommonData.dataInstance.BlackWindows);
}
} else
if (keyData == Keys.D1)
{
screenNumKey = 1;
keyHandled = true;
}
else
if ((keyData == Keys.D2) && (Screen.AllScreens.Length > 1))
{
screenNumKey = 2;
keyHandled = true;
}
else
if (keyData == Keys.D3 && (Screen.AllScreens.Length > 2))
{
screenNumKey = 3;
keyHandled = true;
}
else
if (keyData == Keys.D4 && (Screen.AllScreens.Length > 3))
{
screenNumKey = 4;
keyHandled = true;
}
else
if (keyData == Keys.D5 && (Screen.AllScreens.Length > 4))
{
screenNumKey = 5;
keyHandled = true;
}
else
if (keyData == Keys.D6 && (Screen.AllScreens.Length > 5))
{
screenNumKey = 6;
keyHandled = true;
}
// If any valide alt+X key, and screen associated
if ((screenNumKey > 0) && (screenNumKey <= Screen.AllScreens.Length))
{
if (CommonData.dataInstance.BlackWindows[screenNumKey - 1] == null)
{ // Creating first time BlackWindows for this screen
CommonData.dataInstance.BlackWindows[screenNumKey - 1] = initNewBlackWindow(screenNumKey);
}
BlackWindow currentBlackWindow = CommonData.dataInstance.BlackWindows[screenNumKey - 1];
if (currentBlackWindow.IsVisible)
{ // Hidding black screen window if already visible
hideBlackWindow(currentBlackWindow);
}
else
{ // Showing Blackscreen window if not visible
showBlackWindow(currentBlackWindow, Screen.AllScreens[screenNumKey - 1], screenNumKey);
}
}
}
// If keyHandled == true, current keypressed will not be handled anymore by Windows
// (in certain cases, let it continue to be handled by Microsoft Windows launch Windows system beep sound each time alt+x key was pressed...)
return keyHandled;
}
void App_Exit(object sender, ExitEventArgs e)
{
kh.unhook();
}
}
}