需要知道当前打开了哪些应用程序:任务管理器中的所有应用程序。
这是用于屏幕截图,所以如果可能的话,我需要访问这些应用程序在屏幕上的位置。
使用 .net c# 表达式编码器
需要知道当前打开了哪些应用程序:任务管理器中的所有应用程序。
这是用于屏幕截图,所以如果可能的话,我需要访问这些应用程序在屏幕上的位置。
使用 .net c# 表达式编码器
有关于哪些窗口出现在任务栏中的官方文档。
无论如何,这样的事情应该可以理解一般的想法。既然您知道在哪里看,您就可以自己整理细节。
using System;
using System.Runtime.InteropServices;
using System.Text;
public delegate bool CallBack(IntPtr hWnd, IntPtr lParam);
public class EnumTopLevelWindows {
[DllImport("user32", SetLastError=true)]
private static extern int EnumWindows(CallBack x, IntPtr y);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowLong", SetLastError = true)]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public static string GetText(IntPtr hWnd)
{
// Allocate correct string length first
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
private const int GWL_STYLE = -16;
private const int WS_EX_APPWINDOW = 0x00040000;
public static void Main()
{
CallBack myCallBack = new CallBack(EnumTopLevelWindows.Report);
EnumWindows(myCallBack, IntPtr.Zero);
}
public static bool Report(IntPtr hWnd, IntPtr lParam)
{
if (GetParent(hWnd) == IntPtr.Zero)
{
//window is a non-owned top level window
if (IsWindowVisible(hWnd))
{
//window is visible
int style = GetWindowLongPtr(hWnd, GWL_STYLE).ToInt32();
if ((style & WS_EX_APPWINDOW) == WS_EX_APPWINDOW)
{
//window has WS_EX_APPWINDOW style
Console.WriteLine(GetText(hWnd));
}
}
}
return true;
}
}
您可以使用托管 System.Diagnostic.Processes 类:
Process[] running = Process.GetProcesses();
foreach(Process p in running)
Console.WriteLine(p.ProcessName);