CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    C# General : How do I activate an external Window?

    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 :
    Code:
     using System.Runtime.InteropServices; //required for APIs
    Declare the FindWindow API :
    Code:
            //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 :
    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!");
                }
          
    
            }
    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 :

    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!");
                }
            }
    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 :

    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!");
                }
          
    
            }
        }
    }
    Q: Can I have a complete example, using SetForegroundWindow with Processes?

    A: Here is the Process & SetForegroundWindow example :

    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);
                }
            }
        }
    }
    I am attaching examples of both methods described, with this post.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured