屏幕捕获需要目前使用'开始应用程序

时间:2011-03-07 22:00:09

标签: c# screen-capture windows-applications

需要知道当前打开的应用程序:任务管理器中的所有应用程序。

这是用于屏幕捕获,因此如果可能,我需要访问屏幕上这些应用程序的位置。

使用.net c#表达式编码器

2 个答案:

答案 0 :(得分:1)

任务栏中有official documentation个窗口。

无论如何,这样的事情应该贯穿一般的想法。您可以自己对细节进行排序,因为您知道要在哪里查看。

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;
    }
}

答案 1 :(得分:0)

您可以使用托管的System.Diagnostic.Processes类:

Process[] running = Process.GetProcesses();

foreach(Process p in running)
  Console.WriteLine(p.ProcessName);