Advertisement
Guest User

Keep Chrome TopMost

a guest
Apr 19th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace Force_TopMost
  7. {
  8.     class Program
  9.     {
  10.         [DllImport("User32.dll")]
  11.         private static extern bool SetForegroundWindow(IntPtr hWnd);
  12.         static void Main(string[] args)
  13.         {
  14.                 foreach (Process p in Process.GetProcesses())
  15.                 {
  16.                     try
  17.                     {
  18.                         String mwt = p.MainWindowTitle;
  19.                         if (!mwt.Contains("Chrome")) continue;
  20.                         IntPtr hWnd = p.MainWindowHandle;
  21.                         Console.WriteLine(String.Format("{0}\t{1}", mwt, hWnd));
  22.  
  23.  
  24.                     Console.WriteLine("Force Topmost?");
  25.                     if(Console.ReadKey().Key == ConsoleKey.Y)
  26.                     {
  27.                         Thread thdKeepTopmost = new Thread(() => keepTopMost(hWnd));
  28.                         thdKeepTopmost.IsBackground = true;
  29.                         thdKeepTopmost.Start();
  30.                     }
  31.  
  32.                        
  33.                     }
  34.                     catch (Exception ex)
  35.                     {
  36.                         Console.WriteLine(ex.Message);
  37.                     }
  38.                 }
  39.            
  40.             Console.ReadKey();
  41.             kill = true;
  42.         }
  43.  
  44.         public static bool kill = false;
  45.         public static void keepTopMost(IntPtr hWnd)
  46.         {
  47.             while (!kill)
  48.             {
  49.                 SetForegroundWindow(hWnd);
  50.                 Thread.Sleep(2200); // Dirty CPU preservation.
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement