【发布时间】:2012-10-19 02:58:01
【问题描述】:
在我的主窗体上,我有一个 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);
}
}
【问题讨论】:
-
我只是尝试用 BringWindowToTop(process.MainWindowHandle) 替换 ShowWindow(process.MainWindowHandle, SW_SHOWMAXIMIZED) 并且它使屏幕闪烁,但是没有任何反应。我所做的只是为BringWindowToTop 导入DLL 和extern 函数。这对我来说是新的,所以我需要做些不同的事情吗?
-
不要替换它在ShowWindow之后添加它
-
知道了,我马上试试……
-
你就是那个男人。效果很好!如果您输入一个快速的答案,我肯定会接受并投票,以便您获得信用。
标签: c# winforms process win32gui