C#如何呼叫外部程式,及該托盤程式的恢復與隱藏
第一次寫部落格,太OUT了……
現狀:專案中要呼叫一個外部的應用程式,該程式最小化之後,隱藏在系統托盤中
要求:如果該程式沒有執行,則啟動,如果被隱藏在托盤中,則恢復到使用狀態
看了網上的資料,好多程式碼超級複雜,得碼半天,
ShowWindow中的第二個引數: 0,表示隱藏,1表示正常顯示,2表示最小化,3表示最大化
直接程式碼:
using System.Diagnostics;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string frmText);
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int showWay);
public static IntPtr frmHwnd;
// 下面的程式碼,放到該放的地方去
try
{
Process[] pros= Process.GetProcessesByName("程式程序名");
if (pros.ToList().Count > 0)//存在該程序
{
IntPtr handle = pros[0].MainWindowHandle;
if (handle.ToInt32() == 0)
{
frmHwnd
ShowWindow(frmHwnd,1);//顯示窗體
}
else
{
ShowWindow(handle, 0);// 隱藏窗體
}
}
else//不存在,則啟動該程式
{
string road = "程式路徑";
ProcessStartInfo myStartInfo
myStartInfo.FileName = road;
Process myProc= new Process();
myProc.StartInfo = MyStarInfo;
myProc.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
}