Jul 6, 2011

How do I activate(show)/deactivate(hide) an external Window?

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

Activation of  the process:

        private void button1_Click(object sender, EventArgs e)
        {
            string path = "C:\\";
            string strArguments = " /report" + " " + path + "\\MS32INFO.txt";
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("msinfo32", strArguments);
         
            //System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("msinfo32.exe");
            psi.RedirectStandardOutput = false;
            psi.UseShellExecute = true;

            // Do not create the black window.
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
         
            System.Diagnostics.Process si = new System.Diagnostics.Process();
            si.StartInfo = psi;
            si.Start();
            //string output = si.StandardOutput.ReadToEnd();
            si.Close();
        }

Hiding the process:

 [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 [DllImportAttribute("User32.dll")]
    private static extern IntPtr FindWindow(String ClassName, String WindowName);

 private void button2_Click(object sender, EventArgs e)
 {

    IntPtr hWnd;

    hWnd = FindWindow(null, "System Information");
    ShowWindow(hWnd, 0);

}
    }
}




when u click on the button1 the process will start when u click on the button 2 the process external window will hide and still running



 Here is an example for note pad :



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






No comments:

Post a Comment