122

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#.

Shimmy Weitzhandler
  • 97,705
  • 120
  • 409
  • 613
d4nt
  • 14,827
  • 8
  • 39
  • 51
  • 2
    Were you trying to determine which window within your application has focus or which window of any application has focus? – Peder Rice Jul 15 '14 at 14:51
  • this is relevant https://stackoverflow.com/questions/2423234/make-a-form-not-focusable-in-c-sharp/2428108#2428108 so if you wanted a button click to do it then it's worth making sure your form doesn't take focus. – barlop Nov 11 '17 at 01:30

7 Answers7

182

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.

Răzvan Flavius Panda
  • 21,136
  • 15
  • 108
  • 159
Jorge Ferreira
  • 92,915
  • 25
  • 117
  • 131
  • 7
    Don't forget to be a good citizen. http://blogs.msdn.com/oldnewthing/archive/2007/07/27/4072156.aspx and http://blogs.msdn.com/oldnewthing/archive/2008/10/06/8969399.aspx have relevant info. – Greg D Oct 25 '08 at 15:59
  • 3
    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 class – barlop Jul 04 '15 at 02:33
  • 1
    @smink How get Active foreground window for specific user (let's say process runs as service). – tchelidze Jul 06 '16 at 16:34
  • 1
    Site you've linked to isn't available. Here is (possibly) web archive of it: https://web.archive.org/web/20150814043810/http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/ – Piotrek Dec 28 '17 at 14:19
  • 2
    Also, I want my app to be notified every time foreground window will change. Is there any event for that? – Piotrek Dec 28 '17 at 14:27
  • 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? – Amit Raz Jul 24 '18 at 10:34
  • If i use GetActiveWindowTitle on click of a button I get the title of my app, Can I get the title of the previous window that was on focus ? i.e chrome ? – user2396640 May 25 '20 at 16:27
  • 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.. – Jay Aug 17 '20 at 12:02
19

If you were talking about WPF then use:

 Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
Shimmy Weitzhandler
  • 97,705
  • 120
  • 409
  • 613
Skvettn
  • 201
  • 3
  • 5
  • If the entire application isn't active (another program has focus), then no window will have IsActive set to true. – Kind Contributor Jun 22 '17 at 04:55
  • 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: https://social.msdn.microsoft.com/Forums/vstudio/en-US/654c74fe-0a88-425f-99cd-ccf97ff234bc/active-window-of-an-inactive-application?forum=wpf – Kind Contributor Jun 22 '17 at 05:23
8

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.

shA.t
  • 15,880
  • 5
  • 49
  • 104
Mohammad Dayyan
  • 20,033
  • 39
  • 154
  • 219
5

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

shA.t
  • 15,880
  • 5
  • 49
  • 104
  • 13
    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. – Quagmire Apr 16 '10 at 10:46
3

Use the Windows API. Call GetForegroundWindow().

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

Documentation: GetForegroundWindow function | Microsoft Docs

shA.t
  • 15,880
  • 5
  • 49
  • 104
ine
  • 13,863
  • 8
  • 53
  • 79
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;
Arthur Zennig
  • 1,780
  • 21
  • 17
-3

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.
    }