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 :
Declare the FindWindow API :Code:using System.Runtime.InteropServices; //required for APIs
Get the Notepad window using FindWindow :Code://Import the FindWindow API to find our window [DllImportAttribute("User32.dll")] private static extern int FindWindow(String ClassName, String WindowName);
Q: can I have an example of using the Process object to obtain a "handle" to the Notepad window?Code: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!"); } }
A: Yes, here is an example :
Q: OK, I've got the handle to the window I want to activate, how to I bring that window to the front?Code: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!"); } }
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 :
Q: Can I have a complete example, using SetForegroundWindow with Processes?Code: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!"); } } } }
A: Here is the Process & SetForegroundWindow example :
I am attaching examples of both methods described, with this post.Code: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); } } } }


Reply With Quote