Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BorderlessGaming.Common;
- using BorderlessGaming.Utilities;
- using BorderlessGaming.Forms;
- using BorderlessGaming.WindowsAPI;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace BoarderlessGaming
- {
- public class BoarderlessGaming
- {
- public const string HiddenFile = "HiddenProcess.json";
- public const string FavoritesFile = "Favorites.json";
- public string readonly string Datapath;
- static BoarderlessGaming()
- {
- DataPath = Tools.GetDataPath();
- }
- private readonly MainWondow window;
- private readonly Favorites _favorites;
- private readonly HiddenProcesses _hiddenProcesses;
- private readonly ProcessDetailList _processDetails;
- private readonly Windows windoes;
- private CancellationTokenSource workerTaskToken;
- public HiddenProcesses HiddenProcesses { get { return _hiddenProcesses; } }
- public Favorites Favorites { get { return _favorites; } }
- public bool AutoHandleFavorites { get; set; }
- public BoarderlessGaming(MainWindow window)
- {
- this.window = window;
- _favorites = new Favorites(Path.Combine(Datapath, FavoritesFile));
- _hiddenProcesses = new HiddenProcesses(Path.Combine(Datapath, HiddenFile));
- _processDetails = new ProcessDetailList();
- windows = new Windows();
- AutoHandleFavorites = true;
- }
- public void Start()
- {
- workerTaskToken = new CancellationTokenSource();
- Task.Factory.StartNew(DoMainWork, workerTaskToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
- }
- private void DoMainWork()
- {
- while (!this.workerTaskToken.IsCancellationRequested)
- {
- this.UpdateProcesses();
- if (this.AutoHandleFavorites)
- {
- foreach (var pd in _processDetails)
- {
- foreach (var fav_process in Favorites)
- {
- if (fav_process.Matches(pd))
- {
- RemoveBorder(pd, fav_process);
- }
- }
- }
- }
- Task.WaitAll(Task.Delay(3000));
- }
- }
- private object updateLock = new object();
- private void UpdateProcesses()
- {
- try
- {
- if (!this.AutoHandleFavorites)
- {
- MainWindow frm = MainWindow.ext();
- if (frm != null)
- if ((frm.WindowState == FormWindowState.Minimized) || (!frm.Visible))
- return;
- }
- }
- catch { }
- lock (this.updateLock)
- {
- for (int i = 0; i < this._processDetails.Count;)
- {
- try
- {
- ProcessDetails pd = this._processDetails[i];
- bool should_be_pruned = pd.ProcessHasExited;
- if (!should_be_pruned)
- {
- string current_title = "";
- if (!pd.NoAccess)
- {
- Tools.StartMethodMultithreadedAndWait(() => { current_title = Native.GetWindowTitle(pd.WindowHandle); }, (Utilities.AppEnvironment.SettingValue("SlowWindowDetection", false)) ? 10 : 2);
- should_be_pruned = should_be_pruned || (pd.WindowTitle != current_title);
- }
- }
- if (should_be_pruned)
- {
- if (pd.MadeBorderless)
- HandlePrunedProcess(pd);
- _processDetails.RemoveAt(i);
- }
- else
- i++;
- }
- catch
- {
- i++;
- }
- }
- try
- {
- windows.QueryProcessesWithWindows((pd) =>
- {
- if (_hiddenProcesses.IsHidden(pd.Proc.ProcessName))
- return;
- if (!_processDetails.Select(p => p.Proc.Id).Contains(pd.Proc.Id) ||
- !_processDetails.Select(p => p.WindowTitle).Contains(pd.WindowTitle))
- _processDetails.Add(pd);
- }, _processDetails.WindowPtrSet);
- }
- catch { }
- window.lblUpdateStatus.Text = "Right-click for more options. Last updated " + DateTime.Now.ToString();
- }
- }
- public Task RefreshProcesses()
- {
- lock (this.updateLock)
- {
- this._processDetails.ClearProcesses();
- }
- return Task.Factory.StartNew(this.UpdateProcesses);
- }
- private void HandlePrunedProcess(ProcessDetails pd)
- {
- foreach (var fav in _favorites)
- {
- if (fav.Matches(pd))
- {
- if (fav.HideWindowsTaskbar)
- Manipulation.ToggleWindowsTaskbarVisibility(Tools.Boolstate.True);
- if (fav.HideMouseCursor)
- Manipulation.ToggleMouseCursorVisibility(window, Tools.Boolstate.True);
- }
- }
- }
- public void RemoveBorder(ProcessDetails pd, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
- {
- if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
- {
- Task task = new Task(() => RemoveBorder(pd, favDetails, true));
- task.Wait(TimeSpan.FromSeconds(10));
- }
- Manipulation.MakeWindowBorderless(pd, window, pd.WindowHandle, new Rectangle(), favDetails ?? _favorites.FromProcessDetails(pd));
- }
- public void RemoveBorder_ToSpecificScreen(IntPtr hWnd, Screen screen, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
- {
- if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
- {
- Task task = new Task(() => RemoveBorder_ToSpecificScreen(hWnd, screen, favDetails, true));
- task.Wait(TimeSpan.FromSeconds(10));
- }
- var pd = _processDetails.FromHandle(hWnd);
- Manipulation.MakeWindowBorderless(pd, window, hWnd, screen.Bounds, favDetails ?? _favorites.FromProcessDetails(pd));
- }
- public void RemoveBorder_ToSpecificRect(IntPtr hWnd, Rectangle targetFrame, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
- {
- if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
- {
- Task task = new Task(() => RemoveBorder_ToSpecificRect(hWnd, targetFrame, favDetails, true));
- task.Wait(TimeSpan.FromSeconds(10));
- }
- var pd = _processDetails.FromHandle(hWnd);
- Manipulation.MakeWindowBorderless(pd, window, hWnd, targetFrame, favDetails ?? _favorites.FromProcessDetails(pd));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment