Click here to Skip to main content
Licence CPOL
First Posted 1 Apr 2009
Views 7,533
Downloads 200
Bookmarked 13 times

Control in Focus in Other Processes

By | 1 Apr 2009 | Article
Retrieve the hWnd of focused controls in other applications.

FocusedControlInOtherProcess

Introduction

This article will describe how you can retrieve the unique handle of any focusable control in any Windows application by simply focusing it. We will do this through a series of P/Invokes to the Win32 API in user32.dll.

Background

People using the Win32 API function GetFocus may have noticed that it will only return the handle (hWnd) of controls in your own application, and if you try focusing in another process, you will get NULL. This is because GetFocus only returns the window with the keyboard focus for the current thread's message queue. And, since our application is not in the same thread, you will get nothing.

Using the code

To get the currently focused control hWnd in another process, we can attach our thread's message queue to a window in another thread, using the AttachThreadInput function.

This is how it's done:

  1. Use the GetForegroundWindow function to get the window with which the user is currently working.
  2. Use the GetWindowThreadProcessId function to get the ID of both this window and yours.
  3. Use the AttachThreadInput function to temporarily associate your thread's message queue with the thread owning the other window.
  4. Use the GetFocus function to get the hWnd!
  5. Use the AttachThreadInput function again to disconnect from the other thread.
using System.Runtime.InteropServices;

public partial class FormMain : Form
{
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

    [DllImport("user32.dll")]
    static extern IntPtr AttachThreadInput(IntPtr idAttach, 
                         IntPtr idAttachTo, bool fAttach);

    [DllImport("user32.dll")]
    static extern IntPtr GetFocus();

    public FormMain()
    {
        InitializeComponent();
    }

    private void timerUpdate_Tick(object sender, EventArgs e)
    {
        labelHandle.Text = "hWnd: " + 
                           FocusedControlInActiveWindow().ToString();
    }

    private IntPtr FocusedControlInActiveWindow()
    {
        IntPtr activeWindowHandle = GetForegroundWindow();

        IntPtr activeWindowThread = 
          GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero);
        IntPtr thisWindowThread = GetWindowThreadProcessId(this.Handle, IntPtr.Zero);

        AttachThreadInput(activeWindowThread, thisWindowThread, true);
        IntPtr focusedControlHandle = GetFocus();
        AttachThreadInput(activeWindowThread, thisWindowThread, false);

        return focusedControlHandle;
    }
}

Requirements for the code, which can be done with the Visual Studio designer:

  • Add a timer with the name timerUpdate and add the timerUpdate_Tick method to the Tick event. Set Interval to 100 and Enabled to true.
  • Add a Label to the form with the name labelHandle.
  • Set FormMain's TopMost property to true.

History

  • 1.0 (1 April 2009) - Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Andrec

Software Developer

Sweden Sweden

Member

André Claesson is a developer that uses his skills both in work and as a hobby. He started to write applications for Windows at the age of 15 and sold his first shareware at 16.
He has helped several companies with software projects and has also been employed by UIQ Technology as a C++ developer for almost 3 years.
He is very fond of using new technologies in his projects and he thinks C# and .NET are exiting alternatives to native code.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan I call it from a console app? PinmemberHana Giat5:08 30 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 1 Apr 2009
Article Copyright 2009 by Andrec
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid