Find and Activate External Windows

Find and Activate External Windows

It is very easy to obtain access to an external running window and activate it. All you need is the actual title of the window ( the text written in the titlebar of the window) and FindWindow and SetForegroundWindow APIs.

FindWindow gets access to the window in question. SetForegroundWindow activates the window thus bringing it to the front. Here is a small example:

using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices; //required for APIsnamespace Find{public partial class Form1 : Form{//Import the FindWindow API to find our window[DllImportAttribute("User32.dll")]private static extern int FindWindow(String ClassName, String WindowName);//Import the SetForeground API to activate it[DllImportAttribute("User32.dll")]private static extern IntPtr SetForegroundWindow(int hWnd);public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//Find the window, using the CORRECT Window Title, for example, Notepadint hWnd = FindWindow(null, "Untitled - Notepad");if (hWnd > 0) //If found{SetForegroundWindow(hWnd); //Activate it}else{MessageBox.Show("Window Not Found!");}}}}

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist