How do I test if a window has focus (C#)?

Posted on 2009-02-12
Last Modified: 2013-12-17
I need to test if a Window has focus in C#. This window is an external application -- I am not looking to test if one of my forms is in focus. The only thing I know about the external application is it's process name (karafun.exe) and the title of the Window ("KaraFun").

Specifically, I'm looking to see when a karaoke song has ended, and the easiest way to do that is to see if the player control has focus. At this point I can load in a new song. So I guess what I need is code to: 1) Find the window by title (I used to know how to do this, but it's been a while) 2) Detect whether it has focus.

It would also be okay in this case to just get the title of the window with focus.
Question by:tylermenezes
    LVL 29

    Assisted Solution

    I believe you can achieve that with pinvoke. See for examples:
    LVL 85

    Accepted Solution

    Here is an approach using Process.GetProcessesByName() and the GetForegroundWindow() API:
    using System;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    namespace WindowsFormsApplication1
        public partial class Form1 : Form
            public Form1()
            public static extern IntPtr GetForegroundWindow();
            private void button1_Click(object sender, EventArgs e)
                Boolean focused = false;
                Process[] p = Process.GetProcessesByName("karafun");
                if (p.Length > 0)
                    if (p[0].MainWindowHandle.Equals(GetForegroundWindow()))
                        focused = true;
                if (focused)
                    MessageBox.Show("KaraFun IS in focus.");
                    MessageBox.Show("KaraFun is NOT in focus.");

    Open in new window


    Author Closing Comment

    Thanks to both of you. Neither solution completely addressed my problem (the former was more of a reference and the latter returned true for any window that process had open) but they both helped me solve the problem. Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now