|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Drawing; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Windows.Forms; |
| 12 | +using BorderlessGaming.WindowsApi; |
| 13 | + |
| 14 | +namespace BorderlessGaming |
| 15 | +{ |
| 16 | + public partial class CompactWindow : Form |
| 17 | + { |
| 18 | + private const int SW_SHOW = 0x05; |
| 19 | + private const int WS_EX_APPWINDOW = 0x40000; |
| 20 | + private const int GWL_EXSTYLE = -0x14; //never want to hunt this down again |
| 21 | + private const int WS_EX_DLGMODALFRAME = 0x0001; |
| 22 | + private const int WS_EX_TOOLWINDOW = 0x0080; |
| 23 | + private const short SWP_NOMOVE = 0X2; |
| 24 | + private const short SWP_NOSIZE = 1; |
| 25 | + private const short SWP_NOZORDER = 0X4; |
| 26 | + private const int SWP_SHOWWINDOW = 0x0040; |
| 27 | + |
| 28 | + public static uint MF_BYPOSITION = 0x400; |
| 29 | + public static uint MF_REMOVE = 0x1000; |
| 30 | + public static int GWL_STYLE = -16; |
| 31 | + private readonly List<string> _borderlessWindows = new List<string>(); |
| 32 | + private readonly List<string> _processDataList = new List<string>(); |
| 33 | + private readonly List<string> _tempList = new List<string>(); |
| 34 | + private IntPtr _formHandle; |
| 35 | + private bool _gameFound; |
| 36 | + private string _selectedFavoriteProcess; |
| 37 | + private string _selectedProcessName; |
| 38 | + private BackgroundWorker _worker; |
| 39 | + |
| 40 | + public CompactWindow() |
| 41 | + { |
| 42 | + InitializeComponent(); |
| 43 | + CenterToScreen(); |
| 44 | + PopulateList(); |
| 45 | + ListenForGameLaunch(); |
| 46 | + if (favoritesList == null) return; |
| 47 | + favoritesList.DataSource = Favorites.List; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + private void ListenForGameLaunch() |
| 53 | + { |
| 54 | + _formHandle = Handle; |
| 55 | + |
| 56 | + _worker = new BackgroundWorker(); |
| 57 | + _worker.DoWork += _BackgroundWork; |
| 58 | + _worker.RunWorkerCompleted += _BackgroundWorkCompleted; |
| 59 | + |
| 60 | + if (workerTimer != null) |
| 61 | + workerTimer.Start(); |
| 62 | + } |
| 63 | + |
| 64 | + public static IntPtr FindWindowHandle(string processName, IntPtr ignoreHandle) |
| 65 | + { |
| 66 | + var process = |
| 67 | + Process.GetProcesses() |
| 68 | + .FirstOrDefault( |
| 69 | + p => |
| 70 | + p != null && p.ProcessName.Equals(processName, StringComparison.InvariantCultureIgnoreCase) && |
| 71 | + p.MainWindowHandle != IntPtr.Zero && p.MainWindowHandle != ignoreHandle && |
| 72 | + !string.IsNullOrEmpty(p.MainWindowTitle)); |
| 73 | + |
| 74 | + return process != null ? process.MainWindowHandle : IntPtr.Zero; |
| 75 | + } |
| 76 | + |
| 77 | + private void _BackgroundWork(object sender, DoWorkEventArgs e) |
| 78 | + { |
| 79 | + IntPtr handle; |
| 80 | + var breakLoop = false; |
| 81 | + var windowText = ""; |
| 82 | + while (true) |
| 83 | + { |
| 84 | + processList.Invoke((MethodInvoker)delegate |
| 85 | + { |
| 86 | + //Code to modify control will go here |
| 87 | + processList.DataSource = null; |
| 88 | + processList.Items.Clear(); |
| 89 | + _processDataList.Clear(); |
| 90 | + PopulateList(); |
| 91 | + }); |
| 92 | + |
| 93 | + |
| 94 | + Favorites.List.ForEach(wndName => |
| 95 | + { |
| 96 | + handle = FindWindowHandle(wndName, _formHandle); |
| 97 | + |
| 98 | + if (handle != IntPtr.Zero) |
| 99 | + { |
| 100 | + windowText = wndName; |
| 101 | + breakLoop = true; |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + if (breakLoop) |
| 106 | + { |
| 107 | + Thread.Sleep(2000); |
| 108 | + RemoveBorder(windowText); |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + Thread.Sleep(9000); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void _BackgroundWorkCompleted(object sender, RunWorkerCompletedEventArgs e) |
| 117 | + { |
| 118 | + if (!IsDisposed) |
| 119 | + { |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void PopulateList() //Adds active windows to the processDataList |
| 124 | + { |
| 125 | + _tempList.Add("Refreshing..."); |
| 126 | + processList.DataSource = _tempList; |
| 127 | + var processlist = Process.GetProcesses(); |
| 128 | + |
| 129 | + foreach ( |
| 130 | + var process in |
| 131 | + processlist.Where(process => process != null) |
| 132 | + .Where(process => !process.ProcessName.Equals("explorer"))) |
| 133 | + { |
| 134 | + if (String.IsNullOrEmpty(process.MainWindowTitle)) |
| 135 | + { |
| 136 | + Native.SetWindowText(process.MainWindowHandle, process.ProcessName); |
| 137 | + } |
| 138 | + if (process.MainWindowTitle.Length <= 0) continue; |
| 139 | + _processDataList.Add(process.ProcessName); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + UpdateList(); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + private void RemoveBorder(String procName) //actually make it frameless |
| 148 | + { |
| 149 | + var Procs = Process.GetProcesses(); |
| 150 | + foreach (var proc in Procs) |
| 151 | + { |
| 152 | + if (_gameFound.Equals(true)) |
| 153 | + { |
| 154 | + _gameFound = false; |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + if (!proc.ProcessName.Equals(procName)) continue; |
| 159 | + var pFoundWindow = proc.MainWindowHandle; |
| 160 | + var style = Native.GetWindowLong(pFoundWindow, GWL_STYLE); |
| 161 | + |
| 162 | + //get menu |
| 163 | + var HMENU = Native.GetMenu(proc.MainWindowHandle); |
| 164 | + //get item count |
| 165 | + var count = Native.GetMenuItemCount(HMENU); |
| 166 | + //loop & remove |
| 167 | + for (var i = 0; i < count; i++) |
| 168 | + { |
| 169 | + Native.RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE)); |
| 170 | + Native.RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE)); |
| 171 | + } |
| 172 | + |
| 173 | + //force a redraw |
| 174 | + Native.DrawMenuBar(proc.MainWindowHandle); |
| 175 | + Native.SetWindowLong(pFoundWindow, GWL_STYLE, |
| 176 | + (style & |
| 177 | + ~(WindowStyleFlags.ExtendedDlgmodalframe |
| 178 | + | WindowStyleFlags.Caption |
| 179 | + | WindowStyleFlags.ThickFrame |
| 180 | + | WindowStyleFlags.Minimize |
| 181 | + | WindowStyleFlags.Maximize |
| 182 | + | WindowStyleFlags.SystemMenu |
| 183 | + | WindowStyleFlags.MaximizeBox |
| 184 | + | WindowStyleFlags.MinimizeBox |
| 185 | + | WindowStyleFlags.Border |
| 186 | + | WindowStyleFlags.ExtendedComposited))); |
| 187 | + |
| 188 | + |
| 189 | +<<<<<<< HEAD:CompactWindow.cs |
| 190 | + var bounds = Screen.PrimaryScreen.Bounds; |
| 191 | + if (!_borderlessWindows.Contains(pFoundWindow.ToInt32().ToString())) |
| 192 | + { |
| 193 | + Native.SetWindowPos(pFoundWindow, 0, bounds.X, bounds.Y, bounds.Width, bounds.Height, SetWindowPosFlags.NoZOrder | SetWindowPosFlags.ShowWindow); |
| 194 | + _borderlessWindows.Add(pFoundWindow.ToInt32().ToString()); |
| 195 | + } //today I learn the definition of a hot fix |
| 196 | + |
| 197 | +======= |
| 198 | + //Get the screen bounds from the screen the window is currently active on. |
| 199 | + //If on multiple screens it will grab bounds from the screen it is most on. |
| 200 | + //If not on any screen it grabs bounds from the screen closest |
| 201 | + var bounds = Screen.FromHandle(pFoundWindow).Bounds; |
| 202 | + |
| 203 | + if (!_borderlessWindows.Contains(pFoundWindow.ToInt32().ToString())) |
| 204 | + { |
| 205 | + //Using bounds.X and bounds.Y instead of 0, 0 so it will orient the window |
| 206 | + //on the screen it is currently occupying instead of using the primary screen |
| 207 | + Native.SetWindowPos(pFoundWindow, 0, bounds.X, bounds.Y, bounds.Width, bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW); |
| 208 | + _borderlessWindows.Add(pFoundWindow.ToInt32().ToString()); |
| 209 | + } //today I learn the definition of a hot fix |
| 210 | + |
| 211 | + |
| 212 | +>>>>>>> origin/master:Borderless.cs |
| 213 | + //no more outside window |
| 214 | + // CheckNativeResult(() => Native.MoveWindow(pFoundWindow, 0, 0, bounds.Width, bounds.Height, true)); |
| 215 | + //resets window to main monito |
| 216 | + |
| 217 | + _gameFound = true; |
| 218 | + } |
| 219 | + |
| 220 | + _gameFound = false; |
| 221 | + } |
| 222 | + |
| 223 | + private void ProcessListSelectedIndexChanged(object sender, EventArgs e) |
| 224 | + { |
| 225 | + if (e == null) throw new ArgumentNullException("e"); |
| 226 | + _selectedProcessName = processList.GetItemText(processList.SelectedItem); |
| 227 | + selectedProcess.Text = _selectedProcessName + " is selected!"; |
| 228 | + } |
| 229 | + |
| 230 | + private void MakeBorderless(object sender, EventArgs e) |
| 231 | + { |
| 232 | + RemoveBorder(_selectedProcessName); |
| 233 | + } |
| 234 | + |
| 235 | + private void BlogButtonClick(object sender, EventArgs e) |
| 236 | + { |
| 237 | + GotoSite("http://andrew.codeusa.net/"); |
| 238 | + } |
| 239 | + |
| 240 | + |
| 241 | + private void GitHubButtonClick(object sender, EventArgs e) |
| 242 | + { |
| 243 | + GotoSite("https://github.com/Codeusa/Borderless-Gaming"); |
| 244 | + } |
| 245 | + |
| 246 | + public void GotoSite(string url) //open url |
| 247 | + { |
| 248 | + Process.Start(url); |
| 249 | + } |
| 250 | + |
| 251 | + private void UpdateList() // sets data sources |
| 252 | + { |
| 253 | + processList.DataSource = _processDataList; |
| 254 | + } |
| 255 | + |
| 256 | + private void workerTimer_Tick(object sender, EventArgs e) |
| 257 | + { |
| 258 | + if (_worker.IsBusy) return; |
| 259 | + _worker.RunWorkerAsync(); |
| 260 | + } |
| 261 | + |
| 262 | + private void SendToFavorites(object sender, EventArgs e) |
| 263 | + { |
| 264 | + if (_selectedProcessName == null || !Favorites.CanAdd(_selectedProcessName)) |
| 265 | + { |
| 266 | + MessageBox.Show("Unable to add " + _selectedProcessName + " already added!", "Uh oh!", |
| 267 | + MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 268 | + } |
| 269 | + else |
| 270 | + { |
| 271 | + Favorites.AddGame(_selectedProcessName); |
| 272 | + Favorites.Save("./Favorites.json"); |
| 273 | + favoritesList.DataSource = null; |
| 274 | + favoritesList.DataSource = Favorites.List; |
| 275 | + MessageBox.Show(_selectedProcessName + " added to favorites", "Victory!", |
| 276 | + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + private void BugReportClick(object sender, EventArgs e) |
| 281 | + { |
| 282 | + GotoSite("https://github.com/Codeusa/Borderless-Gaming/issues"); |
| 283 | + } |
| 284 | + |
| 285 | + |
| 286 | + private void RemoveFavoriteButton(object sender, EventArgs e) |
| 287 | + { |
| 288 | + if (_selectedFavoriteProcess == null || !Favorites.CanRemove(_selectedFavoriteProcess)) |
| 289 | + { |
| 290 | + MessageBox.Show("Unable to remove " + _selectedFavoriteProcess + " does not exist!", "Uh oh!", |
| 291 | + MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 292 | + } |
| 293 | + else |
| 294 | + { |
| 295 | + Favorites.Remove("./Favorites.json", _selectedFavoriteProcess); |
| 296 | + favoritesList.DataSource = null; |
| 297 | + favoritesList.DataSource = Favorites.List; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + private void FavoritesListSelectedIndexChanged(object sender, EventArgs e) |
| 302 | + { |
| 303 | + if (e == null) throw new ArgumentNullException("e"); |
| 304 | + _selectedFavoriteProcess = favoritesList.GetItemText(favoritesList.SelectedItem); |
| 305 | + } |
| 306 | + |
| 307 | + private void SupportButtonClick(object sender, EventArgs e) |
| 308 | + { |
| 309 | + GotoSite("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TWHNPSC7HRNR2"); |
| 310 | + } |
| 311 | + |
| 312 | + protected override void OnResize(EventArgs e) |
| 313 | + { |
| 314 | + base.OnResize(e); |
| 315 | + //determine whether the cursor is in the taskbar because Microsoft |
| 316 | + var cursorNotInBar = Screen.GetWorkingArea(this).Contains(Cursor.Position); |
| 317 | + if (WindowState != FormWindowState.Minimized || !cursorNotInBar) return; |
| 318 | + ShowInTaskbar = false; |
| 319 | + notifyIcon.Visible = true; |
| 320 | + // notifyIcon.Icon = SystemIcons.Application; |
| 321 | + notifyIcon.BalloonTipText = "Borderless Gaming Minimized"; |
| 322 | + notifyIcon.ShowBalloonTip(2000); |
| 323 | + Hide(); |
| 324 | + } |
| 325 | + |
| 326 | + private void TrayIconOpen(object sender, EventArgs e) |
| 327 | + { |
| 328 | + Show(); |
| 329 | + WindowState = FormWindowState.Normal; |
| 330 | + ShowInTaskbar = true; |
| 331 | + } |
| 332 | + |
| 333 | + private void TrayIconExit(object sender, EventArgs e) |
| 334 | + { |
| 335 | + Environment.Exit(0); |
| 336 | + } |
| 337 | + } |
| 338 | +} |
0 commit comments