在我的主窗体上,我有一个 ListView,其中包含属于我们软件套件的进程的 LargeIcon 视图列表。每个 LV (ListView) 项目都包含文本和图像,该图像与我们的软件产品的图标相同,用于相应的应用程序(即 MarinaOffice、LaunchOffice、PureRental 等)。有一个计时器根据是否在 Process.GetProcesses() 方法调用中找到进程来更新列表。当用户单击正在运行的进程的 ListView 项时,它应该最大化并在当前进程前面显示该进程(即 WinForms 应用程序)的窗口。下面的代码几乎可以完成我想要完成的工作,但是,如果我想要显示的应用程序已经最大化,但是在我在监视器上的 windows 应用程序后面,它没有将我展示的过程放在我的应用程序前面。换句话说,只要我使用下面的 WIN32 方法显示的应用程序被最小化,它就可以工作。但是,它已经最大化,它没有。
// Used to Show Other Process Windows
private const int SW_SHOWMAXIMIZED = 3;
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
// Method that checks for running apps in our suite every 3000ms
private void timerRunningApps_Tick(object sender, EventArgs e)
{
CheckRunningapps();
}
// Stripped down version of method that looks for running process in our suite and
// adds the process name and icon to a ListView on our main form
private void CheckRunningapps()
{
List<Process> AllProcesses = System.Diagnostics.Process.GetProcesses().ToList();
listViewRunningApps.BeginUpdate();
listViewRunningApps.Items.Clear();
foreach (Process process in AllProcesses)
{
ListViewItem lvi = new ListViewItem();
if (process.ProcessName.ToLower().Contains("marinaoffice"))
{
lvi = new ListViewItem("MarinaOffice");
lvi.SubItems.Add("MarinaOffice");
lvi.ImageIndex = 1;
lvi.Tag = process;
listViewRunningApps.Items.Add(lvi);
}
}
listViewRunningApps.EndUpdate();
}
// Method that actually shows the process. This works as long as process is minimized
// However, if the process is maximized but, merely behind the current window it does
// not bring it in front. I have noticed that there is a ShowWindowAsync method.
// Should I use that instead?
private void listViewRunningApps_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewItem lvi = listViewRunningApps.GetItemAt(e.X, e.Y);
if (lvi != null)
{
Process process = (Process)lvi.Tag;
ShowWindow(process.MainWindowHandle, SW_SHOWMAXIMIZED);
}
}