Advertisement
Guest User

Untitled

a guest
Aug 9th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApp21
  8. {
  9.     class Program
  10.     {
  11.         [DllImport("user32")]
  12.         private static extern IntPtr GetForegroundWindow();
  13.  
  14.         [DllImport("user32", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)]
  15.         public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
  16.  
  17.         const int WM_GETTEXT = 0x000D;
  18.  
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             string previous_title = null;
  23.  
  24.  
  25.             for (; ; )
  26.             {
  27.                 IntPtr hwnd = WinGetHandle("CDisplayEx");
  28.                 if (hwnd == IntPtr.Zero) continue;
  29.  
  30.                 StringBuilder sb = new StringBuilder(700);
  31.  
  32.                 SendMessage(hwnd, WM_GETTEXT, sb.Capacity, sb);
  33.  
  34.                 string title = sb.ToString();
  35.  
  36.                 if (previous_title != title)
  37.                 {
  38.                     previous_title = title;
  39.  
  40.                     Console.WriteLine($"Window title: {title}");
  41.                 }
  42.  
  43.                 Thread.Sle\u0065p(100);
  44.             }
  45.         }
  46.         public static IntPtr WinGetHandle(string name)
  47.         {
  48.             foreach (Process pList in Process.GetProcessesByName(name))
  49.                 return pList.MainWindowHandle;
  50.             return IntPtr.Zero;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement