125

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
  • 101,809
  • 122
  • 424
  • 632
d4nt
  • 15,475
  • 9
  • 42
  • 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

186

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,730
  • 17
  • 111
  • 169
Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
  • 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
  • 4
    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
  • 2
    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
  • 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
  • 101,809
  • 122
  • 424
  • 632
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
10

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
  • 16,580
  • 5
  • 54
  • 111
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
5

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

shA.t
  • 16,580
  • 5
  • 54
  • 111
  • 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
  • 16,580
  • 5
  • 54
  • 111
ine
  • 14,014
  • 8
  • 55
  • 80
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
  • 2,058
  • 26
  • 20
-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.
    }