Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: .NET
Expertise: Beginner
Sep 30, 2016

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 APIs

namespace 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, Notepad

int hWnd = FindWindow(null, "Untitled - Notepad");

if (hWnd > 0) //If found

{

SetForegroundWindow(hWnd); //Activate it

}

else

{

MessageBox.Show("Window Not Found!");

}



}

}

}
Hannes du Preez
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date