forked from Codeusa/Borderless-Gaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompactWindow.cs
335 lines (285 loc) · 11.7 KB
/
CompactWindow.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BorderlessGaming.WindowsApi;
namespace BorderlessGaming
{
public partial class CompactWindow : Form
{
private const int SW_SHOW = 0x05;
private const int WS_EX_APPWINDOW = 0x40000;
private const int GWL_EXSTYLE = -0x14; //never want to hunt this down again
private const int WS_EX_DLGMODALFRAME = 0x0001;
private const int WS_EX_TOOLWINDOW = 0x0080;
private const short SWP_NOMOVE = 0X2;
private const short SWP_NOSIZE = 1;
private const short SWP_NOZORDER = 0X4;
private const int SWP_SHOWWINDOW = 0x0040;
public static uint MF_BYPOSITION = 0x400;
public static uint MF_REMOVE = 0x1000;
public static int GWL_STYLE = -16;
private readonly List<string> _borderlessWindows = new List<string>();
private readonly List<string> _processDataList = new List<string>();
private readonly List<string> _tempList = new List<string>();
private IntPtr _formHandle;
private bool _gameFound;
private string _selectedFavoriteProcess;
private string _selectedProcessName;
private BackgroundWorker _worker;
public CompactWindow()
{
InitializeComponent();
CenterToScreen();
PopulateList();
ListenForGameLaunch();
if (favoritesList == null) return;
favoritesList.DataSource = Favorites.List;
}
private void ListenForGameLaunch()
{
_formHandle = Handle;
_worker = new BackgroundWorker();
_worker.DoWork += _BackgroundWork;
_worker.RunWorkerCompleted += _BackgroundWorkCompleted;
if (workerTimer != null)
workerTimer.Start();
}
public static IntPtr FindWindowHandle(string processName, IntPtr ignoreHandle)
{
var process =
Process.GetProcesses()
.FirstOrDefault(
p =>
p != null && p.ProcessName.Equals(processName, StringComparison.InvariantCultureIgnoreCase) &&
p.MainWindowHandle != IntPtr.Zero && p.MainWindowHandle != ignoreHandle &&
!string.IsNullOrEmpty(p.MainWindowTitle));
return process != null ? process.MainWindowHandle : IntPtr.Zero;
}
private void _BackgroundWork(object sender, DoWorkEventArgs e)
{
IntPtr handle;
var breakLoop = false;
var windowText = "";
while (true)
{
processList.Invoke((MethodInvoker)delegate
{
//Code to modify control will go here
processList.DataSource = null;
processList.Items.Clear();
_processDataList.Clear();
PopulateList();
});
Favorites.List.ForEach(wndName =>
{
handle = FindWindowHandle(wndName, _formHandle);
if (handle != IntPtr.Zero)
{
windowText = wndName;
breakLoop = true;
}
});
if (breakLoop)
{
Thread.Sleep(2000);
RemoveBorder(windowText);
break;
}
Thread.Sleep(9000);
}
}
private void _BackgroundWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!IsDisposed)
{
}
}
private void PopulateList() //Adds active windows to the processDataList
{
_tempList.Add("Refreshing...");
processList.DataSource = _tempList;
var processlist = Process.GetProcesses();
foreach (var process in processlist)
{
if (process == null)
{
continue;
}
if (process.ProcessName.Equals("explorer"))
{
continue;
}
if (String.IsNullOrEmpty(process.MainWindowTitle))
{
Native.SetWindowText(process.MainWindowHandle, process.ProcessName);
}
if (process.MainWindowTitle.Length > 0)
{
_processDataList.Add(process.ProcessName);
}
}
UpdateList();
}
private void RemoveBorder(String procName) //actually make it frameless
{
var Procs = Process.GetProcesses();
foreach (var proc in Procs)
{
if (_gameFound.Equals(true))
{
_gameFound = false;
return;
}
if (!proc.ProcessName.Equals(procName)) continue;
var pFoundWindow = proc.MainWindowHandle;
var style = Native.GetWindowLong(pFoundWindow, GWL_STYLE);
//get menu
var HMENU = Native.GetMenu(proc.MainWindowHandle);
//get item count
var count = Native.GetMenuItemCount(HMENU);
//loop & remove
for (var i = 0; i < count; i++)
{
Native.RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));
Native.RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));
}
//force a redraw
Native.DrawMenuBar(proc.MainWindowHandle);
Native.SetWindowLong(pFoundWindow, GWL_STYLE,
(style &
~(WindowStyleFlags.ExtendedDlgmodalframe
| WindowStyleFlags.Caption
| WindowStyleFlags.ThickFrame
| WindowStyleFlags.Minimize
| WindowStyleFlags.Maximize
| WindowStyleFlags.SystemMenu
| WindowStyleFlags.MaximizeBox
| WindowStyleFlags.MinimizeBox
| WindowStyleFlags.Border
| WindowStyleFlags.ExtendedComposited)));
//Get the screen bounds from the screen the window is currently active on.
//If on multiple screens it will grab bounds from the screen it is most on.
//If not on any screen it grabs bounds from the screen closest
var bounds = Screen.FromHandle(pFoundWindow).Bounds;
if (!_borderlessWindows.Contains(pFoundWindow.ToInt32().ToString()))
{
//Using bounds.X and bounds.Y instead of 0, 0 so it will orient the window
//on the screen it is currently occupying instead of using the primary screen
Native.SetWindowPos(pFoundWindow, 0, bounds.X, bounds.Y, bounds.Width, bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW);
_borderlessWindows.Add(pFoundWindow.ToInt32().ToString());
} //today I learn the definition of a hot fix
//no more outside window
// CheckNativeResult(() => Native.MoveWindow(pFoundWindow, 0, 0, bounds.Width, bounds.Height, true));
//resets window to main monito
_gameFound = true;
}
_gameFound = false;
}
private void ProcessListSelectedIndexChanged(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
_selectedProcessName = processList.GetItemText(processList.SelectedItem);
selectedProcess.Text = _selectedProcessName + " is selected!";
}
private void MakeBorderless(object sender, EventArgs e)
{
RemoveBorder(_selectedProcessName);
}
private void BlogButtonClick(object sender, EventArgs e)
{
GotoSite("http://andrew.codeusa.net/");
}
private void GitHubButtonClick(object sender, EventArgs e)
{
GotoSite("https://github.com/Codeusa/Borderless-Gaming");
}
public void GotoSite(string url) //open url
{
Process.Start(url);
}
private void UpdateList() // sets data sources
{
processList.DataSource = _processDataList;
}
private void workerTimer_Tick(object sender, EventArgs e)
{
if (_worker.IsBusy) return;
_worker.RunWorkerAsync();
}
private void SendToFavorites(object sender, EventArgs e)
{
if (_selectedProcessName == null || !Favorites.CanAdd(_selectedProcessName))
{
MessageBox.Show("Unable to add " + _selectedProcessName + " already added!", "Uh oh!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
Favorites.AddGame(_selectedProcessName);
Favorites.Save("./Favorites.json");
favoritesList.DataSource = null;
favoritesList.DataSource = Favorites.List;
MessageBox.Show(_selectedProcessName + " added to favorites", "Victory!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void BugReportClick(object sender, EventArgs e)
{
GotoSite("https://github.com/Codeusa/Borderless-Gaming/issues");
}
private void RemoveFavoriteButton(object sender, EventArgs e)
{
if (_selectedFavoriteProcess == null || !Favorites.CanRemove(_selectedFavoriteProcess))
{
MessageBox.Show("Unable to remove " + _selectedFavoriteProcess + " does not exist!", "Uh oh!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
Favorites.Remove("./Favorites.json", _selectedFavoriteProcess);
favoritesList.DataSource = null;
favoritesList.DataSource = Favorites.List;
}
}
private void FavoritesListSelectedIndexChanged(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
_selectedFavoriteProcess = favoritesList.GetItemText(favoritesList.SelectedItem);
}
private void SupportButtonClick(object sender, EventArgs e)
{
GotoSite("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TWHNPSC7HRNR2");
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
//determine whether the cursor is in the taskbar because Microsoft
var cursorNotInBar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
if (WindowState != FormWindowState.Minimized || !cursorNotInBar) return;
ShowInTaskbar = false;
notifyIcon.Visible = true;
// notifyIcon.Icon = SystemIcons.Application;
notifyIcon.BalloonTipText = "Borderless Gaming Minimized";
notifyIcon.ShowBalloonTip(2000);
Hide();
}
private void TrayIconOpen(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
private void TrayIconExit(object sender, EventArgs e)
{
Environment.Exit(0);
}
}
}