# CodeGuru Technical FAQs > CodeGuru Individual FAQs >  C# General : How do I activate an external Window?

## HanneSThEGreaT

*Q:* How do I get access to the window I'd like to activate?

*A:* You can use the FindWindow API, or you can use the System.Diagnostics.Process object.

*Q:* Can I have an example of using the FindWindow API?

*A:* Yes, here is an example of using the FindWindow API to find the Notepad window :

Import the System.Runtime.InterOpServices namespace :


```
 using System.Runtime.InteropServices; //required for APIs
```

Declare the FindWindow API :


```
        //Import the FindWindow API to find our window
        [DllImportAttribute("User32.dll")]
        private static extern int FindWindow(String ClassName, String WindowName);
```

Get the Notepad window using FindWindow :


```
        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
            {
                //Do Something
            }
            else //Not Found
            {
                MessageBox.Show("Window Not Found!");
            }
      

        }
```

*Q:* can I have an example of using the Process object to obtain a "handle" to the Notepad window?

*A:* Yes, here is an example :



```
        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
            if (p.Length > 0)
            {
                //Do Something
            }
	    else //Not Found
            {
                MessageBox.Show("Window Not Found!");
            }
        }
```

*Q:* OK, I've got the handle to the window I want to activate, how to I bring that window to the front?

*A:* Use the SetForegroundWindow API

*Q:* Can I have a full example of getting a window handle, and activating it, With FindWindow and SetForegroundWindow?

*A:* Here is the FindWindow & SetForegroundWindow example :



```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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!");
            }
      

        }
    }
}
```

*Q:* Can I have a complete example, using SetForegroundWindow with Processes?

*A:* Here is the Process & SetForegroundWindow example :



```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProcessWindows
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
            if (p.Length > 0)
            {
                SetForegroundWindow(p[0].MainWindowHandle);
            }
        }
    }
}
```

I am attaching examples of both methods described, with this post.

----------

