Get Active Window Title

Get Active Window Title in C:

#include <windows.h>
#include <stdio.h>
 
int main()
{
	char title[256] = "";
	HWND handle = GetForegroundWindow();
	if (handle)
	{
		GetWindowText(handle, title, 256);
		printf("%srn",title);
	}
	else 
	{
		printf("not foundrn");
	}
	return 0;
}

Get Active Window Title in C#:

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
 
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
 
private string GetActiveWindowTitle()
{
	IntPtr handle = IntPtr.Zero;
	StringBuilder title = new StringBuilder(256);
	handle = GetForegroundWindow();
	if (handle != IntPtr.Zero)
	{
		if (GetWindowText(handle, title, 256) > 0)
			return title.ToString();
	}
	return "not found";
}
Categories: C, C# Tags: , , , , ,
%d bloggers like this: