How do I get the title of the current active window using c#?
Asked Answered
E

7

128

I'd like to know how to grab the Window title of the current active window (i.e. the one that has focus) using C#.

Effector answered 22/9, 2008 at 16:22 Comment(2)
Were you trying to determine which window within your application has focus or which window of any application has focus?Cultured
this is relevant #2423734 so if you wanted a button click to do it then it's worth making sure your form doesn't take focus.Episcopal
H
191

See example on how you can do this with full source code here:

http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-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()
{
    const int nChars = 256;
    StringBuilder Buff = new StringBuilder(nChars);
    IntPtr handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        return Buff.ToString();
    }
    return null;
}

Edited with @Doug McClean comments for better correctness.

Holiness answered 22/9, 2008 at 16:27 Comment(7)
Don't forget to be a good citizen. blogs.msdn.com/oldnewthing/archive/2007/07/27/4072156.aspx and blogs.msdn.com/oldnewthing/archive/2008/10/06/8969399.aspx have relevant info.Summerlin
a newb note, to get it to run, using System.Runtime.InteropServices; and re where to put the dll import and static extern lines. pasting it within the classEpiscopal
@smink How get Active foreground window for specific user (let's say process runs as service).Jibe
Site you've linked to isn't available. Here is (possibly) web archive of it: web.archive.org/web/20150814043810/http://www.csharphelp.com/…Horde
Also, I want my app to be notified every time foreground window will change. Is there any event for that?Horde
this works perfectly when the text is in english. but when the text is hebrew I get question marks instead of the hebrew text. is there a way around that?Biosynthesis
Can I use this to access the SaveFileDialog, I want to access the dialog window and want to set its file location, filename and click the save button programmatically..Stake
C
19

If you were talking about WPF then use:

 Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
Coralloid answered 16/4, 2010 at 10:43 Comment(2)
If the entire application isn't active (another program has focus), then no window will have IsActive set to true.Vaunt
Actually that could be wrong, in my case I was trying to access the Window array on a non-UI thread. However, also see this in case I am still right: social.msdn.microsoft.com/Forums/vstudio/en-US/…Vaunt
B
11

Based on GetForegroundWindow function | Microsoft Docs:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);

private string GetCaptionOfActiveWindow()
{
    var strTitle = string.Empty;
    var handle = GetForegroundWindow();
    // Obtain the length of the text   
    var intLength = GetWindowTextLength(handle) + 1;
    var stringBuilder = new StringBuilder(intLength);
    if (GetWindowText(handle, stringBuilder, intLength) > 0)
    {
        strTitle = stringBuilder.ToString();
    }
    return strTitle;
}

It supports UTF8 characters.

Bb answered 24/11, 2017 at 4:12 Comment(0)
D
5

Loop over Application.Current.Windows[] and find the one with IsActive == true.

Deventer answered 27/8, 2009 at 17:47 Comment(1)
Wouldn't this only work for the windows in the current .Net application? I think d4nt wants to get the title of the current active window on the desktop, no matter what application it belongs to.Echinate
M
3

Use the Windows API. Call GetForegroundWindow().

GetForegroundWindow() will give you a handle (named hWnd) to the active window.

Documentation: GetForegroundWindow function | Microsoft Docs

Mazy answered 22/9, 2008 at 16:25 Comment(0)
R
0

If it happens that you need the Current Active Form from your MDI application: (MDI- Multi Document Interface).

Form activForm;
activForm = Form.ActiveForm.ActiveMdiChild;
Resolutive answered 24/10, 2016 at 14:19 Comment(0)
A
-4

you can use process class it's very easy. use this namespace

using System.Diagnostics;

if you want to make a button to get active window.

private void button1_Click(object sender, EventArgs e)
    {            
       Process currentp = Process.GetCurrentProcess();
       TextBox1.Text = currentp.MainWindowTitle;  //this textbox will be filled with active window.
    }
Autocracy answered 14/9, 2015 at 5:53 Comment(1)
This is wrong. It will show the title of your program not of the current active window.Risible

© 2022 - 2024 — McMap. All rights reserved.