C# 利用PROCESS 開啟外部程式並且利用API控制縮小,最後再將開啟程式關閉(範例控制對象USBWebserver)

C# 利用PROCESS 開啟外部程式並且利用API控制縮小,最後再將開啟程式關閉(範例控制對象USBWebserver)

C# 利用PROCESS
開啟外部程式並且利用API控制縮小,最後再將開啟程式關閉(範例控制對象
USBWebserver)


 

資料來源: 01.http://stackoverflow.com/questions/18652162/how-to-minimize-maximize-opened-applications

02.http://www.pinvoke.net/index.aspx(再.NET使用WINDOWS API教學網站)

03. http://www.usbwebserver.com/(USBWebserver官方網站)

 

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;

//http://fecbob.pixnet.net/blog/post/38088805-c%23-%E5%95%9F%E5%8B%95%E5%A4%96%E9%83%A8%E7%A8%8B%E5%BC%8F

/*

C# ÓOXED~!g!o¦XOekG

1. ÓOXED~!g!A¢Gg¢DYLa退XhDXC

2. ÓOXED~!g!Ag¢DYLa退XhDXC

3. ÓOXED~!g!AgLg¢DYLa退XhDXC

4. ÓOXED~!g!AqLLADoEgoLa退XhDXC

 

// using System.Diagnostics;

private string appName = “calc.exe”;

 

/// <summary>

/// 1. ÓOXED~!g!A¢Gg¢DYLa退XhDX

/// </summary>

private void button1_Click(object sender, EventArgs e)

{

Process.Start(appName);

MessageBox.Show(String.Format(“D~!g! {0} ÓOXE±1¡LI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

 

/// <summary>

/// 2. ÓOXED~!g!Ag¢DYLa退XhDX

/// </summary>

private void button2_Click(object sender, EventArgs e)

{

try

{

Process proc = Process.Start(appName);

if (proc != null)

{

proc.WaitForExit(3000);

if (proc.HasExited)

MessageBox.Show(String.Format(“D~!g! {0} wMg退XhDXI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

else

{

// pGD~!g!LS3g2±oBahÓja¡Ñi¡±C

proc.Kill();

MessageBox.Show(String.Format(“D~!g! {0} QÓja¡ÑiI”,
this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

}

}

catch (ArgumentException ex)

{

MessageBox.Show(ex.Message, this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

 

/// <summary>

/// 3. ÓOXED~!g!AgLg¢DYLa退XhDX

/// </summary>

private void button3_Click(object sender, EventArgs e)

{

try

{

Process proc = Process.Start(appName);

if (proc != null)

{

proc.WaitForExit();

MessageBox.Show(String.Format(“D~!g! {0} wMg退XhDXI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

catch (ArgumentException ex)

{

MessageBox.Show(ex.Message, this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

 

/// <summary>

/// 4. ÓOXED~!g!AqLLADoEgoLa退XhDX

/// </summary>

private void button4_Click(object sender, EventArgs e)

{

try

{

// ÓOXED~!g!

Process proc = Process.Start(appName);

if (proc != null)

{

// Egoig退XhDX

proc.EnableRaisingEvents = true;

// uw退XhDXLADoek

proc.Exited += new EventHandler(proc_Exited);

}

}

catch (ArgumentException ex)

{

MessageBox.Show(ex.Message, this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

 

/// <summary>

/// ÓOXED~!g!退XhDXLADo

/// </summary>

void proc_Exited(object sender, EventArgs e)

{

MessageBox.Show(String.Format(“D~!g! {0} wMg退XhDXI”, this.appName), this.Text,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

*/

 

namespace WinForm_Process_test

{

 

    public partial class Form1 : Form

    {

        private
const int
SW_MAXIMIZE = 3;

        private
const int
SW_MINIMIZE = 6;

        //http://www.pinvoke.net/default.aspx/user32.showwindow

        [DllImport(“user32.dll”, EntryPoint = “FindWindow”)]

        public
static extern
IntPtr FindWindowByCaption(IntPtr ZeroOnly, string
lpWindowName);

        [DllImport(“user32.dll”)]

        [return:
MarshalAs(UnmanagedType.Bool)]

        static
extern bool
ShowWindow(IntPtr hWnd, int nCmdShow);

 

        private
Process m_pro;

        public
int m_id;

        public
Form1()

        {

            InitializeComponent();

           

        }

 

        private
void button1_Click(object
sender, EventArgs e)

        {

            if
(m_pro == null)

            {

                /*

                ProcessStartInfo
startInfo = new ProcessStartInfo(“cmd.exe”,”/c
c:\\123.bat”);

               
startInfo.WindowStyle = ProcessWindowStyle.Normal;

                */

                m_pro = Process.Start(@”D:\USBWebserver
v8.6\usbwebserver.exe”
);

                m_id = m_pro.Id;

            }

        }

 

        private
void button2_Click(object
sender, EventArgs e)

        {

            if
(m_pro != null)

            {

                if
(!m_pro.HasExited)//
LuÓog!O±_wXoa±1¡L

                {

                    try

                    {

                        Process[] javaProcList = Process.GetProcessesByName(“usbwebserver”);//nDyo?BATooXoag! ±_hgLko?BAT
2015/12/02

                        foreach (Process
javaProc in javaProcList)

                        {

                            javaProc.Kill();

                            javaProc.Close();

                           
javaProc.Dispose();

                        }

                        m_pro.Close();

                        m_pro.Dispose();

                        m_pro = null;

                    }

                    catch
(Exception exp)

                    {

                    }

                }

            }

        }

 

        private
void button3_Click(object
sender, EventArgs e)

        {

            IntPtr
hwnd = FindWindowByCaption(IntPtr.Zero, “USBWebserver”);

            ShowWindow(hwnd, SW_MINIMIZE);

        }

    }

}

 

_

 


 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *