CGC_Codes

Sample BGing

Feb 22nd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.07 KB | None | 0 0
  1. using BorderlessGaming.Common;
  2. using BorderlessGaming.Utilities;
  3. using BorderlessGaming.Forms;
  4. using BorderlessGaming.WindowsAPI;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15.  
  16.  
  17. namespace BoarderlessGaming
  18. {
  19.     public class BoarderlessGaming
  20.     {
  21.         public const string HiddenFile = "HiddenProcess.json";
  22.         public const string FavoritesFile = "Favorites.json";
  23.  
  24.         public string readonly string Datapath;
  25.  
  26.         static BoarderlessGaming()
  27.         {
  28.             DataPath = Tools.GetDataPath();
  29.         }
  30.  
  31.         private readonly MainWondow window;
  32.         private readonly Favorites _favorites;
  33.         private readonly HiddenProcesses _hiddenProcesses;
  34.         private readonly ProcessDetailList _processDetails;
  35.         private readonly Windows windoes;
  36.  
  37.         private CancellationTokenSource workerTaskToken;
  38.  
  39.         public HiddenProcesses HiddenProcesses { get { return _hiddenProcesses; } }
  40.  
  41.         public Favorites Favorites { get { return _favorites; } }
  42.  
  43.         public bool AutoHandleFavorites { get; set; }
  44.  
  45.         public BoarderlessGaming(MainWindow window)
  46.         {
  47.             this.window = window;
  48.             _favorites = new Favorites(Path.Combine(Datapath, FavoritesFile));
  49.             _hiddenProcesses = new HiddenProcesses(Path.Combine(Datapath, HiddenFile));
  50.             _processDetails = new ProcessDetailList();
  51.             windows = new Windows();
  52.             AutoHandleFavorites = true;
  53.         }
  54.  
  55.         public void Start()
  56.         {
  57.             workerTaskToken = new CancellationTokenSource();
  58.             Task.Factory.StartNew(DoMainWork, workerTaskToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
  59.         }
  60.  
  61.         private void DoMainWork()
  62.         {
  63.             while (!this.workerTaskToken.IsCancellationRequested)
  64.             {
  65.                
  66.                 this.UpdateProcesses();
  67.  
  68.                 if (this.AutoHandleFavorites)
  69.                 {
  70.                    
  71.                     foreach (var pd in _processDetails)
  72.                     {
  73.                         foreach (var fav_process in Favorites)
  74.                         {
  75.                             if (fav_process.Matches(pd))
  76.                             {
  77.                                 RemoveBorder(pd, fav_process);
  78.                             }
  79.                         }
  80.                     }
  81.                 }
  82.                 Task.WaitAll(Task.Delay(3000));
  83.             }
  84.         }
  85.  
  86.         private object updateLock = new object();
  87.  
  88.         private void UpdateProcesses()
  89.         {
  90.            
  91.  
  92.             try
  93.             {
  94.                 if (!this.AutoHandleFavorites)
  95.                 {
  96.                     MainWindow frm = MainWindow.ext();
  97.  
  98.                     if (frm != null)
  99.                         if ((frm.WindowState == FormWindowState.Minimized) || (!frm.Visible))
  100.                             return;
  101.                 }
  102.             }
  103.             catch { }
  104.  
  105.             lock (this.updateLock)
  106.             {
  107.                
  108.                 for (int i = 0; i < this._processDetails.Count;)
  109.                 {
  110.                     try
  111.                     {
  112.                         ProcessDetails pd = this._processDetails[i];
  113.  
  114.                         bool should_be_pruned = pd.ProcessHasExited;
  115.  
  116.                         if (!should_be_pruned)
  117.                         {
  118.                             string current_title = "";
  119.  
  120.                             if (!pd.NoAccess)
  121.                             {
  122.                                
  123.                                 Tools.StartMethodMultithreadedAndWait(() => { current_title = Native.GetWindowTitle(pd.WindowHandle); }, (Utilities.AppEnvironment.SettingValue("SlowWindowDetection", false)) ? 10 : 2);
  124.                                 should_be_pruned = should_be_pruned || (pd.WindowTitle != current_title);
  125.                             }
  126.                         }
  127.  
  128.                         if (should_be_pruned)
  129.                         {
  130.                             if (pd.MadeBorderless)
  131.                                 HandlePrunedProcess(pd);
  132.                             _processDetails.RemoveAt(i);
  133.                         }
  134.                         else
  135.                             i++;
  136.                     }
  137.                     catch
  138.                     {
  139.                        
  140.                         i++;
  141.                     }
  142.                 }
  143.  
  144.                
  145.                 try
  146.                 {
  147.                     windows.QueryProcessesWithWindows((pd) =>
  148.                     {
  149.                         if (_hiddenProcesses.IsHidden(pd.Proc.ProcessName))
  150.                             return;
  151.                         if (!_processDetails.Select(p => p.Proc.Id).Contains(pd.Proc.Id) ||
  152.                             !_processDetails.Select(p => p.WindowTitle).Contains(pd.WindowTitle))
  153.                             _processDetails.Add(pd);
  154.                     }, _processDetails.WindowPtrSet);
  155.                 }
  156.                 catch { }
  157.  
  158.                
  159.                 window.lblUpdateStatus.Text = "Right-click for more options.  Last updated " + DateTime.Now.ToString();
  160.             }
  161.         }
  162.  
  163.         public Task RefreshProcesses()
  164.         {
  165.             lock (this.updateLock)
  166.             {
  167.                 this._processDetails.ClearProcesses();
  168.             }
  169.  
  170.             return Task.Factory.StartNew(this.UpdateProcesses);
  171.         }
  172.  
  173.        
  174.         private void HandlePrunedProcess(ProcessDetails pd)
  175.         {
  176.            
  177.             foreach (var fav in _favorites)
  178.             {
  179.                 if (fav.Matches(pd))
  180.                 {
  181.                     if (fav.HideWindowsTaskbar)
  182.                         Manipulation.ToggleWindowsTaskbarVisibility(Tools.Boolstate.True);
  183.                     if (fav.HideMouseCursor)
  184.                         Manipulation.ToggleMouseCursorVisibility(window, Tools.Boolstate.True);
  185.                 }
  186.             }
  187.         }
  188.  
  189.        
  190.         public void RemoveBorder(ProcessDetails pd, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
  191.         {
  192.             if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
  193.             {
  194.                
  195.                 Task task = new Task(() => RemoveBorder(pd, favDetails, true));
  196.                 task.Wait(TimeSpan.FromSeconds(10));
  197.             }
  198.             Manipulation.MakeWindowBorderless(pd, window, pd.WindowHandle, new Rectangle(), favDetails ?? _favorites.FromProcessDetails(pd));
  199.         }
  200.  
  201.        
  202.         public void RemoveBorder_ToSpecificScreen(IntPtr hWnd, Screen screen, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
  203.         {
  204.             if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
  205.             {
  206.                
  207.                 Task task = new Task(() => RemoveBorder_ToSpecificScreen(hWnd, screen, favDetails, true));
  208.                 task.Wait(TimeSpan.FromSeconds(10));
  209.             }
  210.  
  211.             var pd = _processDetails.FromHandle(hWnd);
  212.             Manipulation.MakeWindowBorderless(pd, window, hWnd, screen.Bounds, favDetails ?? _favorites.FromProcessDetails(pd));
  213.         }
  214.  
  215.      
  216.         public void RemoveBorder_ToSpecificRect(IntPtr hWnd, Rectangle targetFrame, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
  217.         {
  218.             if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
  219.             {
  220.                
  221.                 Task task = new Task(() => RemoveBorder_ToSpecificRect(hWnd, targetFrame, favDetails, true));
  222.                 task.Wait(TimeSpan.FromSeconds(10));
  223.             }
  224.             var pd = _processDetails.FromHandle(hWnd);
  225.             Manipulation.MakeWindowBorderless(pd, window, hWnd, targetFrame, favDetails ?? _favorites.FromProcessDetails(pd));
  226.         }
  227.  
  228.     }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment