Reputation: 4987
On my main form I have a ListView that contains a list in LargeIcon View of the Processes that belong to our software suite. Each LV (ListView) item contains both text and an image which is identical to the icon for our software product for the respective application (i.e. MarinaOffice, LaunchOffice, PureRental, etc...). There is a timer that updates the list based on whether or not the process is found in Process.GetProcesses() method call. When the user clicks on the ListView item for the running process, it should maximize and show the window for that process (i.e WinForms Application) in front of the current process. The code below almost does what I want to accomplish but, in the case where the application that I want to show is already maximized but, behind the windows application that I have on monitor, it does not put the process that I am showing in front of my application. In other words, as long as the application that I am showing using the WIN32 method below is minimized, it works. But, it it is already maximized, it does not.
// 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);
}
}
Upvotes: 0
Views: 575
Reputation: 54552
Try something like this, I think what is going on is that you are not changing the Window state with ShowWindow Call if the Window is already Maximized in that case you need to also bring it to the front of the Z-Order by using the BringWindowToTop Method.
Process process = (Process)lvi.Tag;
ShowWindow(process.MainWindowHandle, SW_SHOWMAXIMIZED);
BringWindowToTop(process.MainWindowHandle);
Upvotes: 1