get active window handle and title
Published: 7. February 2012 | Updated: 7. February 2012License: Microsoft Public License (MS-PL)
Categories: Windows » Win32, Windows
Tags: C# UI Win32 Windows
Import namespaces
using System; using System.Runtime.InteropServices; using System.Text; using System.Threading;
External methods
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
Code
// get handle IntPtr handle = GetForegroundWindow(); // get title const int count = 512; var text = new StringBuilder(count); if (GetWindowText(handle, text, count) > 0) { Console.WriteLine(text.ToString()); }
| Send us feedback about this snippet » |





