I'm trying to find a way to get the open tasks in C#. I've been searching on google and can only find how to get a list of the processes. I want the only the tasks that would show up on the taskbar.
我正试图找到一种方法来获取C#中的开放任务。我一直在搜索谷歌,只能找到如何获得进程列表。我想要唯一会出现在任务栏上的任务。
Also, along with that, it would be cool if I could get the process the task is associated with. And if possible get the thumbnail images that Vista uses for the ALT-TAB menu, like in this image:
此外,如果我能够获得与任务相关联的过程,那将是很酷的。如果可能的话,获取Vista用于ALT-TAB菜单的缩略图,如下图所示:
I assume that I will have to use pinvokes because it really doesn't look like there are any libraries to do this already. Any ideas?
我假设我将不得不使用pinvokes因为它看起来真的不像有任何库已经这样做了。有任何想法吗?
4 个解决方案
#1
5
This article should pretty much tell you exactly what to do, it shows how to build your own task switch and includes the code needed to enumerate all windows and determine if they are "tasks" and it shows you how to use PrintWindow api to get the previews on XP.
这篇文章应该告诉你到底要做什么,它展示了如何构建自己的任务开关,并包括枚举所有窗口并确定它们是否是“任务”所需的代码,它向您展示了如何使用PrintWindow api来获取关于XP的预览。
http://msdn.microsoft.com/en-us/library/ms997649.aspx
Also, here is a blog post that talks about the algorithm used to determine what shows up in the Alt+Tab view. Basically you need to check the WS_EX_APPWINDOW and WS_EX_TOOLWINDOW along with if the window has an owner.
此外,这是一篇博客文章,讨论用于确定Alt + Tab视图中显示内容的算法。基本上,您需要检查WS_EX_APPWINDOW和WS_EX_TOOLWINDOW以及窗口是否拥有所有者。
#2
5
From an API (Win32) perspective there is no such thing as Tasks (at least not the one that Windows Task Manager/Alt-Tab shows).
从API(Win32)的角度来看,没有任务(至少不是Windows任务管理器/ Alt-Tab显示的那个)。
Those "Tasks" are actually top level windows.
那些“任务”实际上是*窗口。
So in order to get a list of those, you need to enumerate the windows (here is the PInvoke for it).
因此,为了获得这些列表,您需要枚举窗口(这里是PInvoke)。
Then look at the style of the windows to determine if they are actually top level windows.
然后查看窗口的样式以确定它们是否实际上是*窗口。
#3
1
I haven't tried it, but I suspect something like this:
我没有尝试过,但我怀疑这样的事情:
using System.Diagnostics;
static void MyFunc()
{
Process[] processes = Process.GetProcesses();
foreach(Process p in processes)
{
if (p.MainWindowHandle != 0)
{ // This is a GUI process
}
else
{ // this is a non-GUI / invisible process
}
}
}
The point is to check each process for a WindowHandle.
关键是要检查WindowHandle的每个进程。
#4
0
@abelenky17
I suspect that this will not cover all cases, for example there are processes who have several top level windows (that all appear in the task manager). Consider for example: FireFox, Windows Explorer, IE, etc... those applications can have multiple windows on the desktop. Also, it will not handle Terminal Sessions scenario's properly (because you enumerate all the processes running in the system).
我怀疑这不会涵盖所有情况,例如,有些进程有几个*窗口(都出现在任务管理器中)。考虑例如:FireFox,Windows资源管理器,IE等......这些应用程序可以在桌面上有多个窗口。此外,它不会正确处理终端会话场景(因为您枚举系统中运行的所有进程)。
@Dan C.
Doing something like this:
做这样的事情:
p.ProcessName != "explorer"
seems ok to you? It smells, bad.
对你好吗?真难闻。
#1
5
This article should pretty much tell you exactly what to do, it shows how to build your own task switch and includes the code needed to enumerate all windows and determine if they are "tasks" and it shows you how to use PrintWindow api to get the previews on XP.
这篇文章应该告诉你到底要做什么,它展示了如何构建自己的任务开关,并包括枚举所有窗口并确定它们是否是“任务”所需的代码,它向您展示了如何使用PrintWindow api来获取关于XP的预览。
http://msdn.microsoft.com/en-us/library/ms997649.aspx
Also, here is a blog post that talks about the algorithm used to determine what shows up in the Alt+Tab view. Basically you need to check the WS_EX_APPWINDOW and WS_EX_TOOLWINDOW along with if the window has an owner.
此外,这是一篇博客文章,讨论用于确定Alt + Tab视图中显示内容的算法。基本上,您需要检查WS_EX_APPWINDOW和WS_EX_TOOLWINDOW以及窗口是否拥有所有者。
#2
5
From an API (Win32) perspective there is no such thing as Tasks (at least not the one that Windows Task Manager/Alt-Tab shows).
从API(Win32)的角度来看,没有任务(至少不是Windows任务管理器/ Alt-Tab显示的那个)。
Those "Tasks" are actually top level windows.
那些“任务”实际上是*窗口。
So in order to get a list of those, you need to enumerate the windows (here is the PInvoke for it).
因此,为了获得这些列表,您需要枚举窗口(这里是PInvoke)。
Then look at the style of the windows to determine if they are actually top level windows.
然后查看窗口的样式以确定它们是否实际上是*窗口。
#3
1
I haven't tried it, but I suspect something like this:
我没有尝试过,但我怀疑这样的事情:
using System.Diagnostics;
static void MyFunc()
{
Process[] processes = Process.GetProcesses();
foreach(Process p in processes)
{
if (p.MainWindowHandle != 0)
{ // This is a GUI process
}
else
{ // this is a non-GUI / invisible process
}
}
}
The point is to check each process for a WindowHandle.
关键是要检查WindowHandle的每个进程。
#4
0
@abelenky17
I suspect that this will not cover all cases, for example there are processes who have several top level windows (that all appear in the task manager). Consider for example: FireFox, Windows Explorer, IE, etc... those applications can have multiple windows on the desktop. Also, it will not handle Terminal Sessions scenario's properly (because you enumerate all the processes running in the system).
我怀疑这不会涵盖所有情况,例如,有些进程有几个*窗口(都出现在任务管理器中)。考虑例如:FireFox,Windows资源管理器,IE等......这些应用程序可以在桌面上有多个窗口。此外,它不会正确处理终端会话场景(因为您枚举系统中运行的所有进程)。
@Dan C.
Doing something like this:
做这样的事情:
p.ProcessName != "explorer"
seems ok to you? It smells, bad.
对你好吗?真难闻。