//启动程序
private void button57_Click(object sender, EventArgs e)
{
Process processInfo = null;
Process[] ps = Process.GetProcesses(System.Environment.MachineName); //Process.GetProcesses();
foreach (Process p in ps)
{
if (p.MainWindowHandle != null)
{
if (p.MainWindowTitle== "Control Panel Manager")
{
processInfo = p;
SwitchToThisWindow(processInfo.MainWindowHandle, true); //top
}
else
{
bool stat = false;
foreach (Process p2 in ps)
{
if (p2.MainWindowTitle == "Control Panel Manager") {
stat = true;
processInfo = p2;
}
}
if (stat)
{
MessageBox.Show("已打开,请用其他方式再次打开");
SwitchToThisWindow(processInfo.MainWindowHandle, true); //top
break;
}
else
{
string pst = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\CPM.lnk";
System.Diagnostics.Process.Start(pst);
break;
}
}
}
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
p.MainWindowTitle== "Control Panel Manager"这个以我愚见是类似与运行的主程序的界面名
如这两个的子名字,,我要实现的效果也是打开某些程序/或者已经运行的就让其top,,,,,尝试了许多次,最开始以为不行,上面那个程序是通过cmd启动的,,试了许多变抓不到,,,,然后突然有感觉了,看了许多人的案例也都没找到我想要的效果,,,后面终于看到类似的,自己修修改改,组合一下逻辑 还真实现了